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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToMonthlyStatisticsDispatchService;
import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToStatisticsTravelService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdDataToStatisticsShiftProd;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdDataToStatisticsTravel;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToAutoStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesShiftProdCenterRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Slf4j
@Service
public class SxProdDataToMonthlyStatisticsShiftProdService implements ISxProdDataToMonthlyStatisticsDispatchService {

    @Autowired
    private MesShiftProdCenterRecordRepository shiftProdCenterRecordRepository;

    @Autowired
    private ISxProdDataToStatisticsTravelService prodDataToStatisticsTravelService;

    @Override
    public SxProdDataToAutoStatisticsModel restock(SxProdDataToAutoStatisticsModel data) {
        return data.moduleCode("MES_SHIFT_PROD_CENTER_RECORD").moduleName("停开机时间记录统计").statisticsDate(data.getDate());
    }

    @Override
    public SxProdDataToAutoStatisticsModel doTravel(SxProdDataToAutoStatisticsModel data) {
        return data.pid(prodDataToStatisticsTravelService.doTravel(data.getTravel()).getId().toString());
    }

    public List<MesShiftProdCenterRecord> queryShiftProdCenterRecordList(SxProdDataToAutoStatisticsModel data) {
        return getShiftProdCenterRecordList(data);
    }

    public MesProdDataToStatisticsShiftProd packItemData(SxProdDataToAutoStatisticsModel data, List<MesShiftProdCenterRecord> itemList) {
        MesProdDataToStatisticsShiftProd statistics = new MesProdDataToStatisticsShiftProd();
        statistics.setDataKey(data.getDataKey());
        statistics.setDataType(data.getStatisticsType());
        statistics.setStatisticsDate(data.getDate());
        statistics.setPid(data.getPid());
        statistics.setWorkTime(itemList.get(0).getWorkTime());
        statistics.setWorkCenterCode(itemList.get(0).getWorkCenterCode());
        statistics.setShiftCode(itemList.get(0).getShiftCode());
        statistics.setShiftName(itemList.get(0).getShiftName());
        statistics.setProdCode(itemList.get(0).getProdCode());
        statistics.setProdName(itemList.get(0).getProdName());
        statistics.setProdType(itemList.get(0).getProdType());
        statistics.setProdCategory(itemList.get(0).getProdCategory());
        statistics.setCategoryCode(itemList.get(0).getCategoryCode());
        statistics.setCareerCode(itemList.get(0).getCareerCode());
        statistics.setStatistics(getStatistics(itemList));
        statistics.setOrganizeCode(data.getOrganizeCode());
        statistics.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(statistics, data.getCreateUser());
        return statistics;
    }

    private List<MesShiftProdCenterRecord> getShiftProdCenterRecordList(SxProdDataToAutoStatisticsModel data) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(data.getOrganizeCode());
        DdlPreparedPack.timeBuilder(data.getStartTime(), data.getEndTime(), MesExtConstWords.WORK_TIME, true, false, packBean);
        return shiftProdCenterRecordRepository.findByHqlWhere(packBean);
    }

    private Double getStatistics(List<MesShiftProdCenterRecord> itemList) {
        return itemList.stream().filter(o -> null != o).mapToDouble(o -> getStatistics(o)).sum();
    }

    private Double getStatistics(MesShiftProdCenterRecord item) {
        try {
            return MathOperation.mul(new Double(TimeTool.getSecoundsBetweenTime(2, item.getStartTime(), item.getEndTime())), item.getWorkerQty());
        } catch (ParseException e) {
            return new Double(0);
        }
    }

}
