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.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.TransactionUtils;
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.BdTransfer;
import com.hzs.common.domain.system.config.ext.BdTransferExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdCurrencyService;
import com.hzs.system.config.service.IBdTransferService;
import lombok.extern.slf4j.Slf4j;
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-07
 */
@Slf4j
@RestController
@RequestMapping("/manage/transfer")
public class BdTransferController  extends BaseController {

    @Autowired
    private IBdTransferService transferService;
    private RedisService redisService;
    @Autowired
    IBdCurrencyService currencyService;

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

    /**
     * @description: 查询转账配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [transfer]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.TRANSFER_CONFIG,method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdTransfer transfer) {
        startPage();
        QueryWrapper<BdTransfer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(transfer.getIsRounding())){
            queryWrapper.eq("IS_ROUNDING",transfer.getIsRounding());
        }
        if(StringUtils.isNotNull(transfer.getIsReference())){
            queryWrapper.eq("IS_REFERENCE",transfer.getIsReference());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdTransfer> list = transferService.list(queryWrapper);
        List<BdTransferExt> listExt=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            for(BdTransfer bt:list){
                BdTransferExt bdTransferExt = BeanUtil.copyProperties(bt, BdTransferExt.class);
                if(StringUtils.isNotNull(bt.getMinAmount())){
                    bdTransferExt.setMinAmountUsa(currencyService.getDollar(bt.getMinAmount(),SecurityUtils.getPkCountry()));
                }
                if(StringUtils.isNotNull(bt.getMaxAmount())){
                    bdTransferExt.setMaxAmountUsa(currencyService.getDollar(bt.getMaxAmount(),SecurityUtils.getPkCountry()));
                }
                listExt.add(bdTransferExt);
            }
        }
        return getDataTable(listExt);
    }

    /**
     * @description: 导出转账配置
     * @author: zhang jing
     * @date: 2023/3/3 16:02
     * @param: [response, transfer]
     * @return: void
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.TRANSFER_CONFIG,method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdTransfer transfer) {
        QueryWrapper<BdTransfer> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(transfer.getIsRounding())){
            queryWrapper.eq("IS_ROUNDING",transfer.getIsRounding());
        }
        if(StringUtils.isNotNull(transfer.getIsReference())){
            queryWrapper.eq("IS_REFERENCE",transfer.getIsReference());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdTransfer> list = transferService.list(queryWrapper);
        List<BdTransferExt> listExt=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            for(BdTransfer bt:list){
                BdTransferExt bdTransferExt = BeanUtil.copyProperties(bt, BdTransferExt.class);
                if(StringUtils.isNotNull(bt.getMinAmount())){
                    bdTransferExt.setMinAmountUsa(currencyService.getDollar(bt.getMinAmount(),SecurityUtils.getPkCountry()));
                }
                if(StringUtils.isNotNull(bt.getMaxAmount())){
                    bdTransferExt.setMaxAmountUsa(currencyService.getDollar(bt.getMaxAmount(),SecurityUtils.getPkCountry()));
                }
                listExt.add(bdTransferExt);
            }
        }
        ExcelUtil<BdTransferExt> util = new ExcelUtil<>(BdTransferExt.class);
        util.exportExcel(response, listExt, "币种配置");
    }


    /**
     * @description: 新增转账配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [transfer]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.TRANSFER_CONFIG,method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdTransfer transfer) {
        if(StringUtils.isNull(transfer.getMinAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MIN_AMOUNT_TRANSFER_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getMaxAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MAX_AMOUNT_TRANSFER_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getIsRounding())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_ROUNDING_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getIsReference())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_TRANSFER_IS_REFERENCE_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getMultiple())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MULTIPLE_NOT_NULL));
        }

        if(StringUtils.isNull(transfer.getServiceType())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_TYPE_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getServiceRatio())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_RATIO_NOT_NULL));
        }

        if(StringUtils.isNull(transfer.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }

        transfer.setPkCountry(SecurityUtils.getPkCountry());
        transfer.setPkCreator(SecurityUtils.getUserId());
        transfer.setCreationTime(new Date());
        redisService.deleteObject(CacheConstants.BD_TRANSFER + SecurityUtils.getPkCountry());
        return toAjax(transferService.save(transfer));
    }


    /**
     * @description: 修改转账配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [transfer]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.WALLET_CONFIG,business = EOperationBusiness.TRANSFER_CONFIG,method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdTransfer transfer) {
        if(StringUtils.isNull(transfer.getMinAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MIN_AMOUNT_TRANSFER_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getMaxAmount())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MAX_AMOUNT_TRANSFER_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getIsRounding())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_ROUNDING_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getIsReference())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_TRANSFER_IS_REFERENCE_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getMultiple())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MULTIPLE_NOT_NULL));
        }

        if(StringUtils.isNull(transfer.getServiceType())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_TYPE_NOT_NULL));
        }
        if(StringUtils.isNull(transfer.getServiceRatio())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SERVICE_RATIO_NOT_NULL));
        }

        if(StringUtils.isNull(transfer.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        transfer.setPkModified(SecurityUtils.getUserId());
        transfer.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_TRANSFER + SecurityUtils.getPkCountry());
        return toAjax(transferService.updateById(transfer));
    }


    /**
     * @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.TRANSFER_CONFIG,method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdTransfer transfer = transferService.getById(pkId);
        return AjaxResult.success(transfer);
    }


    /**
     * @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.TRANSFER_CONFIG,method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        BdTransfer transfer =new BdTransfer();
        transfer.setPkCountry(SecurityUtils.getPkCountry());
        Integer count= transferService.checkTransfer(transfer);
        if(count>0){
            return  AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ENABLE_STATE_NOT_DELETE));
        }
        UpdateWrapper<BdTransfer> 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_TRANSFER + SecurityUtils.getPkCountry());
        return AjaxResult.success(transferService.update(updateWrapper));
    }
}
