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.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
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.BdWithdrawal;
import com.hzs.common.domain.system.config.BdWithdrawalAuto;
import com.hzs.common.domain.system.config.ext.BdWithdrawalAutoExt;
import com.hzs.common.domain.system.config.ext.BdWithdrawalExt;
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.service.IBdWithdrawalAutoService;
import com.hzs.system.config.service.IBdWithdrawalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 提现设置主表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/withdrawal")
public class BdWithdrawalController extends BaseController {

    @Autowired
    private IBdWithdrawalService iBdWithdrawalService;
    @Autowired
    private IBdWithdrawalAutoService iBdWithdrawalAutoService;
    @Autowired
    private IBdCurrencyService iBdCurrencyService;

    /**
     * 查询提现设置列表 理论每个国家只有一个提现配置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdWithdrawalExt withdrawalExt) {
        startPage();
        withdrawalExt.setPkCountry(SecurityUtils.getPkCountry());
        List<BdWithdrawalExt> listExt = iBdWithdrawalService.listExt(withdrawalExt);
        for (BdWithdrawalExt bwd : listExt) {
            //暂时使用中国
            if (StringUtils.isNotNull(bwd.getMinAmount())) {
                bwd.setMinAmountUsa(iBdCurrencyService.getDollar(bwd.getMinAmount(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getMaxAmount())) {
                bwd.setMaxAmountUsa(iBdCurrencyService.getDollar(bwd.getMaxAmount(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getLowerBound())) {
                bwd.setLowerBoundUsa(iBdCurrencyService.getDollar(bwd.getLowerBound(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getUpperBound())) {
                bwd.setUpperBoundUsa(iBdCurrencyService.getDollar(bwd.getUpperBound(), SecurityUtils.getPkCountry()));
            }

            QueryWrapper<BdWithdrawalAuto> queryWrapperAuto = new QueryWrapper<>();
            queryWrapperAuto.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
            if (StringUtils.isNotNull(bwd.getPkId())) {
                queryWrapperAuto.eq("PK_WITHDRAWAL", bwd.getPkId());
            }
            List<BdWithdrawalAuto> listAuto = iBdWithdrawalAutoService.list(queryWrapperAuto);
            List<BdWithdrawalAutoExt> watExtList = new ArrayList<>();
            listAuto.forEach(item -> watExtList.add(BeanUtil.copyProperties(item, BdWithdrawalAutoExt.class)));
            bwd.setWithdrawalAutoList(watExtList);
        }
        return getDataTable(listExt);
    }

    /**
     * 导出提现配置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        QueryWrapper<BdWithdrawal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawal> list = iBdWithdrawalService.list(queryWrapper);
        List<BdWithdrawalExt> listExt = new ArrayList<>();
        for (BdWithdrawal bwd : list) {
            BdWithdrawalExt withdrawalExtNew = BeanUtil.copyProperties(bwd, BdWithdrawalExt.class);
            //暂时使用中国
            if (StringUtils.isNotNull(bwd.getMinAmount())) {
                withdrawalExtNew.setMinAmountUsa(iBdCurrencyService.getDollar(bwd.getMinAmount(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getMaxAmount())) {
                withdrawalExtNew.setMaxAmountUsa(iBdCurrencyService.getDollar(bwd.getMaxAmount(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getLowerBound())) {
                withdrawalExtNew.setLowerBoundUsa(iBdCurrencyService.getDollar(bwd.getLowerBound(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getUpperBound())) {
                withdrawalExtNew.setUpperBoundUsa(iBdCurrencyService.getDollar(bwd.getUpperBound(), SecurityUtils.getPkCountry()));
            }

            QueryWrapper<BdWithdrawalAuto> queryWrapperAuto = new QueryWrapper<>();
            queryWrapperAuto.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
            if (StringUtils.isNotNull(bwd.getPkId())) {
                queryWrapperAuto.eq("PK_WITHDRAWAL", bwd.getPkId());
            }
            List<BdWithdrawalAuto> listAuto = iBdWithdrawalAutoService.list(queryWrapperAuto);
            List<BdWithdrawalAutoExt> watExtList = new ArrayList<>();
            listAuto.forEach(item -> watExtList.add(BeanUtil.copyProperties(item, BdWithdrawalAutoExt.class)));
            withdrawalExtNew.setWithdrawalAutoList(watExtList);
            listExt.add(withdrawalExtNew);
        }
        ExcelUtil<BdWithdrawalExt> util = new ExcelUtil<>(BdWithdrawalExt.class);
        util.exportExcel(response, listExt, "提现设置");
    }

    /**
     * 提现手续费配置，理论每个国家只有一个提现配置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/serviceChargelist")
    public TableDataInfo serviceChargelist() {
        startPage();
        QueryWrapper<BdWithdrawal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawal> list = iBdWithdrawalService.list(queryWrapper);
        List<BdWithdrawalExt> listExt = new ArrayList<>();
        for (BdWithdrawal bwd : list) {
            BdWithdrawalExt withdrawalExtNew = BeanUtil.copyProperties(bwd, BdWithdrawalExt.class);
            //暂时使用中国
            if (StringUtils.isNotNull(bwd.getMinAmount())) {
                withdrawalExtNew.setMinAmountUsa(iBdCurrencyService.getDollar(bwd.getMinAmount(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getMaxAmount())) {
                withdrawalExtNew.setMaxAmountUsa(iBdCurrencyService.getDollar(bwd.getMaxAmount(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getLowerBound())) {
                withdrawalExtNew.setLowerBoundUsa(iBdCurrencyService.getDollar(bwd.getLowerBound(), SecurityUtils.getPkCountry()));
            }
            if (StringUtils.isNotNull(bwd.getUpperBound())) {
                withdrawalExtNew.setUpperBoundUsa(iBdCurrencyService.getDollar(bwd.getUpperBound(), SecurityUtils.getPkCountry()));
            }

            QueryWrapper<BdWithdrawalAuto> queryWrapperAuto = new QueryWrapper<>();
            queryWrapperAuto.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
            if (StringUtils.isNotNull(bwd.getPkId())) {
                queryWrapperAuto.eq("PK_WITHDRAWAL", bwd.getPkId());
            }
            List<BdWithdrawalAuto> listAuto = iBdWithdrawalAutoService.list(queryWrapperAuto);
            List<BdWithdrawalAutoExt> watExtList = new ArrayList<>();
            listAuto.forEach(item -> watExtList.add(BeanUtil.copyProperties(item, BdWithdrawalAutoExt.class)));
            withdrawalExtNew.setWithdrawalAutoList(watExtList);
            listExt.add(withdrawalExtNew);
        }
        return getDataTable(listExt);
    }

    /**
     * 新增提现设置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody @Valid BdWithdrawalExt withdrawalExt) {
        if (StringUtils.isNull(withdrawalExt.getMinAmount())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MIN_AMOUNT_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalExt.getMaxAmount())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MAX_AMOUNT_NOT_NULL));
        }
        if (withdrawalExt.getMaxAmount().compareTo(withdrawalExt.getMinAmount()) < 0) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.COMPARISON_OF_SIZE));
        }
        if (StringUtils.isNull(withdrawalExt.getIsAuto())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_AUTO_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalExt.getIsRounding())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_ROUNDING_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalExt.getWithdrawalNumber())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WITHDRAWAL_NUMBER_NOT_NULL));
        }
        if (EYesNo.YES.getIntValue() == withdrawalExt.getIsAuto()) {
            if (StringUtils.isNull(withdrawalExt.getLowerBound())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LOWER_BOUND_NOT_NULL));
            }
            if (StringUtils.isNull(withdrawalExt.getUpperBound())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.UPPER_BOUND_NOT_NULL));
            }
            if (CollectionUtil.isEmpty(withdrawalExt.getWithdrawalAutoList())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WITHDRAWAL_CYCLE_NOT_NULL));
            }
        }
        if (StringUtils.isNull(withdrawalExt.getEffectiveDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        withdrawalExt.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalExt.setPkCreator(SecurityUtils.getUserId());
        withdrawalExt.setCreationTime(new Date());
        return toAjax(iBdWithdrawalService.saveWithdrawal(withdrawalExt));
    }

    /**
     * 修改提现设置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdWithdrawalExt withdrawalExt) {
        if (StringUtils.isNull(withdrawalExt.getMinAmount())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MIN_AMOUNT_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalExt.getMaxAmount())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MAX_AMOUNT_NOT_NULL));
        }
        if (withdrawalExt.getMaxAmount().compareTo(withdrawalExt.getMinAmount()) < 0) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.COMPARISON_OF_SIZE));
        }
        if (StringUtils.isNull(withdrawalExt.getIsAuto())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_AUTO_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalExt.getIsRounding())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_ROUNDING_NOT_NULL));
        }
        if (StringUtils.isNull(withdrawalExt.getWithdrawalNumber())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WITHDRAWAL_NUMBER_NOT_NULL));
        }

        if (StringUtils.isNull(withdrawalExt.getEffectiveDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        if (EYesNo.YES.getIntValue() == withdrawalExt.getIsAuto()) {
            if (StringUtils.isNull(withdrawalExt.getLowerBound())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LOWER_BOUND_NOT_NULL));
            }
            if (StringUtils.isNull(withdrawalExt.getUpperBound())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.UPPER_BOUND_NOT_NULL));
            }
            if (CollectionUtil.isEmpty(withdrawalExt.getWithdrawalAutoList())) {
                return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WITHDRAWAL_CYCLE_NOT_NULL));
            }
        }
        if (StringUtils.isNull(withdrawalExt.getEffectiveDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        withdrawalExt.setPkModified(SecurityUtils.getUserId());
        withdrawalExt.setModifiedTime(new Date());
        return toAjax(iBdWithdrawalService.updateWithdrawal(withdrawalExt));
    }

    /**
     * 查询单条提现设置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Integer pkId) {
        BdWithdrawal withdrawalParam = iBdWithdrawalService.getById(pkId);
        BdWithdrawalExt withdrawalExt = BeanUtil.copyProperties(withdrawalParam, BdWithdrawalExt.class);
        BdWithdrawalAutoExt withdrawalAutoExt = new BdWithdrawalAutoExt();
        withdrawalAutoExt.setPkWithdrawal(pkId);
        List<BdWithdrawalAutoExt> withdrawalAutoList = iBdWithdrawalService.getWithdrawalAutoList(withdrawalAutoExt);
        if (CollectionUtil.isNotEmpty(withdrawalAutoList)) {
            withdrawalExt.setWithdrawalAutoList(withdrawalAutoList);
        } else {
            List<BdWithdrawalAutoExt> withdrawalAutoListNew = new ArrayList<>();
            withdrawalExt.setWithdrawalAutoList(withdrawalAutoListNew);
        }
        return AjaxResult.success(withdrawalExt);
    }

    /**
     * 删除提现设置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        BdWithdrawalAutoExt withdrawalAutoExt = new BdWithdrawalAutoExt();
        withdrawalAutoExt.setPkCountry(SecurityUtils.getPkCountry());
        Integer count = iBdWithdrawalService.checkWithdraw(withdrawalAutoExt);
        if (count > 0) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ENABLE_STATE_NOT_DELETE));
        }
        UpdateWrapper<BdWithdrawal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(iBdWithdrawalService.update(updateWrapper));
    }

}
