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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesWorkCellScanMonitorLogService;
import cn.estsh.i3plus.pojo.mes.repository.MesProductionRecordRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCellScanMonitorLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import cn.estsh.i3plus.platform.common.convert.ConvertBean;
//import cn.estsh.i3plus.platform.common.tool.TimeTool;
//import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
//import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
//import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
//import cn.estsh.i3plus.pojo.mes.bean.MesProductionRecord;
//import cn.estsh.i3plus.pojo.mes.bean.MesWorkCellScanMonitorLog;
//import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
//import com.netflix.discovery.converters.Auto;

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

/**
 * @Description :
 * @Reference :
 * @Author : Castle
 * @CreateDate : 2024/6/30 16:57
 * @Modify:
 **/
@Service
public class MesWorkCellScanMonitorLogServiceImpl implements IMesWorkCellScanMonitorLogService {
    @Autowired
    private MesWorkCellScanMonitorLogRepository monitorLogRao;

    @Autowired
    private MesProductionRecordRepository productionRecordRao;

    /**
     * 根据 MesWorkCellScanMonitorLog 表中 mouldRecordId 开模id 有数据 才去补录
     * 补录数据 是根据MesProductionRecord 中的 开模id相匹配去做
     *
     * @param pageSize
     * @param organizeCode
     */
    @Override
    public void doRecordCellOperationLog(String organizeCode, Integer pageSize, Integer times) {
//        //查询serviceFlag是未处理状态,mouldRecordId不为空的记录，每次查询pageSize 默认50条
//        DdlPackBean logPackBean = DdlPackBean.getDdlPackBean(organizeCode);
//        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.MES_LOG_DEAL_STATUS.UNDEAL.getValue(), "dealStatus", logPackBean);
//        DdlPreparedPack.getNumberSmallerEqualPack(times, "times", logPackBean);
//        DdlPreparedPack.getIsNotNull("mouldRecordId", logPackBean);
//        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{"createDatetime"}, logPackBean);
//        List<MesWorkCellScanMonitorLog> monitorLogs = monitorLogRao.findByHqlTopWhere(logPackBean, pageSize);
//
//        if (!monitorLogs.isEmpty()) {
//            List<Long> mouldRecordIdList = monitorLogs.stream().map(MesWorkCellScanMonitorLog::getMouldRecordId).distinct().collect(Collectors.toList());
//            Map<Long, List<MesWorkCellScanMonitorLog>> logs = monitorLogs.stream().collect(Collectors.groupingBy(MesWorkCellScanMonitorLog::getMouldRecordId));
//            for (Long mouldRecordId : mouldRecordIdList) {
//                DdlPackBean recordPackBean = DdlPackBean.getDdlPackBean(organizeCode);
//                DdlPreparedPack.getNumEqualPack(mouldRecordId, "mouldRecordId", recordPackBean);
//
//                List<MesProductionRecord> recordList = productionRecordRao.findByHqlWhere(recordPackBean);
//                //需要补录如下字段
//                DdlPackBean monitorLogPackBean = DdlPackBean.getDdlPackBean(organizeCode);
//                DdlPreparedPack.getNumEqualPack(mouldRecordId, "mouldRecordId", monitorLogPackBean);
//                int newTimes = logs.get(mouldRecordId).get(0).getTimes() + 1;
//                if (!recordList.isEmpty()) {
//                    String workOrderNoStr = recordList.stream().map(MesProductionRecord::getWorkOrderNo).collect(Collectors.joining(","));
//                    String serialNoStr = recordList.stream().map(MesProductionRecord::getSerialNumber).collect(Collectors.joining(","));
//                    String productSnStr = recordList.stream().map(MesProductionRecord::getProductSn).collect(Collectors.joining(","));
//                    String custSnStr = recordList.stream().map(MesProductionRecord::getCustSn).collect(Collectors.joining(","));
//                    String partNoStr = recordList.stream().map(MesProductionRecord::getPartNo).collect(Collectors.joining(","));
//                    String partNameStr = recordList.stream().map(MesProductionRecord::getPartName).collect(Collectors.joining(","));
//                    monitorLogRao.updateByProperties(new String[]{"workOrderNo", "serialNumber", "productSn", "custSn", "partNo", "partName", "modifyDatetime", "modifyUser", "dealStatus", "times"},
//                            new Object[]{workOrderNoStr, serialNoStr, productSnStr, custSnStr, partNoStr, partNameStr, TimeTool.getNowTime(true), "JOB", MesExtEnumUtil.MES_LOG_DEAL_STATUS.DEAL_SUCCESS.getValue(), newTimes}, monitorLogPackBean);
//                } else {
//                    if (newTimes > times) {
//                        monitorLogRao.updateByProperties(new String[]{"modifyDatetime", "modifyUser", "times"}, new Object[]{TimeTool.getNowTime(true), "JOB", newTimes}, monitorLogPackBean);
//                    } else {
//                        monitorLogRao.updateByProperties(new String[]{"modifyDatetime", "modifyUser", "times", "dealStatus"}, new Object[]{TimeTool.getNowTime(true), "JOB", newTimes, MesExtEnumUtil.MES_LOG_DEAL_STATUS.DEAL_FAILURE.getValue()}, monitorLogPackBean);
//                    }
//                }
//            }
//        }

    }
}
