package com.yh.saas.plugin.servicer.workflow;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.yh.saas.common.support.workflow.AbstractWorkflowTaskListener;
import com.yh.saas.common.support.workflow.annotation.WorkflowTaskListener;
import com.yh.saas.plugin.servicer.entity.ServicerMerchantInfo;
import com.yh.saas.plugin.servicer.enums.ServicerStatusEnum;
import com.yh.saas.plugin.servicer.service.IServicerMerchantInfoService;
import com.yh.saas.plugin.supply.servicer.entity.SupplyServicerMerchantInfo;
import com.yh.saas.plugin.supply.servicer.service.ISupplyServicerMerchantInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@WorkflowTaskListener(name = "新增服务商", group = "服务商管理")
public class ServicerMerchantInfoListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IServicerMerchantInfoService merhantInfoService;

    @SofaReference
    private ISupplyServicerMerchantInfoService supplyMerchantInfoService;

    @Override
    public void handleStatusChanged(String businessKey, String status, String statusEn, boolean processIsEnd) {
        ServicerMerchantInfo merchantInfo = merhantInfoService.selectById(businessKey);
        if (merchantInfo == null) {
            return;
        }
        if (processIsEnd) {
            merchantInfo.setStatusFlag(ServicerStatusEnum.COMPLETED.getFlag());
            merchantInfo.setStatusName(ServicerStatusEnum.COMPLETED.getName());
            merchantInfo.setStatusEnName(ServicerStatusEnum.COMPLETED.getNameEn());
            merchantInfo.setApproveStatus(null);
            merchantInfo.setApproveStatusEn(null);
            merchantInfo.setSpareWorkFlowId(null);
            merchantInfo.setWorkFlowId(null);
            merchantInfo.setPassFlag(1);
        } else {
            merchantInfo.setApproveStatus(status);
            merchantInfo.setApproveStatusEn(statusEn);
        }
        merhantInfoService.updateAllColumnById(merchantInfo);
        SupplyServicerMerchantInfo supplyServicerMerchantInfo = new SupplyServicerMerchantInfo();
        BeanUtils.copyProperties(merchantInfo, supplyServicerMerchantInfo);
        supplyMerchantInfoService.updateAllColumnById(supplyServicerMerchantInfo);
    }

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        ServicerMerchantInfo merchantInfo = merhantInfoService.selectById(businessKey);
        if (merchantInfo == null) {
            return;
        }
        merchantInfo.setStatusFlag(ServicerStatusEnum.REFUSED.getFlag());
        merchantInfo.setStatusName(ServicerStatusEnum.REFUSED.getName());
        merchantInfo.setStatusEnName(ServicerStatusEnum.REFUSED.getNameEn());
        merchantInfo.setApproveStatus(null);
        merchantInfo.setApproveStatusEn(null);
        merhantInfoService.updateAllColumnById(merchantInfo);

        SupplyServicerMerchantInfo supplyServicerMerchantInfo = new SupplyServicerMerchantInfo();
        BeanUtils.copyProperties(merchantInfo, supplyServicerMerchantInfo);
        supplyMerchantInfoService.updateAllColumnById(supplyServicerMerchantInfo);
    }

}
