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


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.HttpStatus;
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.member.base.CuMember;
import com.hzs.common.domain.system.config.CuMemberTransferWhite;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.service.ICuMemberTransferWhiteService;
import com.hzs.system.config.vo.CuMemberTransferWhiteVO;
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.List;
import java.util.Map;

/**
 * <p>
 * 会员转账币种白名单  前端控制器
 * </p>
 *
 * @author hzs
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/manage/transfer-white")
public class CuMemberTransferWhiteController extends BaseController {
    @DubboReference
    private IMemberServiceApi iMemberServiceApi;


    @Autowired
    private ICuMemberTransferWhiteService transferWhiteService;


    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 保存转账币种白名单
     *
     * @param cuMemberTransferWhiteVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.TRANSFER_ACCOUNT_WHITE, method = EOperationMethod.INSERT)
    @PostMapping("/add")
    public AjaxResult addTransferWhiteConfig(@RequestBody CuMemberTransferWhiteVO cuMemberTransferWhiteVO) {
        String memberCode = cuMemberTransferWhiteVO.getMemberCode();
        CuMember cumember = iMemberServiceApi.getMember(memberCode).getData();

        AjaxResult ajaxResult = parametersVerify(cuMemberTransferWhiteVO, cumember);
        if ((Integer) ajaxResult.get(AjaxResult.CODE_TAG) == HttpStatus.ERROR) {
            return ajaxResult;
        }
        cuMemberTransferWhiteVO.setPkCreator(SecurityUtils.getUserId());
        cuMemberTransferWhiteVO.setPkMember(cumember.getPkId());
        cuMemberTransferWhiteVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(transferWhiteService.addTransferWhiteConfig(cuMemberTransferWhiteVO));
    }


    /**
     * 转账配置白名单列表显示
     *
     * @param transferWhiteVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.TRANSFER_ACCOUNT_WHITE, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo showTransferConfigList(@RequestBody CuMemberTransferWhiteVO transferWhiteVO) {
        transferWhiteVO.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        Object[] objects = transferWhiteService.selectTransferWhiteConfigList(transferWhiteVO);
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberTransferWhite>) objects[0]);
        tableDataInfo.setRows((List<CuMemberTransferWhiteVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 白名单导出
     *
     * @param response        响应
     * @param transferWhiteVO 查询入参
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.TRANSFER_ACCOUNT_WHITE, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void transferWhiteExport(HttpServletResponse response, CuMemberTransferWhiteVO transferWhiteVO) {
        transferWhiteVO.setPkCountry(SecurityUtils.getPkCountry());
        List<CuMemberTransferWhiteVO> memberTransferWhiteVOList = (List<CuMemberTransferWhiteVO>) transferWhiteService.selectTransferWhiteConfigList(transferWhiteVO)[1];
        //多语言翻译
        if (CollUtil.isNotEmpty(memberTransferWhiteVOList)) {
            //枚举翻译
            Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_ACC);
            Map<Integer, String> transactionMapAwards = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
            for (CuMemberTransferWhiteVO configVO : memberTransferWhiteVOList) {
                configVO.setPkAwardsVal(transactionMapAwards.get(configVO.getPkAwards()));
                configVO.setPkAccountVal(transactionMap.get(configVO.getPkAccount()));
            }
        }
        ExcelUtil<CuMemberTransferWhiteVO> util = new ExcelUtil<>(CuMemberTransferWhiteVO.class);
        util.exportExcel(response, memberTransferWhiteVOList, "转账账户白名单导出");
    }


    /**
     * 更新转账配置白名单信息
     *
     * @param transferWhiteVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.TRANSFER_ACCOUNT_WHITE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateTransferConfig(@RequestBody CuMemberTransferWhiteVO transferWhiteVO) {
        CuMember cumember = iMemberServiceApi.queryMember(transferWhiteVO.getMemberCode()).getData();
        AjaxResult ajaxResult = parametersVerify(transferWhiteVO, cumember);
        if ((Integer) ajaxResult.get(AjaxResult.CODE_TAG) == HttpStatus.ERROR) {
            return ajaxResult;
        }
        transferWhiteVO.setPkCreator(SecurityUtils.getUserId());
        transferWhiteVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(transferWhiteService.updateTransferWhite(transferWhiteVO));
    }

    /**
     * 删除交易白名单配置
     *
     * @param pkIds 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.TRANSFER_ACCOUNT_WHITE, method = EOperationMethod.DELETE)
    @GetMapping("/delete")
    public AjaxResult deleteTransferConfig(String pkIds) {
        if (StringUtils.isEmpty(pkIds)) {
            return AjaxResult.error(ConfigMsgConstants.DELETE_INFO);
        }
        return toAjax(transferWhiteService.deleteTransferWhiteConfig(pkIds, SecurityUtils.getUserId()));
    }


    /**
     * 查看详情
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.TRANSFER_ACCOUNT_WHITE, method = EOperationMethod.SELECT, remark = "查看详情")
    @GetMapping("/detail")
    public AjaxResult selectTransferWhiteDetail(Long pkId) {
        return AjaxResult.success(transferWhiteService.selectTransferWhiteById(pkId));
    }


    /**
     * 公共参数校验
     *
     * @param memberTransferWhiteVO 入参
     * @return AjaxResult
     */
    private AjaxResult parametersVerify(CuMemberTransferWhiteVO memberTransferWhiteVO, CuMember cumember) {

        if (StringUtils.isEmpty(memberTransferWhiteVO.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_EMPTY));
        }

        if (null == cumember) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_EXIST));
        }

        if (null == memberTransferWhiteVO.getPkAccount()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ACCOUNT_NAME_NOT_NULL));
        }

        Integer pkAccount = memberTransferWhiteVO.getPkAccount();
        CuMemberTransferWhite cuMemberTransferWhite = transferWhiteService.selectByPkMemberAndPkAcount(cumember.getPkId(), pkAccount);
        if (null != cuMemberTransferWhite) {
            return AjaxResult.error(ConfigMsgConstants.MEMBER_EXIST);
        }
        return AjaxResult.success();
    }
}
