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.ProjectCourseControlInfo;
import com.jeeplus.modules.sg.budgetSpectaculars.mapper.ProjectCourseMapper;
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 ProjectCourseService extends CrudService<ProjectCourseMapper, ProjectCourseControlInfo> {

    @Autowired
    private ProjectCourseMapper mapper;

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

    private void disposeProjectBasics(List<ProjectCourseControlInfo> projectCourseControlList){
        for (ProjectCourseControlInfo info: projectCourseControlList) {
            //处理计划开工日期准确性   |计划开工日期- 首次领料日期| 两个日期均有为绿灯 0-15天（含15天）灰色 15-30天（含30天）黄灯 30天以上红灯（字体颜色）
            Integer differenceValue = dateDifferenceValue(info.getPlanStartUpDate(), info.getFirstAcquisitionTime());
            if(StringUtils.isNotBlank(info.getPlanStartUpDate()) && StringUtils.isNotBlank(info.getFirstAcquisitionTime())){
                info.setPlanStartWorkingTimeAccuracy(differenceValue.toString());
                info.setPlanStartWorkingTimeAccuracyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setPlanStartWorkingTimeAccuracy("");
                    info.setPlanStartWorkingTimeAccuracyFlag(2);
                }else{
                    if (0 <= differenceValue &&  differenceValue<= 15) {
                        info.setPlanStartWorkingTimeAccuracy(differenceValue.toString());
                        info.setPlanStartWorkingTimeAccuracyFlag(1);
                    }else if (15 < differenceValue &&  differenceValue<= 30) {
                        info.setPlanStartWorkingTimeAccuracy(differenceValue.toString());
                        info.setPlanStartWorkingTimeAccuracyFlag(2);
                    }else if (30 < differenceValue) {
                        info.setPlanStartWorkingTimeAccuracy(differenceValue.toString());
                        info.setPlanStartWorkingTimeAccuracyFlag(3);
                    }else if(differenceValue<0){
                        info.setPlanStartWorkingTimeAccuracy("逻辑错误");
                        info.setPlanStartWorkingTimeAccuracyFlag(4);
                    }
                }
            }
            //处理 结算送审日期及时性  结算送审日期-实际竣工日期 两个日期均有为绿灯 0-15天（含15天）灰色 15-30天（含30天）黄灯 30天以上红灯 小于0天，显示“逻辑错误”
            differenceValue = dateDifferenceValue(info.getPlanStartUpDate(), info.getPracticalcompletionDate());
            if(StringUtils.isNotBlank(info.getPlanStartUpDate()) && StringUtils.isNotBlank(info.getPracticalcompletionDate())){
                info.setCalculateAuditDateTimely(differenceValue.toString());
                info.setCalculateAuditDateTimelyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setCalculateAuditDateTimely("");
                    info.setCalculateAuditDateTimelyFlag(2);
                }else {
                    if (0 <= differenceValue && differenceValue <= 15) {
                        info.setCalculateAuditDateTimely(differenceValue.toString());
                        info.setCalculateAuditDateTimelyFlag(1);
                    } else if (15 < differenceValue && differenceValue <= 30) {
                        info.setCalculateAuditDateTimely(differenceValue.toString());
                        info.setCalculateAuditDateTimelyFlag(2);
                    } else if (30 < differenceValue) {
                        info.setCalculateAuditDateTimely(differenceValue.toString());
                        info.setCalculateAuditDateTimelyFlag(3);
                    } else if (differenceValue < 0) {
                        info.setCalculateAuditDateTimely("逻辑错误");
                        info.setCalculateAuditDateTimelyFlag(4);
                    }
                }
            }
            //处理 结算审定日期及时性  结算审定日期-结算送审日期 两个日期均有为绿灯 0-15天（含15天）灰色 15-30天（含30天）黄灯 30天以上红灯 小于0天，显示“逻辑错误”
            differenceValue = dateDifferenceValue(info.getCalculationExamineTime(), info.getCalculationForReviewTime());
            if(StringUtils.isNotBlank(info.getCalculationExamineTime()) && StringUtils.isNotBlank(info.getCalculationForReviewTime())){
                info.setCalculateExamineDateTimely(differenceValue.toString());
                info.setCalculateExamineDateTimelyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setCalculateExamineDateTimely("");
                    info.setCalculateExamineDateTimelyFlag(2);
                }else {
                    if (0 <= differenceValue && differenceValue <= 15) {
                        info.setCalculateExamineDateTimely(differenceValue.toString());
                        info.setCalculateExamineDateTimelyFlag(1);
                    } else if (15 < differenceValue && differenceValue <= 30) {
                        info.setCalculateExamineDateTimely(differenceValue.toString());
                        info.setCalculateExamineDateTimelyFlag(2);
                    } else if (30 < differenceValue) {
                        info.setCalculateExamineDateTimely(differenceValue.toString());
                        info.setCalculateExamineDateTimelyFlag(3);
                    } else if (differenceValue < 0) {
                        info.setCalculateExamineDateTimely("逻辑错误");
                        info.setCalculateExamineDateTimelyFlag(4);
                    }
                }
            }
            //处理 施工费入账日期及时性  施工费入账日期-结算审定日期 两个日期均有为绿灯 0-3天（含3天）灰色 3-6天（含6天）黄灯 6天以上红灯 小于0天，显示“逻辑错误”
            differenceValue = dateDifferenceValue(info.getFirstConstructionFeeRecordedTime(), info.getCalculationExamineTime());
            if(StringUtils.isNotBlank(info.getFirstConstructionFeeRecordedTime()) && StringUtils.isNotBlank(info.getCalculationExamineTime())){
                info.setConstructionFeeRecordedTimely(differenceValue.toString());
                info.setConstructionFeeRecordedTimelyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setConstructionFeeRecordedTimely("");
                    info.setConstructionFeeRecordedTimelyFlag(2);
                }else {
                    if (0 <= differenceValue && differenceValue <= 3) {
                        info.setConstructionFeeRecordedTimely(differenceValue.toString());
                        info.setConstructionFeeRecordedTimelyFlag(1);
                    } else if (3 < differenceValue && differenceValue <= 6) {
                        info.setConstructionFeeRecordedTimely(differenceValue.toString());
                        info.setConstructionFeeRecordedTimelyFlag(2);
                    } else if (6 < differenceValue) {
                        info.setConstructionFeeRecordedTimely(differenceValue.toString());
                        info.setConstructionFeeRecordedTimelyFlag(3);
                    } else if (differenceValue < 0) {
                        info.setConstructionFeeRecordedTimely("逻辑错误");
                        info.setConstructionFeeRecordedTimelyFlag(4);
                    }
                }
            }
            //处理 核对甲供材是否一致  甲供材系统中是否一致列 一致绿灯 不一致红灯
                if("是".equals(info.getIfGetRejectedMaterialFinish())){
                    info.setIfCoincideCheckWhichMaterial(info.getIfGetRejectedMaterialFinish());
                    info.setIfCoincideCheckWhichMaterialFlag(0);
                }else{
                    info.setIfCoincideCheckWhichMaterial(info.getIfGetRejectedMaterialFinish());
                    info.setIfCoincideCheckWhichMaterialFlag(4);
                }
            //处理 提报竣工决算日期及时性  提报竣工决算日期-施工费入账日期 两个日期均有为绿灯 0-2天（含2天）灰色 3-4天（含4天）黄灯 5天以上红灯 小于0天，显示“逻辑错误”
            differenceValue = dateDifferenceValue(info.getCompletedActualBudgetTime(), info.getFirstConstructionFeeRecordedTime());
            if(StringUtils.isNotBlank(info.getCompletedActualBudgetTime()) && StringUtils.isNotBlank(info.getFirstConstructionFeeRecordedTime())){
                info.setCompletionSettlementDateTimely(differenceValue.toString());
                info.setCompletionSettlementDateTimelyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setCompletionSettlementDateTimely("");
                    info.setCompletionSettlementDateTimelyFlag(2);
                }else {
                    if (0 <= differenceValue && differenceValue <= 2) {
                        info.setCompletionSettlementDateTimely(differenceValue.toString());
                        info.setCompletionSettlementDateTimelyFlag(1);
                    } else if (2 < differenceValue && differenceValue <= 4) {
                        info.setCompletionSettlementDateTimely(differenceValue.toString());
                        info.setCompletionSettlementDateTimelyFlag(2);
                    } else if (4 < differenceValue) {
                        info.setCompletionSettlementDateTimely(differenceValue.toString());
                        info.setCompletionSettlementDateTimelyFlag(3);
                    } else if (differenceValue < 0) {
                        info.setCompletionSettlementDateTimely("逻辑错误");
                        info.setCompletionSettlementDateTimelyFlag(4);
                    }
                }
            }
            //处理 决算送审日期及时性  决算送审日期-提报竣工决算日期 两个日期均有为绿灯 0-3天（含3天）灰色 4-5天（含5天）黄灯 5天以上红灯 小于0天，显示“逻辑错误”
            differenceValue = dateDifferenceValue(info.getActualBudgetSubmittalsTime(), info.getCompletedActualBudgetTime());
            if(StringUtils.isNotBlank(info.getActualBudgetSubmittalsTime()) && StringUtils.isNotBlank(info.getCompletedActualBudgetTime())){
                info.setFinalAuditDateTimely(differenceValue.toString());
                info.setFinalAuditDateTimelyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setFinalAuditDateTimely("");
                    info.setFinalAuditDateTimelyFlag(2);
                }else {
                    if (0 <= differenceValue && differenceValue <= 3) {
                        info.setFinalAuditDateTimely(differenceValue.toString());
                        info.setFinalAuditDateTimelyFlag(1);
                    } else if (3 < differenceValue && differenceValue <= 5) {
                        info.setFinalAuditDateTimely(differenceValue.toString());
                        info.setFinalAuditDateTimelyFlag(2);
                    } else if (5 < differenceValue) {
                        info.setFinalAuditDateTimely(differenceValue.toString());
                        info.setFinalAuditDateTimelyFlag(3);
                    } else if (differenceValue < 0) {
                        info.setFinalAuditDateTimely("逻辑错误");
                        info.setFinalAuditDateTimelyFlag(4);
                    }
                }
            }
            //处理 出具决算审计报告及时性  决算审计报告出具日期-决算送审日期 两个日期均有为绿灯 0-7天（含7天）灰色 8-15天（含15天）黄灯 15天以上 小于0天，显示“逻辑错误”
            differenceValue = dateDifferenceValue(info.getActualBudgetSubmittalsTime(), info.getCompletedActualBudgetTime());
            if(StringUtils.isNotBlank(info.getActualBudgetSubmittalsTime()) && StringUtils.isNotBlank(info.getCompletedActualBudgetTime())){
                info.setFinalAccountsAuditReportTimely(differenceValue.toString());
                info.setFinalAccountsAuditReportTimelyFlag(0);
            }else{
                if(null == differenceValue){
                    info.setFinalAccountsAuditReportTimely("");
                    info.setFinalAccountsAuditReportTimelyFlag(2);
                }else {
                    if (0 <= differenceValue && differenceValue <= 7) {
                        info.setFinalAccountsAuditReportTimely(differenceValue.toString());
                        info.setFinalAccountsAuditReportTimelyFlag(1);
                    } else if (7 < differenceValue && differenceValue <= 15) {
                        info.setFinalAccountsAuditReportTimely(differenceValue.toString());
                        info.setFinalAccountsAuditReportTimelyFlag(2);
                    } else if (15 < differenceValue) {
                        info.setFinalAccountsAuditReportTimely(differenceValue.toString());
                        info.setFinalAccountsAuditReportTimelyFlag(3);
                    } else if (differenceValue < 0) {
                        info.setFinalAccountsAuditReportTimely("逻辑错误");
                        info.setFinalAccountsAuditReportTimelyFlag(4);
                    }
                }
            }
        }

    }

    private Integer dateDifferenceValue(String dataOne,String dateTwo){
        Integer betweenDaysValue = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        try {
            if(StringUtils.isBlank(dataOne) && StringUtils.isBlank(dateTwo)){//如果两个时间均为空 则不进行计算，前端页面展示为空
                return betweenDaysValue;
            }else{

                Date date1 = null;
                if(StringUtils.isNotBlank(dataOne)){
                    date1 = simpleDateFormat.parse(dataOne);
                }else{
                    date1 = new Date();
                }
                Date date2 = null;
                if(StringUtils.isNotBlank(dateTwo)){
                    date2 = simpleDateFormat.parse(dateTwo);
                }else{
                    date2 = new Date();
                }
                Long betweenDays = (date1.getTime() - date2.getTime()) / (1000L*3600L*24L);
                betweenDaysValue = betweenDays.intValue();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return betweenDaysValue;
    }
}
