package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.job;

import cn.estsh.i3plus.ext.mes.pcn.api.job.IMesWorkOrderCalcQtyRestoreDispatchService;
import cn.estsh.i3plus.ext.mes.pcn.api.job.IMesWorkOrderCalcQtyRestoreService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrderCalcQtyRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Slf4j
@Service
public class MesWorkOrderCalcQtyRestoreService implements IMesWorkOrderCalcQtyRestoreService {

    @Autowired
    private IMesWorkOrderCalcQtyRestoreDispatchService workOrderCalcQtyRestoreDispatchService;

    @Override
    public void doRestoreWorkOrderCalcQty(Map<String, String> paramMap) {

        //参数 工厂
        String organizeCode = paramMap.get(MesPcnExtConstWords.ORGANIZE_CODE);

        //参数 处理数据量
        Integer qty = paramMap.containsKey(MesPcnExtConstWords.QTY) ? Integer.valueOf(paramMap.get(MesPcnExtConstWords.QTY)) : MesPcnExtConstWords.ONE_HUNDRED;

        //生产工单计数记录表
        List<MesWorkOrderCalcQtyRecord> workOrderCalcQtyRecordList = workOrderCalcQtyRestoreDispatchService.getWorkOrderCalcQtyRecordList(organizeCode, qty);

        Map<String, List<MesWorkOrderCalcQtyRecord>> workOrderNoMap = CollectionUtils.isEmpty(workOrderCalcQtyRecordList) ? null :
                workOrderCalcQtyRecordList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesWorkOrderCalcQtyRecord::getWorkOrderNo));

        log.info("补录生产工单计数JOB --- 查询到未补录完成的记录 {} 条 --- 生产工单包含: {} ---",
                CollectionUtils.isEmpty(workOrderCalcQtyRecordList) ? "[]" : workOrderCalcQtyRecordList.size(),
                CollectionUtils.isEmpty(workOrderNoMap) ? "[]" : workOrderNoMap.keySet().toString());

        if (CollectionUtils.isEmpty(workOrderNoMap)) return;

        for (Map.Entry<String, List<MesWorkOrderCalcQtyRecord>> entry : workOrderNoMap.entrySet()) {

            if (null == entry) continue;

            //查询工单, 无事务查询
            MesWorkOrder workOrder = workOrderCalcQtyRestoreDispatchService.getWorkOrderDb(organizeCode, entry.getValue().get(0).getPid());

            if (null == workOrder) log.info("补录生产工单计数JOB --- 补录: {} --- ID: {} --- START --- 信息不存在 ---", entry.getKey(), entry.getValue().get(0).getPid());
            else log.info("补录生产工单计数JOB --- 补录: {} --- ID: {} --- START ---", entry.getKey(), entry.getValue().get(0).getPid());

            try {
                workOrderCalcQtyRestoreDispatchService.doRestoreWorkOrderCalcQty(organizeCode, workOrder, entry.getValue());
            } catch (Exception e) {
                log.info("补录生产工单计数JOB --- 补录: {} --- ID: {} --- EXCEPTION: {} ---", entry.getKey(), entry.getValue().get(0).getPid(), e.toString());
            }

        }

    }

}
