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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.DateUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.creditresolution.CreditResolution;
import com.sunxung.factoring.entity.creditresolution.ReconsiderAdjustApproval;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.project.ProjectInformation;
import com.sunxung.factoring.entity.supplier.SupplierContact;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.UserDO;
import com.sunxung.factoring.mapper.creditresolution.ReconsiderAdjustApprovalMapper;
import com.sunxung.factoring.service.creditresolution.ICreditResolutionService;
import com.sunxung.factoring.service.creditresolution.IReconsiderAdjustApprovalService;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.project.IProjectCoreEnterpriseService;
import com.sunxung.factoring.service.project.IProjectInformationService;
import com.sunxung.factoring.service.supplier.ISupplierContactService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.CUserService;
import com.sunxung.factoring.service.sys.DictionaryService;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

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

/**
 * <p>
 * 复议调整审批 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-12-21
 */
@Service("reconsider_adjust_approvalService")
public class ReconsiderAdjustApprovalServiceImpl extends ServiceImpl<ReconsiderAdjustApprovalMapper, ReconsiderAdjustApproval> implements IReconsiderAdjustApprovalService {

    @Resource(name = "projectInformationService")
    private IProjectInformationService projectInformationService;

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

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Resource(name = "creditResolutionService")
    private ICreditResolutionService creditResolutionService;

    @Resource(name = "projectCoreEnterpriseService")
    private IProjectCoreEnterpriseService projectCoreEnterpriseService;

    @Autowired
    private CUserService cUserService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${sop.reviewResolutionReportUrl}")
    private String reviewResolutionReportUrl;

    @Autowired
    private UserService userService;

    @Autowired
    private ISupplierContactService supplierContactService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ReconsiderAdjustApproval approval) {
        save(approval);
        //流程处理
        String result;
        CreditResolution creditResolution = creditResolutionService.get(approval.getCreditResolutionId());
        ProjectInformation projectInformation = projectInformationService.get(creditResolution.getcProjectInformationId());
        Map<String, Object> msgVariables = new HashMap<>();
        if(approval.getConclusion().equals(ConstantConversionUtil.APPROVAL_RESULT_PASS)){
            result = ConstantConversionUtil.APPROVAL_RESULT_PASS_DISPLAY;
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "项目名称：" + projectInformation.getProjectName());
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "项目名称“" + projectInformation.getProjectName() + "”的项目复议调整审批已审核已通过。进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.CREDIT_RESOLUTION) + "授信决议，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        }else {
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "项目名称：" + projectInformation.getProjectName());
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "项目名称“" + projectInformation.getProjectName() + "”的项目复议调整已退回“"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            result = "退回";
        }
        flowableService.completeMyTask(approval.getTaskId(), creditResolution, null, result, approval.getId(), msgVariables, ConstantUtil.CREDIT_RESOLUTION);
    }

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