package com.ruoyi.xq.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.BeanConvertUtil;
import com.ruoyi.xq.domain.AreaCode;
import com.ruoyi.xq.dto.app.areacode.AreaCodeHomeVo;
import com.ruoyi.xq.dto.app.areacode.AreaCodeQuery;
import com.ruoyi.xq.dto.app.areacode.AreaCodeTree;
import com.ruoyi.xq.dto.app.setting.AgreementDTO;
import com.ruoyi.xq.service.AgreementSettingService;
import com.ruoyi.xq.service.AreaCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/setting")
@Tag(name = "获取设置相关接口")
@SaIgnore
public class SettingAppController {

    @Autowired
    private AgreementSettingService agreementSettingService;
    @Autowired
    private AreaCodeService areaCodeService;

    @GetMapping("/agreement/user")
    @Operation(summary = "获取用户协议")
    @Log(title = "获取用户协议", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<AgreementDTO> userAgreement() {
        String userAgreement = agreementSettingService.getAgreementSetting().getUserAgreement();
        return R.ok(new AgreementDTO(userAgreement));
    }

    @GetMapping("/agreement/privacy")
    @Operation(summary = "获取隐私协议")
    @Log(title = "获取隐私协议", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<AgreementDTO> privacyAgreement() {
        String privacyAgreement = agreementSettingService.getAgreementSetting().getPrivacyAgreement();
        return R.ok(new AgreementDTO(privacyAgreement));
    }

    @GetMapping("/agreement/anchorJoin")
    @Operation(summary = "用户主播入驻协议")
    @Log(title = "用户主播入驻协议", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<AgreementDTO> anchorJoinAgreement() {
        String anchorJoinAgreement = agreementSettingService.getAgreementSetting().getAnchorJoinAgreement();
        return R.ok(new AgreementDTO(anchorJoinAgreement));
    }

    @GetMapping("/areaCode/homeCity")
    @Operation(summary = "获取首页城市选择列表")
    @Log(title = "获取首页城市选择列表", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<AreaCodeHomeVo>> homeCity(){
        List<AreaCode> list = areaCodeService.list(Wrappers.lambdaQuery(AreaCode.class).eq(AreaCode::getLevel, "city"));
        List<AreaCodeHomeVo> vos = BeanConvertUtil.convertListTo(list, AreaCodeHomeVo::new);
        return R.ok(vos);
    }


    @GetMapping("/areaCode/list")
    @Operation(summary = "获取省市区编码接口")
    @Log(title = "获取省市区编码接口", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<AreaCode>> listAreaCode(AreaCodeQuery query){
        List<AreaCode> list = areaCodeService.listAreaCode(query);
        return R.ok(list);
    }

    @GetMapping("/areaCode/tree")
    @Operation(summary = "获取所有省市区编码")
    @Log(title = "获取所有省市区编码", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<AreaCodeTree>> areaCodeTree(){
        List<AreaCodeTree> node = areaCodeService.tree();
        return R.ok(node);
    }

}
