package com.yiboshi.science.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiboshi.science.base.BaseServiceImpl;
import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.config.bean.SystemProperties;
import com.yiboshi.science.config.security.SecurityUserHolder;
import com.yiboshi.science.dao.ComYearDAO;
import com.yiboshi.science.entity.ComYear;
import com.yiboshi.science.param.dto.ComYearDTO;
import com.yiboshi.science.param.query.ComYearQueryVO;
import com.yiboshi.science.service.ComYearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 年度设置表 Service 实现类
 *
 * @author lkl
 * @version 2021-08-26
 */
@Service
public class ComYearServiceImpl extends BaseServiceImpl<ComYearDAO, ComYearQueryVO, ComYearDTO, ComYear> implements ComYearService {

    @Autowired
    private ComYearDAO comYearDAO;
    private final SystemProperties properties;
    public ComYearServiceImpl(SystemProperties properties) {
        this.properties = properties;
    }

    @Override
    protected void setCriteriaForQuery(ComYearQueryVO vo, QueryWrapper<ComYearQueryVO> criteria) {
        if (Objects.nonNull(vo.getReportYear())) {
            criteria.eq("report_year", vo.getReportYear());
        }
        if (Objects.nonNull(vo.getUnitCode())) {
            criteria.eq("unit_code", vo.getUnitCode());
        }
        if (Objects.nonNull(vo.getYearType())) {
            criteria.eq("year_type", vo.getYearType());
        }
        if (Objects.nonNull(vo.getStartTime())) {
            criteria.eq("start_time", vo.getStartTime());
        }
        if (Objects.nonNull(vo.getEndTime())) {
            criteria.eq("end_time", vo.getEndTime());
        }
        if (Objects.nonNull(vo.getRemark())) {
            criteria.eq("remark", vo.getRemark());
        }
        if (Objects.nonNull(vo.getCheckYear())) {
            criteria.eq("check_year", vo.getCheckYear());
        }
    }

    @Override
    public Pagination<ComYearDTO> getListByPage(ComYearQueryVO vo) {
        QueryWrapper criteria = new QueryWrapper();
        setCriteriaForQuery(vo, criteria);
        Page<ComYearQueryVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<ComYearDTO> dtoList = comYearDAO.getListByPage(page, criteria).getRecords();
        return new Pagination<>(dtoList, page.getTotal(), vo.getPageSize());
    }

    @Override
    public Integer getCheckYear() {
        return comYearDAO.getMaxYearByType(3);
    }
    @Override
    public Integer getTestYear() {
        return comYearDAO.getMaxYearByType(2);
    }

    @Override
    public Integer getReportYear() {
        return comYearDAO.getMaxYearByType(1);
    }

    @Override
    public ComYearDTO getYearByTreeCode(String treeCode, int reportYear, int yearType) {
        ComYearDTO comYearDTO = new ComYearDTO();
        ComYear comYear = this.getByTreeCode(treeCode, reportYear, yearType);
        Date Date = Calendar.getInstance().getTime();
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (null != comYear) {
            comYearDTO = convert2DTO(comYear);
            String start = time.format(comYear.getStartTime());
            String end = time.format(comYear.getEndTime());
            if (Date.before(comYear.getStartTime())) {
                comYearDTO.setDisabled(false);
                comYearDTO.setDescription(start + "--" + end + "(时间未到)");
            } else if (Date.after(comYear.getEndTime())) {
                comYearDTO.setDisabled(false);
                comYearDTO.setDescription(start + "--" + end + "(已过期)");
            } else {
                comYearDTO.setDisabled(true);
                comYearDTO.setDescription(start + "--" + end);
            }
        } else {
            comYearDTO.setDisabled(false);
            comYearDTO.setDescription("未设置时间");
        }
        if (treeCode.length() == 4) {
            comYearDTO.setDisabled(true);
        }
        comYearDTO.setReportYear(reportYear);
        return comYearDTO;
    }

    public String getCheckYear(int year) {
        ComYear comYear = new ComYear();
        comYear.setYearType(1);
        comYear.setReportYear(year);
        comYear = this.getEntity(comYear);
        return comYear == null ? "" : comYear.getCheckYear();
    }

    @Override
    public ComYearDTO getYearInfo(int type) {
        ComYearDTO comYearDTO = new ComYearDTO();
        Integer reportYear=0;
        switch (type){
            case 1:
                reportYear = this.getReportYear();
                break;
            case 2:
                reportYear = this.getTestYear();
                break;
            case 3:
                reportYear = this.getCheckYear();
                break;
        }
        if (SecurityUserHolder.getUnitCode().length() != properties.getDefaultCodeLength()) {
            comYearDTO = this.getYearInfo(SecurityUserHolder.getUnitCode(), reportYear, type);
        } else {
            comYearDTO.setUnitCode(SecurityUserHolder.getUnitCode());
            comYearDTO.setYearType(type);
            comYearDTO.setDisabled(true);
            comYearDTO.setReportYear(reportYear.equals(0) ? Calendar.getInstance().get(Calendar.YEAR) : reportYear + 1);
        }
        return comYearDTO;
    }
    public ComYearDTO getYearInfo(String treeCode, int reportYear, int yearType) {
        ComYearDTO comYearDTO = new ComYearDTO();
        ComYear comYear = this.getByTreeCode(treeCode, reportYear, yearType);
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (null != comYear) {
            comYearDTO = convert2DTO(comYear);
            String start = time.format(comYear.getStartTime());
            String end = time.format(comYear.getEndTime());
            comYearDTO.setDescription("当前填报年度：" + comYear.getReportYear() + ", 上级单位设置起止时间：" + start + "--" + end);
            ComYear year = new ComYear();
            year.setUnitCode(treeCode);
            year.setReportYear(reportYear);
            year.setYearType(yearType);
            year = this.getEntity(year);
            if (year == null)
                comYearDTO.setDisabled(true);
        } else {
            comYearDTO.setDescription("上级单位未设置填报时间");
        }
        comYearDTO.setUnitCode(treeCode);
        return comYearDTO;
    }

    /**
     * 添加/更新年度设置
     *
     * @param vo
     * @return
     */
    @Override
    public void addOrUpdateYearInfo(ComYear vo) {
        ComYear comYear = new ComYear();
        comYear.setUnitCode(vo.getUnitCode());
        comYear.setReportYear(vo.getReportYear());
        comYear.setYearType(vo.getYearType());
        comYear = this.getEntity(comYear);
        if (comYear == null) {
            this.insert(vo);
        } else {
            comYear.setStartTime(vo.getStartTime());
            comYear.setEndTime(vo.getEndTime());
            comYear.setCheckYear(vo.getCheckYear());
            comYear.setRemark(vo.getRemark());
            this.update(comYear);
        }
    }

    private ComYear getByTreeCode(String treeCode, int reportYear, int yearType) {
        if (treeCode.length() != properties.getDefaultCodeLength())
            treeCode = treeCode.substring(0, treeCode.length() - properties.getDefaultCodeLength());
        ComYear comYear = new ComYear();
        comYear.setUnitCode(treeCode);
        comYear.setReportYear(reportYear);
        comYear.setYearType(yearType);
        comYear = this.getEntity(comYear);
        if (null == comYear && ((treeCode.length() / properties.getDefaultCodeLength()) > 1)) {
            comYear = getByTreeCode(treeCode, reportYear, yearType);
        }
        return comYear;
    }
}