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

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.core.enums.EVerificationModule;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.account.CuMemberBusinessLicense;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.system.config.BdTradeConfig;
import com.hzs.common.domain.system.config.BdTradeWhiteConfig;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.account.service.ICuMemberAuthenticationService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberBusinessLicenseService;
import com.hzs.member.account.vo.CuMemberAuthenticationVO;
import com.hzs.member.account.vo.CuMemberBusinessLicenseVO;
import com.hzs.member.base.service.ICuMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 会员营业执照 前端控制器
 */
@RestController
@RequestMapping("/api/business/license")
public class ApiCuMemberBusinessLicenseController extends BaseController {

    @Autowired
    private ICuMemberBusinessLicenseService cuMemberBusinessLicenseService;
    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuMemberBaseService iCuMemberBaseService;
    @Autowired
    private ICuMemberAuthenticationService authenticationService;


    /**
     * 保存营业执照
     *
     * @param businessLicense 营业执照入参
     * @return AjaxResult
     */
    @PostMapping("/add")
    @RepeatSubmitSimple
    public AjaxResult saveBusinessLicense(@RequestBody CuMemberBusinessLicenseVO businessLicense) {
        //参数校验
        if (VerifyParameters(businessLicense).equals(AjaxResult.success())) {
            Long pkMember = SecurityUtils.getUserId();
            Integer pkCountry = SecurityUtils.getPkCountry();
            //查询是否已实名认证
            CuMemberAuthenticationVO authenticationVO = authenticationService.selectCuMemberAuthenticationByPkMember(CuMemberAuthenticationVO.builder().pkMember(pkMember).pkCountry(pkCountry).build());
            //验证实名认证的真实姓名与法人的姓名是否一致
            if (null != authenticationVO) {
                String accountName = authenticationVO.getAccountName();
                //法人姓名
                String legalName = businessLicense.getLegalName();
                if (!accountName.equals(legalName)) {
                    return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.REAL_NAME_LICENSE_NOT_SAME));
                }
            }
            businessLicense.setPkMember(pkMember);
            businessLicense.setPkCountry(pkCountry);
            CuMemberBusinessLicense memberBusinessLicense = BeanUtil.copyProperties(businessLicense, CuMemberBusinessLicense.class);
            memberBusinessLicense.setApproveStatus(EApproveStatus.ALREADY_SUBMIT.getValue());
            memberBusinessLicense.setPkCreator(pkMember);
            return toAjax(cuMemberBusinessLicenseService.save(memberBusinessLicense));
        } else {
            return VerifyParameters(businessLicense);
        }

    }


    /**
     * 保存营业执照
     *
     * @param businessLicense 营业执照入参
     * @return AjaxResult
     */
    @PostMapping("/update")
    @RepeatSubmitSimple
    public AjaxResult updateBusinessLicense(@RequestBody CuMemberBusinessLicenseVO businessLicense) {
        //参数校验
        if (VerifyParameters(businessLicense).equals(AjaxResult.success())) {
            //查询是否已实名认证
            CuMemberAuthenticationVO authenticationVO = authenticationService.selectCuMemberAuthenticationByPkMember(CuMemberAuthenticationVO.builder().pkMember(SecurityUtils.getUserId()).pkCountry(SecurityUtils.getPkCountry()).build());
            //验证实名认证的真实姓名与法人的姓名是否一致
            if (null != authenticationVO) {
                String accountName = authenticationVO.getAccountName();
                //法人姓名
                String legalName = businessLicense.getLegalName();
                if (!accountName.equals(legalName)) {
                    return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.REAL_NAME_LICENSE_NOT_SAME));
                }
            }
            CuMemberBusinessLicense memberBusinessLicense = BeanUtil.copyProperties(businessLicense, CuMemberBusinessLicense.class);
            memberBusinessLicense.setModifiedTime(new Date());
            memberBusinessLicense.setPkModified(SecurityUtils.getUserId());
            memberBusinessLicense.setApproveStatus(EApproveStatus.ALREADY_SUBMIT.getValue());
            return toAjax(cuMemberBusinessLicenseService.updateById(memberBusinessLicense));
        } else {
            return VerifyParameters(businessLicense);
        }

    }


    /**
     * 查看详情
     *
     * @return AjaxResult
     */
    @GetMapping("/detail")
    public AjaxResult showDetail() {
        return AjaxResult.success(cuMemberBusinessLicenseService.selectBusinessLicenseById(SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }


    /**
     * 是否上传营业执照
     *
     * @param businessModule 1:提现 2:转账 (来源枚举EBusinessModule)
     * @return AjaxResult
     */
    @GetMapping("/is-exist")
    public AjaxResult isExistBusinessLicence(Integer businessModule) {
        String flag = EYesNo.YES.getValue();
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        //查询配置白名单
        BdTradeWhiteConfig bdTradeWhiteConfig = iCuMemberBaseService.selectConfigWhite(pkMember, pkCountry, businessModule);
        if (null == bdTradeWhiteConfig) {
            //查询是否配置营业执照
            BdTradeConfig bdTradeConfig = iCuMemberBaseService.selectConfigTrade(pkMember, pkCountry, businessModule, EVerificationModule.BUSINESS_LICENSE);
            //配置后才进行验证
            if (null != bdTradeConfig) {
                //查询是否完成经销商认证
                CuMember cuMember = iCuMemberService.queryMember(pkMember);
                flag = null != cuMember && cuMember.getIsDealer().equals(EYesNo.YES.getIntValue()) ? EYesNo.YES.getValue() : EYesNo.NO.getValue();
            }
        }
        return AjaxResult.success().put("flag", flag);
    }


    /**
     * 校验参数
     *
     * @return AjaxResult
     */
    private AjaxResult VerifyParameters(CuMemberBusinessLicenseVO businessLicense) {
        if (StringUtils.isEmpty(businessLicense.getLicenseName())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_BUSINESS_LICENSE_NAME));
        }
        if (StringUtils.isEmpty(businessLicense.getCreditCode())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_BUSINESS_CREDIT_CODE));
        }
        if (StringUtils.isEmpty(businessLicense.getLegalName())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_BUSINESS_LEGAL_NAME));
        }
        if (StringUtils.isEmpty(businessLicense.getLicenseImage())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_BUSINESS_LICENSE_IMAGE));
        }
        return AjaxResult.success();
    }


}
