package com.hzs.member.account.controller.api;


import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.account.CuMemberRechargeMatch;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.service.ICuMemberRechargeMatchService;
import com.hzs.member.account.vo.CuMemberRechargeMatchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * <p>
 * 充值匹配 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2024-04-17
 */
@RestController
@RequestMapping("/api/recharge/match")
public class ApiCuMemberRechargeMatchController extends BaseController {

    @Autowired
    private ICuMemberRechargeMatchService memberRechargeMatchService;


    /**
     * 查询列表
     *
     * @param rechargeMatchVO 入参
     * @return TableDataInfo
     */
    @PostMapping("/list")
    public AjaxResult findRechargeMatchList(@RequestBody CuMemberRechargeMatchVO rechargeMatchVO) {
        //汇款人
        String remitter = rechargeMatchVO.getRemitter();
        //汇款金额
        BigDecimal remittanceAmount = rechargeMatchVO.getRemittanceAmount();
        //汇款开始时间
        String startDate = rechargeMatchVO.getStartDate();
        if (StringUtils.isEmpty(remitter) || null == remittanceAmount || StringUtils.isEmpty(startDate) || null == rechargeMatchVO.getVoucherNumber()) {
            return AjaxResult.success(new ArrayList<>());
        }
        rechargeMatchVO.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(memberRechargeMatchService.findRechargeMatchMemberList(rechargeMatchVO));
    }


    /**
     * 会员确认充值
     *
     * @param pkId ID
     * @return AjaxResult
     */
    @GetMapping("/confirm")
    @RepeatSubmitSimple
    public AjaxResult memberConfirm(Long pkId) {
        if (null == pkId) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.PARAMETER_NOT_EMPTY));
        }
        CuMemberRechargeMatch cuMemberRechargeMatch = memberRechargeMatchService.getById(pkId);
        if (null == cuMemberRechargeMatch) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_IMPOR_CODE_ERROR));
        }
        //导入的会员ID
        Long importPkMember = cuMemberRechargeMatch.getPkMember();
        //登录会员
        Long pkMember = SecurityUtils.getUserId();
        if (null != importPkMember && !importPkMember.equals(pkMember)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.REMITTER_ERROR));
        }
        if (null == importPkMember) {
            cuMemberRechargeMatch.setPkMember(pkMember);
        }
        // TODO: 2024/4/23  国家暂时判断一下，后继根据需求修改（影响充值币种）
        //导入的国家ID
        Integer importPkCountry = cuMemberRechargeMatch.getPkCountry();
        //登录会员国家
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (!importPkCountry.equals(pkCountry)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.REMITTER_ERROR));
        }
        return toAjax(memberRechargeMatchService.memberConfirmRecharge(pkMember, cuMemberRechargeMatch));
    }
}
