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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.project.BusinessProcessingLog;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.supplier.DueDiligenceAppointment;
import com.sunxung.factoring.mapper.supplier.DueDiligenceAppointmentMapper;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.project.BusinessProcessingLogService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.supplier.IDueDiligenceAppointmentService;
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-11-25
 */
@Service("due_diligence_reservationService")
public class DueDiligenceAppointmentServiceImpl extends ServiceImpl<DueDiligenceAppointmentMapper, DueDiligenceAppointment> implements IDueDiligenceAppointmentService {

    @Autowired
    private BusinessProcessingLogService businessProcessingLogService;

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

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void completeDueDiligenceAppointment(Long id, Long historyId) {
        ThreadLocalUtil.setUser(userService.getByLoginName("admin"));
        SupplierInfo supplierInfo = supplierInfoService.get(id);
        Enterprise enterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        BusinessProcessingLog log = businessProcessingLogService.getLastByBusinessKeyBusinessTypeTaskName(id,ConstantUtil.SUPPLIER_INFO,"尽调预约");
        if(log == null){
            throw new ValidatorException(CodeUtil.FAIL,"日志信息为空！");
        }
        String taskId = log.getHistoricTaskId();
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "供应商名称：" + enterprise.getCorporateName());
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                "供应商名称为“" + enterprise.getCorporateName() + "”的供应商已进入“"
                        + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.SUPPLIER_INFO) + "—供应商尽调时间确认，"
                        + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        flowableService.completeMyTask(taskId, supplierInfo, null, null, historyId, msgVariables, ConstantUtil.SUPPLIER_INFO);
    }

    @Override
    public DueDiligenceAppointment getView(Long id) {
        return getById(id);
    }
}
