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.dict.impl.FileModuleDict;
import com.sunxung.factoring.dict.impl.ProjectInitiationStatusDict;
import com.sunxung.factoring.entity.creditresolution.CreditResolution;
import com.sunxung.factoring.entity.creditresolution.ReconsiderAdjust;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.project.ProjectCoreEnterprise;
import com.sunxung.factoring.entity.project.ProjectInformation;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.entity.sys.SysAttachmentRef;
import com.sunxung.factoring.mapper.creditresolution.ReconsiderAdjustMapper;
import com.sunxung.factoring.service.creditresolution.ICreditResolutionService;
import com.sunxung.factoring.service.creditresolution.IReconsiderAdjustService;
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.ISupplierInfoService;
import com.sunxung.factoring.service.sys.FileService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
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.List;
import java.util.Map;

/**
 * <p>
 * 复议调整 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-12-21
 */
@Service("reconsider_adjust_addService")
public class ReconsiderAdjustServiceImpl extends ServiceImpl<ReconsiderAdjustMapper, ReconsiderAdjust> implements IReconsiderAdjustService {

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

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

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

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private IEnterpriseService enterpriseService;

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ReconsiderAdjust reconsiderAdjust) {
        save(reconsiderAdjust);
        List<Long> files = reconsiderAdjust.getFiles();
        AttachmentDto attachmentDto = AttachmentDto.builder()
                .setEntityId(reconsiderAdjust.getId())
                .setChildEnum(FileModuleDict.ChildEnum.RECONSIDER_ADJUST_FILE)
                .build();
        sysAttachmentRefService.saveRef(files, attachmentDto);

        CreditResolution creditResolution = creditResolutionService.get(reconsiderAdjust.getCreditResolutionId());
        creditResolutionService.lambdaUpdate().eq(CreditResolution::getId,creditResolution.getId()).set(CreditResolution::getStatusStr, ProjectInitiationStatusDict.ChildEnum.PROCESSING.getCode()).update();
        ProjectInformation projectInformation = projectInformationService.get(creditResolution.getcProjectInformationId());
        //流程处理
        Map<String, Object> msgVariables = new HashMap<>();
        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);
        flowableService.completeMyTask(reconsiderAdjust.getTaskId(), creditResolution, null, null, reconsiderAdjust.getId(), msgVariables, ConstantUtil.CREDIT_RESOLUTION);
    }

    @Override
    public ReconsiderAdjust getView(Long id) {
        ReconsiderAdjust reconsiderAdjust = getById(id);
        CreditResolution creditResolution = creditResolutionService.get(reconsiderAdjust.getCreditResolutionId());
        creditResolution.setFinancingRatioLow(creditResolution.getFinancingRatioLow() == null? 0: creditResolution.getFinancingRatioLow() * 100);
        creditResolution.setFinancingRatioHigh(creditResolution.getFinancingRatioHigh() == null? 0: creditResolution.getFinancingRatioHigh() * 100);
        creditResolution.setPlanIrr(creditResolution.getPlanIrr() == null? 0: creditResolution.getPlanIrr() * 100);
        creditResolution.setProposedFinancingRatioTwoLow(creditResolution.getProposedFinancingRatioTwoLow() == null? 0: creditResolution.getProposedFinancingRatioTwoLow() * 100);
        creditResolution.setProposedFinancingRatioTwoHigh(creditResolution.getProposedFinancingRatioTwoHigh() == null? 0: creditResolution.getProposedFinancingRatioTwoHigh() * 100);
        creditResolution.setPlanIrrTwo(creditResolution.getPlanIrrTwo() == null? 0: creditResolution.getPlanIrrTwo() * 100);
        creditResolution.setFinancingRateLow(creditResolution.getFinancingRateLow() == null? 0: creditResolution.getFinancingRateLow() * 100);
        creditResolution.setFinancingRateHigh(creditResolution.getFinancingRateHigh() == null? 0: creditResolution.getFinancingRateHigh() * 100);
        creditResolution.setManagementRateLow(creditResolution.getManagementRateLow() == null? 0: creditResolution.getManagementRateLow() * 100);
        creditResolution.setManagementRateHigh(creditResolution.getManagementRateHigh() == null? 0: creditResolution.getManagementRateHigh() * 100);
        creditResolution.setMarginRatioLow(creditResolution.getMarginRatioLow() == null? 0: creditResolution.getMarginRatioLow() * 100);
        creditResolution.setMarginRatioHigh(creditResolution.getMarginRatioHigh() == null? 0: creditResolution.getMarginRatioHigh() * 100);
        creditResolution.setComprehensiveQuotation(creditResolution.getComprehensiveQuotation() == null? 0: creditResolution.getComprehensiveQuotation() * 100);
        ProjectInformation projectInformation = projectInformationService.get(creditResolution.getcProjectInformationId());
        SupplierInfo supplierInfo = supplierInfoService.get(projectInformation.getcSupplierInfoId());
        Enterprise supplierEnterprise = enterpriseService.getById(supplierInfo.getcEnterpriseId());
        ProjectCoreEnterprise projectCoreEnterprise = projectCoreEnterpriseService.getByProjectInfoId(projectInformation.getId());
        Enterprise coreEnterprise = enterpriseService.getById(projectCoreEnterprise.getcEnterpriseId());
        reconsiderAdjust.setProjectInformation(projectInformation);
        reconsiderAdjust.setSupplierEnterprise(supplierEnterprise);
        reconsiderAdjust.setCoreEnterprise(coreEnterprise);
        AttachmentDto attachmentDto = AttachmentDto.builder()
                .setEntityId(reconsiderAdjust.getId())
                .setChildEnum(FileModuleDict.ChildEnum.RECONSIDER_ADJUST_FILE)
                .build();
        List<FileStorage> files = sysAttachmentRefService.getFiles(attachmentDto);
        reconsiderAdjust.setFileStorages(files);
        return reconsiderAdjust;
    }
}
