package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.ems;

import cn.estsh.i3plus.ext.mes.api.busi.ems.IEmsCloudService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesOutPutStatisticsRepository;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dominic
 * @description
 * @createDate 2022/7/14 14:32
 */
@Service
public class EmsCloudService implements IEmsCloudService {

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    @Override
    public double getLastMonthlyOutput(Integer annual, Integer monthly) {
        String startYear = String.valueOf(annual - 1);
        String endYear = String.valueOf(annual - 1);
        String startMonth = String.valueOf(monthly);
        String endMonth = String.valueOf(monthly + 1);
        if (startMonth.length() == 1) {
            startMonth = "0" + startMonth;
        }
        if (endMonth.length() == 1) {
            endMonth = "0" + endMonth;
        } else if (Integer.parseInt(endMonth) == 13) {
            endYear = String.valueOf(annual);
            endMonth = "01";
        }
        String startTime = startYear + "-" + startMonth + "-01";
        String endTime = endYear + "-" + endMonth + "-01";
        DdlPackBean packBean = DdlPackBean.getDdlPackBean();
        DdlPreparedPack.timeBuilder(startTime, endTime, "workTime", true, false, packBean);
        List<MesOutPutStatistics> outPutStatisticsList = outPutStatisticsRepository.findByHqlWhere(packBean);
        return outPutStatisticsList.stream().mapToDouble(MesOutPutStatistics::getQty).sum();
    }
}
