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

import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.EApprovalBusiness;
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.ext.CuMemberExt;
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.vo.CuMemberAuthenticationVO;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.self.service.ICuSelfServiceLogService;
import com.hzs.member.self.vo.DomicileChangeVO;
import com.hzs.member.sms.service.IApiAliSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 变理户籍
 */
@Slf4j
@RequestMapping("/api/change-domicile")
@RestController
public class ApiDomicileChangeController extends BaseController {

    @Autowired
    private ICuMemberAuthenticationService iCuMemberAuthenticationService;
    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuSelfServiceLogService iCuSelfServiceLogService;
    @Autowired
    private IApiAliSmsService iApiAliSmsService;

    /**
     * 查询可申请信息
     *
     * @return AjaxResult
     */
    @GetMapping("/application-info-list")
    public AjaxResult applicationInformation() {
        Long pkMember = SecurityUtils.getUserId();
        CuMemberExt member = iCuMemberService.getMemberById(pkMember);
        DomicileChangeVO domicileChangeVO = DomicileChangeVO.builder().memberCode(member.getMemberCode())
                .memberName(member.getMemberName()).phone(member.getPhone())
                .changeNum(MagicNumberConstants.CHANGE_NUM).build();
        return AjaxResult.success(domicileChangeVO);
    }


    @GetMapping("/application-info-detail")
    public AjaxResult applicationInformationDetail() {
        return AjaxResult.success(iCuSelfServiceLogService.selectDomicileChangeByPkMember(SecurityUtils.getUserId(), EApprovalBusiness.DOMICILE_CHANGE.getValue(), SecurityUtils.getPkCountry()));
    }

    /**
     * 申请户籍变更
     *
     * @param domicileChangeVO 变更信息
     * @return TableDataInfo
     */
    @PostMapping("/change-domicile")
    @RepeatSubmitSimple
    public AjaxResult changeDomicile(@RequestBody DomicileChangeVO domicileChangeVO) {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(domicileChangeVO.getChangePhone())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_PHONE_CANNOT_EMPTY));
        }
        if (StringUtils.isEmpty(domicileChangeVO.getChangeMemberName())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.MEMBER_NAME_CANNOT_EMPTY));
        }
        if (StringUtils.isEmpty(domicileChangeVO.getIdFront())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.ID_FRONT_CANNOT_EMPTY));
        }
        if (StringUtils.isEmpty(domicileChangeVO.getIdBack())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.ID_BACK_CANNOT_EMPTY));
        }
        if (StringUtils.isEmpty(domicileChangeVO.getChangeIdFront())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.ID_FRONT_CANNOT_EMPTY));
        }
        if (StringUtils.isEmpty(domicileChangeVO.getChangeIdBack())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.ID_BACK_CANNOT_EMPTY));
        }
        domicileChangeVO.setPkCountry(pkCountry);
        domicileChangeVO.setPkMember(pkMember);
        // 验证码校验
        String message = iApiAliSmsService.checkCode(null, domicileChangeVO.getVerificationCode(), null, pkMember);
        if (null != message) {
            return AjaxResult.error(message);
        }
        return toAjax(iCuSelfServiceLogService.saveChangeDomicile(domicileChangeVO));
    }

    /**
     * 显示户籍变更信息
     *
     * @return AjaxResult
     */
    @GetMapping("/show-change-domicile")
    public AjaxResult showChangeDomicile() {
        String idBack = null;
        String idFront = null;
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        CuMemberExt member = iCuMemberService.getMemberById(pkMember);
        //查询实名认证信息原证件图
        CuMemberAuthenticationVO authenticationVO = CuMemberAuthenticationVO.builder().pkCountry(pkCountry).pkMember(pkMember).build();
        CuMemberAuthenticationVO cuMemberAuthenticationVO = iCuMemberAuthenticationService.selectCuMemberAuthenticationByPkMember(authenticationVO);
        if (null != cuMemberAuthenticationVO) {
            idBack = cuMemberAuthenticationVO.getIdBack();
            idFront = cuMemberAuthenticationVO.getIdFront();
        }
        DomicileChangeVO domicileChangeVO = DomicileChangeVO.builder().memberCode(member.getMemberCode())
                .memberName(member.getMemberName()).phone(member.getPhone())
                .idBack(idBack).idFront(idFront).build();
        return AjaxResult.success(domicileChangeVO);
    }

    /**
     * 查询已申请信息
     *
     * @return AjaxResult
     */
    @GetMapping("/applied-info-list")
    public AjaxResult appliedInformation() {
        DomicileChangeVO domicileChangeVO = DomicileChangeVO.builder().pkMember(SecurityUtils.getUserId())
                .pkCountry(SecurityUtils.getPkCountry()).editType(EApprovalBusiness.DOMICILE_CHANGE.getValue()).build();
        List<DomicileChangeVO> domicileChangeVOList = (List<DomicileChangeVO>) iCuSelfServiceLogService.selectSelfServiceByPkMember(domicileChangeVO)[1];
        return AjaxResult.success(domicileChangeVOList);
    }

}
