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.DueDiligenceAllocation;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.UserDO;
import com.sunxung.factoring.mapper.supplier.DueDiligenceAllocationMapper;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.supplier.IDueDiligenceAllocationService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.CUserService;
import com.sunxung.factoring.service.sys.UserService;
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-04
 */
@Service("due_diligence_allocationService")
public class DueDiligenceAllocationServiceImpl extends ServiceImpl<DueDiligenceAllocationMapper, DueDiligenceAllocation> implements IDueDiligenceAllocationService {

    @Autowired
    private FlowableService flowableService;

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

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private UserService userService;

    @Autowired
    private CUserService cUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addInfo(DueDiligenceAllocation dueDiligenceAllocation,SupplierInfo supplierInfo) {
        save(dueDiligenceAllocation);
        String taskId = dueDiligenceAllocation.getTaskId();
        cUserService.lambdaUpdate().eq(UserDO::getId,supplierInfo.getSysUserId()).set(UserDO::getRiskManager,dueDiligenceAllocation.getRiskManagerId()).update();
        Enterprise enterprise = supplierInfo.getEnterprise();
        Map<String, Object> procVariables = new HashMap<>();
        procVariables.put(ConstantUtil.FIRST_REVIEW_ASSIGNEE,dueDiligenceAllocation.getRiskManagerId());
        procVariables.put(ConstantUtil.SECOND_REVIEW_ASSIGNEE,dueDiligenceAllocation.getRiskManagerId());
        procVariables.put(ConstantUtil.DUE_DILIGENCE_CONFIRM_ASSIGNEE,dueDiligenceAllocation.getRiskManagerId());
        procVariables.put(ConstantUtil.VIDEO_DUE_DILIGENCE_ASSIGNEE,dueDiligenceAllocation.getRiskManagerId());
        User admin = userService.getByLoginName("admin");
        procVariables.put(ConstantUtil.DUE_DILIGENCE_RESERVATION_ASSIGNEE,admin.getId());
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "供应商名称：" + enterprise.getCorporateName());
        String status;
        if(supplierInfo.getConstructionIndustry().equals(0)){
            procVariables.put("result","非建工");
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "供应商名称为“" + enterprise.getCorporateName() + "”的供应商已进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.SUPPLIER_INFO) + "—供应商尽调时间确认，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            status = SupplierStatusEnums.DUE_DILIGENCE_APPOINTMENT.getCode();
        }else {
            procVariables.put("result","建工");
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "供应商名称为“" + enterprise.getCorporateName() + "”的供应商已进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.SUPPLIER_INFO) + "—供应商审核第一步，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            status = SupplierStatusEnums.DUE_DILIGENCE_REVIEW.getCode();
        }
        flowableService.completeMyTask(taskId, supplierInfo, procVariables, null, dueDiligenceAllocation.getId(), msgVariables, ConstantUtil.SUPPLIER_INFO);
        return status;
    }

    @Override
    public DueDiligenceAllocation getView(Long id) {
        DueDiligenceAllocation dueDiligenceAllocation = getById(id);
        User user = userService.get(dueDiligenceAllocation.getRiskManagerId());
        dueDiligenceAllocation.setRiskManager(user);
        return dueDiligenceAllocation;
    }

    @Override
    public DueDiligenceAllocation getDueDiligenceAllocation(Long id) {
        SupplierInfo supplierInfo = supplierInfoService.get(id);
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        DueDiligenceAllocation dueDiligenceAllocation = new DueDiligenceAllocation();
        dueDiligenceAllocation.setName(enterprise.getCorporateName());
        dueDiligenceAllocation.setSocialCreditCode(enterprise.getSocialCreditCode());
        dueDiligenceAllocation.setcSupplierInfoId(id);
        return dueDiligenceAllocation;
    }
}
