package com.hzs.system.config.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.config.BdWithdrawalTax;
import com.hzs.common.domain.system.config.BdWithdrawalTaxDetails;
import com.hzs.common.domain.system.config.ext.BdWithdrawalAutoExt;
import com.hzs.common.domain.system.config.ext.BdWithdrawalTaxDetailsExt;
import com.hzs.common.domain.system.config.ext.BdWithdrawalTaxExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.config.IWithdrawalTaxServiceApi;
import com.hzs.system.config.service.IBdWithdrawalService;
import com.hzs.system.config.service.IBdWithdrawalTaxDetailsService;
import com.hzs.system.config.service.IBdWithdrawalTaxService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 个税配置表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-06
 */
@Slf4j
@RestController
@RequestMapping("/manage/withdrawal-tax")
public class BdWithdrawalTaxController extends BaseController {

    @Autowired
    private IBdWithdrawalTaxService withdrawalTaxService;
    @Autowired
    private IBdWithdrawalTaxDetailsService withdrawalTaxDetailsService;
    @Autowired
    IBdCurrencyService currencyService;
    @DubboReference
    private IWithdrawalTaxServiceApi withdrawalTaxServiceApi;
    @Autowired
    private IBdWithdrawalService withdrawalService;
    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * @description: 查询个税配置表列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [bdWithdrawal]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_TAX,method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdWithdrawalTax withdrawalTax) {
        QueryWrapper<BdWithdrawalTax> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        BdWithdrawalTax bwt = withdrawalTaxService.getOne(queryWrapper);
        List<BdWithdrawalTaxDetailsExt> bwtdListExt = new ArrayList<>();
        if (StringUtils.isNotNull(bwt)) {
            QueryWrapper<BdWithdrawalTaxDetails> queryWrapperDetails = new QueryWrapper();
            queryWrapperDetails.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
            queryWrapperDetails.eq("PK_WITHDRAWAL_TAX", bwt.getPkId());
            startPage();
            List<BdWithdrawalTaxDetails> bwtdList = withdrawalTaxDetailsService.list(queryWrapperDetails);
            if (CollectionUtil.isNotEmpty(bwtdList)) {
                for (BdWithdrawalTaxDetails bwtd : bwtdList) {
                    BdWithdrawalTaxDetailsExt bwtdExt = BeanUtil.copyProperties(bwtd, BdWithdrawalTaxDetailsExt.class);
                    bwtdExt.setLowerBoundUsa(currencyService.getDollar(bwtdExt.getLowerBound(),SecurityUtils.getPkCountry()));
                    bwtdExt.setUpperBoundUsa(currencyService.getDollar(bwtdExt.getUpperBound(),SecurityUtils.getPkCountry()));
                    bwtdExt.setServiceType(bwt.getServiceType());
                    bwtdExt.setTaxCycle(bwt.getTaxCycle());
                    bwtdExt.setEffectiveDate(bwt.getEffectiveDate());
                    bwtdListExt.add(bwtdExt);
                }
            }
        }
        return getDataTable(bwtdListExt);
    }

    /**
     * @description: 导出提现个税配置
     * @author: zhang jing
     * @date: 2023/3/3 16:31
     * @param: [response, withdrawalTax]
     * @return: void
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_TAX,method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdWithdrawalTax withdrawalTax) {
        QueryWrapper<BdWithdrawalTax> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        BdWithdrawalTax bwt = withdrawalTaxService.getOne(queryWrapper);
        List<BdWithdrawalTaxDetailsExt> bwtdListExt = new ArrayList<>();
        if (StringUtils.isNotNull(bwt)) {
            QueryWrapper<BdWithdrawalTaxDetails> queryWrapperDetails = new QueryWrapper();
            queryWrapperDetails.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
            queryWrapperDetails.eq("PK_WITHDRAWAL_TAX", bwt.getPkId());
            startPage();
            List<BdWithdrawalTaxDetails> bwtdList = withdrawalTaxDetailsService.list(queryWrapperDetails);
            if (CollectionUtil.isNotEmpty(bwtdList)) {
                for (BdWithdrawalTaxDetails bwtd : bwtdList) {
                    BdWithdrawalTaxDetailsExt bwtdExt = BeanUtil.copyProperties(bwtd, BdWithdrawalTaxDetailsExt.class);
                    bwtdExt.setLowerBoundUsa(currencyService.getDollar(bwtdExt.getLowerBound(),SecurityUtils.getPkCountry()));
                    bwtdExt.setUpperBoundUsa(currencyService.getDollar(bwtdExt.getUpperBound(),SecurityUtils.getPkCountry()));
                    bwtdExt.setServiceType(bwt.getServiceType());
                    bwtdExt.setTaxCycle(bwt.getTaxCycle());
                    bwtdExt.setEffectiveDate(bwt.getEffectiveDate());
                    bwtdListExt.add(bwtdExt);
                }
            }
        }
        ExcelUtil<BdWithdrawalTaxDetailsExt> util = new ExcelUtil<>(BdWithdrawalTaxDetailsExt.class);
        util.exportExcel(response, bwtdListExt, "提现个税配置");
    }


    /**
     * @description: 新增个税配置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bdWithdrawal]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_TAX,method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdWithdrawalTaxExt withdrawalTaxExt) {
        if (StringUtils.isNull(withdrawalTaxExt.getTaxCycle())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAX_CYCLE_NOT_NULL));
        }

        if (withdrawalTaxExt.getTaxCycle() < 0 && withdrawalTaxExt.getTaxCycle() > 12) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAX_CYCLE_NOT_STANDARD));
        }
        if (StringUtils.isNull(withdrawalTaxExt.getServiceType())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAX_SERVICE_TYPE_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalTaxExt.getEffectiveDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }

        if (CollectionUtil.isEmpty(withdrawalTaxExt.getWithdrawalTaxDetailsList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_WITHDRAWAL_TAX_DETAILS_NOT_NULL));
        } else {
            for (BdWithdrawalTaxDetails bwtd : withdrawalTaxExt.getWithdrawalTaxDetailsList()) {
                if (StringUtils.isNull(bwtd.getLowerBound())) {
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAX_LOWER_BOUND_NOT_NULL));
                }
                if (StringUtils.isNull(bwtd.getUpperBound())) {
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAX_MAX_AMOUNT_NOT_NULL));
                }
                if (StringUtils.isNull(bwtd.getRatio())) {
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TAX_RATIO_NOT_NULL));
                }
                bwtd.setPkCountry(SecurityUtils.getPkCountry());
                bwtd.setPkCreator(SecurityUtils.getUserId());
                bwtd.setCreationTime(new Date());
            }
        }
        withdrawalTaxExt.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalTaxExt.setPkCreator(SecurityUtils.getUserId());
        withdrawalTaxExt.setCreationTime(new Date());
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL_TAX + SecurityUtils.getPkCountry());
        return toAjax(withdrawalTaxService.saveWithdrawalTax(withdrawalTaxExt));
    }




    /**
     * @description: 查询单条个税配置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_TAX,method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdWithdrawalTaxExt withdrawalTaxExt = withdrawalTaxService.getEithdrawalTaxExt(pkId);
        return AjaxResult.success(withdrawalTaxExt);
    }


    /**
     * @description: 删除个税配置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_TAX,method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        BdWithdrawalAutoExt withdrawalAutoExt =new BdWithdrawalAutoExt();
        withdrawalAutoExt.setPkCountry(SecurityUtils.getPkCountry());
        Integer count= withdrawalService.checkWithdraw(withdrawalAutoExt);
        if(count>0){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ENABLE_STATE_NOT_DELETE));
        }
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL_TAX + SecurityUtils.getPkCountry());
        return toAjax(withdrawalTaxService.deleteWithdrawalTax(pkId));
    }
}
