package org.springblade.yeePay.listener;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.SystemConstant;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussiness.service.IBussinessService;
import org.springblade.modules.guosen.bussinessInfo.entity.BussinessInfo;
import org.springblade.modules.guosen.bussinessInfo.service.IBussinessInfoService;
import org.springblade.modules.guosen.bussinessRegisterRequest.entity.BussinessRegisterRequest;
import org.springblade.modules.guosen.bussinessRegisterRequest.service.IBussinessRegisterRequestService;
import org.springblade.yeePay.event.MerchantRegisterEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MerchantRegisterNotifyListener {

    @Autowired
    private IBussinessRegisterRequestService bussinessRegisterRequestService;
    @Autowired
    private IBussinessInfoService bussinessInfoService;
    @Autowired
    private IBussinessService bussinessService;

    /**
     * 商户入网申请回调
     *
     * @param merchantEvent 回调事件
     */
    @EventListener
    @Async
    public void merchantRegisterNotify(MerchantRegisterEvent merchantEvent) {
        JSONObject decryptData = merchantEvent.getDecryptData(); //获取回调信息，解密后的数据

        String requestNo = decryptData.getString("requestNo");
        BussinessRegisterRequest registerRequest = bussinessRegisterRequestService.getByRequestNo(requestNo);
        if(registerRequest == null){
            log.error("================商户进件回调，申请记录不存在=================");
            return;
        }
        BussinessInfo bussinessInfo = bussinessInfoService.getById(registerRequest.getBussinessInfoId());
        if(bussinessInfo == null){
            log.error("================商户进件回调，商户主体信息记录不存在=================");
            return;
        }
        Bussiness bussiness = bussinessService.getById(bussinessInfo.getBussinessId());
        if(bussiness == null){
            log.error("================商户进件回调，采购商信息记录不存在=================");
            return;
        }
        if(registerRequest.getApplicationStatus().intValue() == SystemConstant.BussinessRegisterStatus.SUCCESS.getValue() || registerRequest.getApplicationStatus().intValue() == SystemConstant.BussinessRegisterStatus.FAIL.getValue()) {
            return;
        }else{
            String applicationStatus = decryptData.getString("applicationStatus");
            if(applicationStatus.equals("REVIEW_BACK")){
                registerRequest.setApplicationStatus(SystemConstant.BussinessRegisterStatus.FAIL.getValue());
                bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.FAIL.getValue());
                bussiness.setRegisterStatus(SystemConstant.BussinessRegisterStatus.FAIL.getValue());
            } else if (applicationStatus.equals("AGREEMENT_SIGNING")){
                registerRequest.setApplicationStatus(SystemConstant.BussinessRegisterStatus.WAIT_SIGNING.getValue());
                bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.WAIT_SIGNING.getValue());
                bussiness.setRegisterStatus(SystemConstant.BussinessRegisterStatus.WAIT_SIGNING.getValue());
            } else if(applicationStatus.equals("AUTHENTICITY_VERIFYING")){
                registerRequest.setApplicationStatus(SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue());
                bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue());
                bussiness.setRegisterStatus(SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue());
            }else if(applicationStatus.equals("COMPLETED")){
                registerRequest.setApplicationStatus(SystemConstant.BussinessRegisterStatus.SUCCESS.getValue());
                bussinessInfo.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUCCESS.getValue());
                bussiness.setRegisterStatus(SystemConstant.BussinessRegisterStatus.SUCCESS.getValue());
            }
            registerRequest.setAuditOpinion(decryptData.getString("auditOpinion"));
            registerRequest.setApplicationNo(decryptData.getString("applicationNo"));
            registerRequest.setMerchantNo(decryptData.getString("merchantNo"));
            registerRequest.setAgreementSignUrl(decryptData.getString("agreementSignUrl"));
            bussinessInfo.setAgreementSignUrl(decryptData.getString("agreementSignUrl"));
            bussinessInfo.setAuditOpinion(decryptData.getString("auditOpinion"));
            boolean update = bussinessRegisterRequestService.updateById(registerRequest);
            if(update){
                bussinessInfo.setMerchantNo(decryptData.getString("merchantNo"));
                bussinessInfoService.updateById(bussinessInfo);
                bussiness.setMerchantNo(decryptData.getString("merchantNo"));
                bussinessService.updateById(bussiness);
            }
        }
    }
}
