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.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.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.system.config.service.IBdWithdrawalProcessService;
import com.hzs.system.config.service.IBdWithdrawalService;
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;

/**
 * 提现手续费手续费配置 前端控制器
 */
@RestController
@RequestMapping("/manage/bd-withdrawal-process")
public class BdWithdrawalProcessController extends BaseController {

    @Autowired
    private IBdWithdrawalProcessService iBdWithdrawalProcessService;
    @Autowired
    private IBdWithdrawalService iBdWithdrawalService;

    /**
     * 查询提现手续费设置列表 理论每个国家只有一个提现手续费配置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        QueryWrapper<BdWithdrawalProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawalProcess> list = iBdWithdrawalProcessService.list(queryWrapper);
        List<BdWithdrawalProcessExt> listExt = new ArrayList<>();
        list.forEach(item -> listExt.add(BeanUtil.copyProperties(item, BdWithdrawalProcessExt.class)));
        return getDataTable(listExt);
    }

    /**
     * 导出提现手续费配置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        QueryWrapper<BdWithdrawalProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawalProcess> list = iBdWithdrawalProcessService.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, "提现手续费配置");
    }

    /**
     * 新增提现手续费设置
     **/
    @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(ConfigMsgConstants.SERVICE_TYPE_NOT_NULL);
        }
        if (StringUtils.isNull(withdrawalProcess.getServiceRatio())) {
            return AjaxResult.error(ConfigMsgConstants.SERVICE_RATIO_NOT_NULL);
        }
        if (StringUtils.isNull(withdrawalProcess.getEffectiveDate())) {
            return AjaxResult.error(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL);
        }
        QueryWrapper<BdWithdrawalProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        BdWithdrawalProcess withdrawalProcessCheck = iBdWithdrawalProcessService.getOne(queryWrapper);
        if (StringUtils.isNotNull(withdrawalProcessCheck)) {
            return AjaxResult.error(ConfigMsgConstants.SERVICE_TYPE_REPEAT);
        }
        withdrawalProcess.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalProcess.setPkCreator(SecurityUtils.getUserId());
        withdrawalProcess.setCreationTime(new Date());
        return toAjax(iBdWithdrawalProcessService.save(withdrawalProcess));
    }

    /**
     * 修改提现手续费设置
     **/
    @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(ConfigMsgConstants.SERVICE_TYPE_NOT_NULL);
        }
        if (StringUtils.isNull(withdrawalProcess.getServiceRatio())) {
            return AjaxResult.error(ConfigMsgConstants.SERVICE_RATIO_NOT_NULL);
        }
        if (StringUtils.isNull(withdrawalProcess.getEffectiveDate())) {
            return AjaxResult.error(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL);
        }
        withdrawalProcess.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalProcess.setPkModified(SecurityUtils.getUserId());
        withdrawalProcess.setModifiedTime(new Date());
        return toAjax(iBdWithdrawalProcessService.updateById(withdrawalProcess));
    }

    /**
     * 查询单条提现手续费设置
     **/
    @Log(module = EOperationModule.WALLET_CONFIG, business = EOperationBusiness.WITHDRAWAL_SERVICE_CHARGE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Integer pkId) {
        BdWithdrawalProcess withdrawalProcess = iBdWithdrawalProcessService.getById(pkId);
        BdWithdrawalProcessExt withdrawalProcessExt = BeanUtil.copyProperties(withdrawalProcess, BdWithdrawalProcessExt.class);
        return AjaxResult.success(withdrawalProcessExt);
    }

    /**
     * 删除提现手续费设置
     **/
    @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 = iBdWithdrawalService.checkWithdraw(withdrawalAutoExt);
        if (count > 0) {
            return AjaxResult.error(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());
        return toAjax(iBdWithdrawalProcessService.update(updateWrapper));
    }

}
