package com.jeeplus.modules.sg.audit.report.service;

import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.information.mapper.InformationMapper;
import com.jeeplus.modules.sg.audit.report.entity.SgReport;
import com.jeeplus.modules.sg.audit.report.mapper.ReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ReportService extends CrudService<ReportMapper, SgReport> {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private InformationMapper informationMapper;


    public List<SgReport> findData(SgReport sgReport){
        dataRuleFilter(sgReport);
        return reportMapper.findData(sgReport);
    }

    public List<SgReport> findList(SgReport sgReport){
        dataRuleFilter(sgReport);
        return reportMapper.findList(sgReport);
    }

    public List<SgReport> findUnitData(SgReport sgReport){
        dataRuleFilter(sgReport);
        return reportMapper.findUnitData(sgReport);
    }

    public List<SgReport> findUnitList(SgReport sgReport){
        dataRuleFilter(sgReport);
        return reportMapper.findUnitList(sgReport);
    }
    public List<SgReport> findSecondList(SgReport sgReport){
        dataRuleFilter(sgReport);
        return reportMapper.findSecondList(sgReport);
    }

    public List<SgReport> findSecondData(SgReport sgReport){
        dataRuleFilter(sgReport);
        return reportMapper.findSecondData(sgReport);
    }

    public List<Information> findInformationList(SgReport sgReport) {
        dataRuleFilter(sgReport);
        List<Information> list = reportMapper.findInformationList(sgReport);
        String find = informationMapper.findOverTime("overTime");
        int overTime = Integer.parseInt(find);
        Calendar cal = Calendar.getInstance();
        long between_days = 0;
        for (Information in : list) {
            //判断审定日期是否为空
//			if (in.getApprovalDate() != null) {
            //格式化审定日期
            if ("否".equals(in.getFirstFinish()) || "否".equals(in.getSecondFinish())) {
                in.setComplete("否");
                if (in.getApprovalDate() != null) {
                    cal.setTime(in.getApprovalDate());
                    long appDay = cal.getTimeInMillis();
                    cal.setTime(new Date());
                    long nowDate = cal.getTimeInMillis();
                    between_days = (nowDate - appDay) / (1000 * 3600 * 24);
                    in.setNoFinishDate(String.valueOf(between_days) + "天");
                }
            } else {
                in.setComplete("是");
                if (in.getApprovalDate() != null) {
                    cal.setTime(in.getApprovalDate());
                    long appDay = cal.getTimeInMillis();
                    if (in.getFirstFinishDate() != null || in.getSecondFinishDate() != null) {
                        if (in.getSecondFinishDate() != null) {
                            cal.setTime(in.getSecondFinishDate());
                            long secondDate = cal.getTimeInMillis();
                            between_days = (secondDate - appDay) / (1000 * 3600 * 24);
                            in.setFinishDate(String.valueOf(between_days) + "天");
                            if (between_days > overTime) {
                                in.setOverdue("是");
                            } else {
                                in.setOverdue("否");
                            }
                        } else {
                            cal.setTime(in.getFirstFinishDate());
                            long firstDate = cal.getTimeInMillis();
                            between_days = (firstDate - appDay) / (1000 * 3600 * 24);
                            in.setFinishDate(String.valueOf(between_days) + "天");
                            if (between_days > overTime) {
                                in.setOverdue("是");
                            } else {
                                in.setOverdue("否");
                            }
                        }
                    }
                }
            }
//			}
        }
        return list;
    }

}
