package cn.com.victorysoft.business.calc.service;

import cn.com.victorysoft.business.calc.dao.IndexDao;
import cn.com.victorysoft.business.calc.dto.IndexUnitMonthDto;
import cn.com.victorysoft.business.calc.entity.UnitDaily;
import cn.com.victorysoft.business.indicator.dao.UnitMonthDao;
import cn.com.victorysoft.business.indicator.entity.UnitMonth;
import cn.com.victorysoft.business.indicator.service.UnitMonthService;
import cn.com.victorysoft.business.sys.param.UnitDateParam;
import cn.com.victorysoft.business.sys.param.UnitMonthParam;
import cn.com.victorysoft.business.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Index;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 水质检测数据同步服务
 */
@Slf4j
@Service
public class IndexService {

    @Resource
    private IndexDao indexDao;
    @Resource
    private UnitMonthDao unitMonthDao;

    /**
     * 查询单位日度数据
     */
    public UnitDaily selectUnitDaily(UnitDateParam param) {
        UnitDaily today = indexDao.selectUnitDaily(param);
        UnitDateParam paramYesterday = new UnitDateParam();
        paramYesterday.setUnitcode(param.getUnitcode());
        paramYesterday.setTestdate(DateUtils.addDays(param.getTestdate(), - 1));
        UnitDaily yesterday = indexDao.selectUnitDaily(paramYesterday);
        if (today == null) {
            today = new UnitDaily();
        }
        if (yesterday != null) {
            today.setTotalWellDiff(sub(today.getTotalWell(), yesterday.getTotalWell()));
            today.setOpenWellDiff(sub(today.getOpenWell(), yesterday.getOpenWell()));
            today.setSingleWellDiff(sub(today.getSingleWell(), yesterday.getSingleWell()));
            today.setSplitWellDiff(sub(today.getSplitWell(), yesterday.getSplitWell()));
            today.setDailyWaterDiff(sub(today.getDailyWater(), yesterday.getDailyWater()));
        }
        return today;
    }

    public IndexUnitMonthDto selectUnitMonth(UnitMonthParam param) {
        IndexUnitMonthDto dto = new IndexUnitMonthDto();
        // 查询单位本月
        UnitMonth current = unitMonthDao.selectUnitMonth(param);
        dto.setCurrent(current);
        // 查询上月
        UnitMonthParam paramPrev = new UnitMonthParam();
        paramPrev.setUnitcode(param.getUnitcode());
        paramPrev.setMonth(DateUtils.getPreviousMonth(param.getMonth(), 1));
        UnitMonth prevMonth = unitMonthDao.selectUnitMonth(paramPrev);
        dto.setPrevMonth(prevMonth);
        // 查询去年同期
        paramPrev.setMonth(DateUtils.getPreviousMonth(param.getMonth(), 12));
        UnitMonth prevYear = unitMonthDao.selectUnitMonth(paramPrev);
        dto.setPrevYear(prevYear);

        // 查询本单位和子单位数据
        List<UnitMonth> list = unitMonthDao.selectListMonth(param);
        dto.setList(list);

        return dto;
    }

    private Integer sub(Integer today, Integer yesterday) {
        return today != null && yesterday != null ? today - yesterday : null;
    }

    private BigDecimal sub(BigDecimal today, BigDecimal yesterday) {
        return today != null && yesterday != null ? today.subtract(yesterday) : null;
    }

}
