package com.ruoyi.cai.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cai.domain.Gift;
import com.ruoyi.cai.domain.Goods;
import com.ruoyi.cai.domain.ReportCate;
import com.ruoyi.cai.domain.WithdrawExchange;
import com.ruoyi.cai.dto.app.vo.AgreementDTO;
import com.ruoyi.cai.dto.app.vo.GiftVo;
import com.ruoyi.cai.dto.app.vo.setting.SettingGoodsVo;
import com.ruoyi.cai.dto.app.vo.setting.WithdrawExchangeVo;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.manager.SystemConfigManager;
import com.ruoyi.cai.service.*;
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 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 = "获取设置相关接口")
public class SettingAppController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private WithdrawExchangeService withdrawExchangeService;
    @Autowired
    private ReportCateService reportCateService;
    @Autowired
    private GiftService giftService;
    @Autowired
    private AgreementSettingService agreementSettingService;
    @Autowired
    private SystemConfigManager systemConfigManager;

    @GetMapping("/goods")
    @Operation(summary = "充值配置")
    @Log(title = "充值配置", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<SettingGoodsVo> goods(){
        SettingGoodsVo vo = new SettingGoodsVo();
        vo.setHasAlipayPay(systemConfigManager.getSystemConfigOfInt(SystemConfigEnum.OPEN_ALI_PAY));
        vo.setHasWechatPay(systemConfigManager.getSystemConfigOfInt(SystemConfigEnum.OPEN_WX_PAY));
        vo.setAlipayFirst(systemConfigManager.getSystemConfigOfInt(SystemConfigEnum.ALI_PAY_FIRST));
        List<Goods> list = goodsService.list(Wrappers.lambdaQuery(Goods.class)
                .eq(Goods::getStatus,0)
                .orderByAsc(Goods::getPrice));
        vo.setGoods(list);
        return R.ok(vo);
    }

    @GetMapping("/withdraw")
    @Operation(summary = "提现配置")
    @Log(title = "提现配置", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<WithdrawExchangeVo> withdraw(){
        WithdrawExchangeVo vo = new WithdrawExchangeVo();
        List<WithdrawExchange> list = withdrawExchangeService.list(Wrappers.lambdaQuery(WithdrawExchange.class)
                        .eq(WithdrawExchange::getStatus,0)
                .orderByAsc(WithdrawExchange::getMoney));
        vo.setWithdrawRuleRemark("平台50元起提现，最快2小时到账（每天不限次数），22点后提现次日中午10点到账");
        vo.setList(list);
        return R.ok(vo);
    }

    @GetMapping("/report")
    @Operation(summary = "举报分类配置")
    @Log(title = "举报分类配置", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<ReportCate>> report(){
        List<ReportCate> list = reportCateService.list(Wrappers.lambdaQuery(ReportCate.class));
        return R.ok(list);
    }


    @GetMapping("/gift")
    @Operation(summary = "礼物配置")
    @Log(title = "礼物配置", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<GiftVo>> gift(){
        List<Gift> res = giftService.all();
        return R.ok(BeanConvertUtil.convertListTo(res, GiftVo::new));
    }

    @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/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));
    }

}
