package com.sunxung.factoring.service.supplier.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.enums.SupplierStatusEnums;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.supplier.ConfirmationDueDiligenceTime;
import com.sunxung.factoring.entity.supplier.SupplierContact;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.mapper.supplier.ConfirmationDueDiligenceTimeMapper;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.supplier.IConfirmationDueDiligenceTimeService;
import com.sunxung.factoring.service.supplier.ISupplierContactService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 尽调时间确认 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-12-09
 */
@Service("due_diligence_confirmService")
public class ConfirmationDueDiligenceTimeServiceImpl extends ServiceImpl<ConfirmationDueDiligenceTimeMapper, ConfirmationDueDiligenceTime> implements IConfirmationDueDiligenceTimeService {

    @Resource(name = "SupplierInfoService")
    private ISupplierInfoService supplierInfoService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private ISupplierContactService supplierContactService;

    @Autowired
    private FlowableService flowableService;

    @Override
    public ConfirmationDueDiligenceTime getDueDiligenceConfirm(Long supplierId) {
        SupplierInfo supplierInfo = supplierInfoService.get(supplierId);
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        SupplierContact supplierContact = supplierContactService.lambdaQuery().eq(SupplierContact::getSupplierInfoId, supplierId).one();
        ConfirmationDueDiligenceTime confirmationDueDiligenceTime = new ConfirmationDueDiligenceTime();
        confirmationDueDiligenceTime.setContactName(supplierContact.getContactName());
        confirmationDueDiligenceTime.setcSupplierInfoId(supplierId);
        confirmationDueDiligenceTime.setMobile(supplierContact.getMobile());
        confirmationDueDiligenceTime.setEnterpriseName(enterprise.getCorporateName());
        confirmationDueDiligenceTime.setSocialCreditCode(enterprise.getSocialCreditCode());
        return confirmationDueDiligenceTime;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dueDiligenceConfirm(ConfirmationDueDiligenceTime confirmationDueDiligenceTime) {
        save(confirmationDueDiligenceTime);
        SupplierInfo supplierInfo = supplierInfoService.get(confirmationDueDiligenceTime.getcSupplierInfoId());
        SupplierContact supplierContact = supplierContactService.lambdaQuery().eq(SupplierContact::getSupplierInfoId, confirmationDueDiligenceTime.getcSupplierInfoId()).one();
        if(!supplierContact.getMobile().equals(confirmationDueDiligenceTime.getMobile())){
            supplierContact.setMobile(confirmationDueDiligenceTime.getMobile());
            supplierContact.setContactName(confirmationDueDiligenceTime.getContactName());
            supplierContactService.updateById(supplierContact);
        }
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        //完成流程
        String taskId = confirmationDueDiligenceTime.getTaskId();
        Map<String, Object> msgVariables = new HashMap<>();
        Map<String, Object> procVariables = new HashMap<>();
        String result;
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "供应商名称：" + enterprise.getCorporateName());
        if(supplierInfo.getConstructionIndustry().equals(1)){
            result = "建工";
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "供应商名称为“" + enterprise.getCorporateName() + "”的供应商已进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.SUPPLIER_INFO) + "—供应商视频尽调，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            supplierInfo.setStatusStr(SupplierStatusEnums.VIDEO_DUE_DILIGENCE.getCode());
        }else {
            result = "非建工";
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "供应商名称为“" + enterprise.getCorporateName() + "”的供应商已完成审核“"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            supplierInfo.setStatusStr(SupplierStatusEnums.WAIT_DUE_DILIGENCE.getCode());
        }
        supplierInfoService.updateById(supplierInfo);
        procVariables.put("result",result);
        flowableService.completeMyTask(taskId, supplierInfo, procVariables, null, confirmationDueDiligenceTime.getId(), msgVariables, ConstantUtil.SUPPLIER_INFO);
    }

    @Override
    public ConfirmationDueDiligenceTime getView(Long id) {
        ConfirmationDueDiligenceTime confirmationDueDiligenceTime = getById(id);
        SupplierInfo supplierInfo = supplierInfoService.get(confirmationDueDiligenceTime.getcSupplierInfoId());
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        confirmationDueDiligenceTime.setEnterpriseName(enterprise.getCorporateName());
        confirmationDueDiligenceTime.setSocialCreditCode(enterprise.getSocialCreditCode());
        return confirmationDueDiligenceTime;
    }
}
