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


import cn.hutool.core.bean.BeanUtil;
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.CacheConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
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.BdWithdrawalProcess;
import com.hzs.common.domain.system.config.ext.BdWithdrawalAutoExt;
import com.hzs.common.domain.system.config.ext.BdWithdrawalProcessExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.config.IWithdrawalServiceApi;
import com.hzs.system.config.service.IBdWithdrawalProcessService;
import com.hzs.system.config.service.IBdWithdrawalService;
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 javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 提现手续费手续费配置 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/manage/bd-withdrawal-process")
public class BdWithdrawalProcessController extends BaseController {

    @Autowired
    private IBdWithdrawalProcessService withdrawalProcessService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;
    private RedisService redisService;
    @DubboReference
    private IWithdrawalServiceApi withdrawalProcessServiceApi;
    @Autowired
    private IBdWithdrawalService withdrawalService;
    @Autowired
    IBdCurrencyService currencyService;


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


    /**
     * @description: 查询提现手续费设置列表 理论每个国家只有一个提现手续费配置
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [withdrawalParam]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE,method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdWithdrawalProcess withdrawalProcess) {
        startPage();
        QueryWrapper<BdWithdrawalProcess> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawalProcess> list = withdrawalProcessService.list(queryWrapper);
        List<BdWithdrawalProcessExt> listExt=new ArrayList<>();
        list.forEach(item -> listExt.add(BeanUtil.copyProperties(item, BdWithdrawalProcessExt.class)));
        return getDataTable(listExt);
    }

    /**
     * @description: 导出提现手续费配置
     * @author: zhang jing
     * @date: 2023/3/3 17:07
     * @param: [response, withdrawalProcess]
     * @return: void
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE,method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdWithdrawalProcess withdrawalProcess) {
        QueryWrapper<BdWithdrawalProcess> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawalProcess> list = withdrawalProcessService.list(queryWrapper);
        List<BdWithdrawalProcessExt> listExt=new ArrayList<>();
        list.forEach(item -> listExt.add(BeanUtil.copyProperties(item, BdWithdrawalProcessExt.class)));
        ExcelUtil<BdWithdrawalProcessExt> util = new ExcelUtil<>(BdWithdrawalProcessExt.class);
        util.exportExcel(response, listExt, "提现手续费配置");
    }

    /**
     * @description: 新增提现手续费设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [withdrawalParam]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE,method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody @Valid BdWithdrawalProcess withdrawalProcess) {
        if(StringUtils.isNull(withdrawalProcess.getServiceType())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_TYPE_NOT_NULL));
        }
        if(StringUtils.isNull(withdrawalProcess.getServiceRatio())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_RATIO_NOT_NULL));
        }
        if(StringUtils.isNull(withdrawalProcess.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        QueryWrapper<BdWithdrawalProcess> queryWrapper=new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        BdWithdrawalProcess withdrawalProcessCheck=withdrawalProcessService.getOne(queryWrapper);
        if(StringUtils.isNotNull(withdrawalProcessCheck)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_TYPE_REPEAT));
        }
        withdrawalProcess.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalProcess.setPkCreator(SecurityUtils.getUserId());
        withdrawalProcess.setCreationTime(new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_PROCESS + SecurityUtils.getPkCountry());
        return toAjax(withdrawalProcessService.save(withdrawalProcess));
    }


    /**
     * @description: 修改提现手续费设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [withdrawalParam]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE,method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdWithdrawalProcess withdrawalProcess) {
        if(StringUtils.isNull(withdrawalProcess.getServiceType())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_TYPE_NOT_NULL));
        }
        if(StringUtils.isNull(withdrawalProcess.getServiceRatio())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_RATIO_NOT_NULL));
        }
        if(StringUtils.isNull(withdrawalProcess.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        withdrawalProcess.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalProcess.setPkModified(SecurityUtils.getUserId());
        withdrawalProcess.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_PROCESS + SecurityUtils.getPkCountry());
        return toAjax(withdrawalProcessService.updateById(withdrawalProcess));
    }


    /**
     * @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_SERVICE_CHARGE,method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Integer pkId) {
        BdWithdrawalProcess withdrawalProcess = withdrawalProcessService.getById(pkId);
        BdWithdrawalProcessExt withdrawalProcessExt = BeanUtil.copyProperties(withdrawalProcess, BdWithdrawalProcessExt.class);
        return AjaxResult.success(withdrawalProcessExt);
    }


    /**
     * @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_SERVICE_CHARGE,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));
        }
        UpdateWrapper<BdWithdrawalProcess> 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());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL + SecurityUtils.getPkCountry());
        return toAjax(withdrawalProcessService.update(updateWrapper));
    }

}
