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.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProdDataToStatisticsOutPut;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToAutoStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesOutPutStatisticsRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProdDataToStatisticsOutPutRepository;
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.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SxProdDataToMonthlyStatisticsOutPut3ByMonthService implements ISxProdDataToMonthlyStatisticsDispatchService {

    @Autowired
    private SxProdDataToMonthlyStatisticsOutPutService dispatchService;

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    @Autowired
    private MesProdDataToStatisticsOutPutRepository prodDataToStatisticsOutPutRepository;

    @Override
    public String name() { return MesExtEnumUtil.JOB_STATISTICS_PROD_DATA_KEY.OUT_PUT_MONTH_KEY3.name(); }

    @Override
    public String flag() { return MesExtEnumUtil.JOB_STATISTICS_PROD_DATA_KEY.OUT_PUT_MONTH_KEY3.getFlag(); }

    @Override
    public SxProdDataToAutoStatisticsModel restock(SxProdDataToAutoStatisticsModel data) {
        return dispatchService.restock(data)
                .statisticsType(flag()).dataKey(MesExtEnumUtil.JOB_STATISTICS_PROD_DATA_KEY.OUT_PUT_MONTH_KEY3.getValue());
    }

    @Override
    public SxProdDataToAutoStatisticsModel query(SxProdDataToAutoStatisticsModel data) {

        List<MesOutPutStatistics> outPutStatisticsList = queryOutPutStatisticsList(data);

        Map<String, List<MesOutPutStatistics>> dataMap = getDataMap(outPutStatisticsList);

        return CollectionUtils.isEmpty(outPutStatisticsList) ? data.sourceCount(0).targetCount(0) :
                data.sourceCount(outPutStatisticsList.size()).targetCount(dataMap.size()).data(dataMap);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = {ImppBusiException.class, Exception.class})
    public SxProdDataToAutoStatisticsModel exec(SxProdDataToAutoStatisticsModel data) {

        dispatchService.doTravel(data);

        if (null == data.getData()) return data;

        Map<String, List<MesOutPutStatistics>> dataMap = (Map<String, List<MesOutPutStatistics>>) data.getData();

        saveStatisticsData(data, dataMap);

        return data;
    }

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


    private Map<String, List<MesOutPutStatistics>> getDataMap(List<MesOutPutStatistics> outPutStatisticsList) {
        return CollectionUtils.isEmpty(outPutStatisticsList) ? null :
                outPutStatisticsList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesOutPutStatistics::getPartNo));
    }

    private void saveStatisticsData(SxProdDataToAutoStatisticsModel data, Map<String, List<MesOutPutStatistics>> dataMap) {
        for (Map.Entry<String, List<MesOutPutStatistics>> entry : dataMap.entrySet()) {
            List<MesOutPutStatistics> itemList = entry.getValue();
            MesProdDataToStatisticsOutPut statistics = new MesProdDataToStatisticsOutPut();
            statistics.setDataKey(data.getDataKey());
            statistics.setDataType(data.getStatisticsType());
            statistics.setStatisticsDate(data.getDate());
            statistics.setPid(data.getPid());
            statistics.setPartNo(itemList.get(0).getPartNo());
            statistics.setPartNameRdd(itemList.get(0).getPartNameRdd());
            statistics.setQty(dispatchService.getQty(itemList));
            statistics.setOrganizeCode(data.getOrganizeCode());
            statistics.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            ConvertBean.serviceModelInitialize(statistics, data.getCreateUser());
            statistics.setWorkTime(data.getDate());
            prodDataToStatisticsOutPutRepository.insert(statistics);
        }
    }

}
