package com.jeeplus.modules.sg.budgetSpectaculars.service;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.budgetSpectaculars.entity.PostEvaluationInfo;
import com.jeeplus.modules.sg.budgetSpectaculars.mapper.ProjectPostEvaluationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: 徐滕
 * @create: 2021-09-22 17:28
 **/
@Service
@Transactional(readOnly = true)
public class ProjectPostEvaluationService extends CrudService<ProjectPostEvaluationMapper, PostEvaluationInfo> {

    @Autowired
    private ProjectPostEvaluationMapper mapper;

    public Page<PostEvaluationInfo> findPage(Page<PostEvaluationInfo> page, PostEvaluationInfo entity) {
        Page<PostEvaluationInfo> page1 = super.findPage(page, entity);
        disposeProjectBasics(page1.getList());
        return page1;
    }

    private void disposeProjectBasics(List<PostEvaluationInfo> projectCourseControlList){
        for (PostEvaluationInfo info: projectCourseControlList) {
            //处理计划开工日期准确性   |计划开工日期- 首次领料日期| 0-30天（含30天）绿灯 30天以上红灯(前后数据均不能为空)
            if(StringUtils.isNotBlank(info.getPlanStartUpDate()) && StringUtils.isNotBlank(info.getFirstAcquisitionTime())){
                Integer differenceValue = dateDifferenceValue(info.getPlanStartUpDate(), info.getFirstAcquisitionTime());
                if (0 <= differenceValue &&  differenceValue<= 15) {
                    info.setPlanStartWorkingTimeAccuracy(differenceValue.toString());
                    info.setPlanStartWorkingTimeAccuracyFlag(0);
                }else if(differenceValue > 30){
                    info.setPlanStartWorkingTimeAccuracy(differenceValue.toString());
                    info.setPlanStartWorkingTimeAccuracyFlag(4);
                }if(differenceValue<0){
                    info.setPlanStartWorkingTimeAccuracy("逻辑错误");
                    info.setPlanStartWorkingTimeAccuracyFlag(4);
                }
            }else{
                info.setPlanStartWorkingTimeAccuracy("");
            }
            //处理 结算送审日期及时性  结算送审日期-实际竣工日期 0-30天（含30天）绿灯 30天以上红灯 小于0天，显示“逻辑错误”
            if(StringUtils.isNotBlank(info.getPlanStartUpDate()) && StringUtils.isNotBlank(info.getPracticalcompletionDate())){
                Integer differenceValue = dateDifferenceValue(info.getPlanStartUpDate(), info.getPracticalcompletionDate());
                if (0 <= differenceValue &&  differenceValue<= 30) {
                    info.setCalculateAuditDateTimely(differenceValue.toString());
                    info.setCalculateAuditDateTimelyFlag(0);
                }else if (30 < differenceValue) {
                    info.setCalculateAuditDateTimely(differenceValue.toString());
                    info.setCalculateAuditDateTimelyFlag(4);
                }else if(differenceValue<0){
                    info.setCalculateAuditDateTimely("逻辑错误");
                    info.setCalculateAuditDateTimelyFlag(4);
                }
            }else{
                info.setCalculateAuditDateTimely("");
            }
            //处理 结算审定日期及时性  结算审定日期-结算送审日期  0-30天（含30天）绿灯 30天以上红灯 小于0天，显示“逻辑错误”
            if(StringUtils.isNotBlank(info.getCalculationExamineTime()) && StringUtils.isNotBlank(info.getCalculationForReviewTime())){
                Integer differenceValue = dateDifferenceValue(info.getCalculationExamineTime(), info.getCalculationForReviewTime());
                if (0 <= differenceValue &&  differenceValue<= 30) {
                    info.setCalculateExamineDateTimely(differenceValue.toString());
                    info.setCalculateExamineDateTimelyFlag(0);
                }else if (30 < differenceValue) {
                    info.setCalculateExamineDateTimely(differenceValue.toString());
                    info.setCalculateExamineDateTimelyFlag(4);
                }else if(differenceValue<0){
                    info.setCalculateExamineDateTimely("逻辑错误");
                    info.setCalculateExamineDateTimelyFlag(4);
                }
            }else{
                info.setCalculateExamineDateTimely("");
            }
            //处理 施工费入账日期及时性  施工费入账日期-结算审定日期 0-6天（含6天）绿灯 6天以上红灯 小于0天，显示“逻辑错误”
            if(StringUtils.isNotBlank(info.getFirstConstructionFeeRecordedTime()) && StringUtils.isNotBlank(info.getCalculationExamineTime())){
                Integer differenceValue = dateDifferenceValue(info.getFirstConstructionFeeRecordedTime(), info.getCalculationExamineTime());

                if (0 <= differenceValue &&  differenceValue<= 6) {
                    info.setConstructionFeeRecordedTimely(differenceValue.toString());
                    info.setConstructionFeeRecordedTimelyFlag(0);
                }else if (6 < differenceValue) {
                    info.setConstructionFeeRecordedTimely(differenceValue.toString());
                    info.setConstructionFeeRecordedTimelyFlag(4);
                }else if(differenceValue<0){
                    info.setConstructionFeeRecordedTimely("逻辑错误");
                    info.setConstructionFeeRecordedTimelyFlag(4);
                }
            }else{
                info.setConstructionFeeRecordedTimely("");
            }
            //处理 核对甲供材是否一致  甲供材系统中是否一致列 一致绿灯 不一致红灯
            if("是".equals(info.getIfGetRejectedMaterialFinish())){
                info.setIfCoincideCheckWhichMaterial(info.getIfGetRejectedMaterialFinish());
                info.setIfCoincideCheckWhichMaterialFlag(0);
            }else{
                info.setIfCoincideCheckWhichMaterial(info.getIfGetRejectedMaterialFinish());
                info.setIfCoincideCheckWhichMaterialFlag(4);
            }
            //处理 提报竣工决算日期及时性  提报竣工决算日期-施工费入账日期 0-5天（含5天）绿灯 5天以上红灯 小于0天，显示“逻辑错误”
            if(StringUtils.isNotBlank(info.getCompletedActualBudgetTime()) && StringUtils.isNotBlank(info.getFirstConstructionFeeRecordedTime())){
                Integer differenceValue = dateDifferenceValue(info.getCompletedActualBudgetTime(), info.getFirstConstructionFeeRecordedTime());

                if (0 <= differenceValue &&  differenceValue<= 5) {
                    info.setCompletionSettlementDateTimely(differenceValue.toString());
                    info.setCompletionSettlementDateTimelyFlag(0);
                }else if (5 < differenceValue) {
                    info.setCompletionSettlementDateTimely(differenceValue.toString());
                    info.setCompletionSettlementDateTimelyFlag(4);
                }else if(differenceValue<0){
                    info.setCompletionSettlementDateTimely("逻辑错误");
                    info.setCompletionSettlementDateTimelyFlag(4);
                }
            }else{
                info.setCompletionSettlementDateTimely("");
            }
            //处理 决算送审日期及时性  决算送审日期-提报竣工决算日期 0-5天（含5天）绿灯 5天以上红灯 小于0天，显示“逻辑错误”
            if(StringUtils.isNotBlank(info.getActualBudgetSubmittalsTime()) && StringUtils.isNotBlank(info.getCompletedActualBudgetTime())){
                Integer differenceValue = dateDifferenceValue(info.getActualBudgetSubmittalsTime(), info.getCompletedActualBudgetTime());

                if (0 <= differenceValue &&  differenceValue<= 5) {
                    info.setFinalAuditDateTimely(differenceValue.toString());
                    info.setFinalAuditDateTimelyFlag(0);
                }else if (5 < differenceValue) {
                    info.setFinalAuditDateTimely(differenceValue.toString());
                    info.setFinalAuditDateTimelyFlag(4);
                }else if(differenceValue<0){
                    info.setFinalAuditDateTimely("逻辑错误");
                    info.setFinalAuditDateTimelyFlag(4);
                }
            }else{
                info.setFinalAuditDateTimely("");
            }
            //处理 出具决算审计报告及时性  决算审计报告出具日期-决算送审日期 0-15天（含15天）绿灯 15天以上红灯 小于0天，显示“逻辑错误”
            if(StringUtils.isNotBlank(info.getActualBudgetSubmittalsTime()) && StringUtils.isNotBlank(info.getCompletedActualBudgetTime())){
                Integer differenceValue = dateDifferenceValue(info.getActualBudgetSubmittalsTime(), info.getCompletedActualBudgetTime());

                if (0 <= differenceValue &&  differenceValue<= 15) {
                    info.setFinalAccountsAuditReportTimely(differenceValue.toString());
                    info.setFinalAccountsAuditReportTimelyFlag(0);
                }else if (15 < differenceValue) {
                    info.setFinalAccountsAuditReportTimely(differenceValue.toString());
                    info.setFinalAccountsAuditReportTimelyFlag(4);
                }else if(differenceValue<0){
                    info.setFinalAccountsAuditReportTimely("逻辑错误");
                    info.setFinalAccountsAuditReportTimelyFlag(4);
                }
            }else{
                info.setFinalAccountsAuditReportTimely("");
            }
        }

    }

    private Integer dateDifferenceValue(String dataOne,String dateTwo){
        Integer betweenDaysValue = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        try {
            Date date1 = simpleDateFormat.parse(dataOne);
            Date date2 = simpleDateFormat.parse(dateTwo);
            Long betweenDays = (date1.getTime() - date2.getTime()) / (1000L*3600L*24L);
            betweenDaysValue = betweenDays.intValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return betweenDaysValue;
    }
}
