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

import cn.estsh.i3plus.ext.mes.pcn.api.job.ISxGenPrintRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCellPrintRecord;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCellPrintRecordDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.PrintRecordModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCellPrintRecordDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCellPrintRecordRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import com.alibaba.fastjson.JSONObject;
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;

/**
 * @Author: wangjie
 * @CreateDate: 2019/8/23 1:16 PM
 * @Description: Pcn处理打印记录JOB
 **/
@Slf4j
@Service
public class SxGenPrintRecordService implements ISxGenPrintRecordService {

    @Autowired
    private MesWorkCellPrintRecordRepository workCellPrintRecordRepository;

    @Autowired
    private MesWorkCellPrintRecordDetailRepository workCellPrintRecordDetailRepository;

    @Override
    public void doGenPrintRecord(String organizeCode) {

        List<MesWorkCellPrintRecord> workCellPrintRecordList = workCellPrintRecordRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.HANDLE_STATUS},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()},
                " order by createDatetime asc ");

        if (CollectionUtils.isEmpty(workCellPrintRecordList)) {
            log.info("Pcn处理打印记录JOB,未获取到打印记录");
            return;
        }

        if (workCellPrintRecordList.size() > 10) {
            workCellPrintRecordList = workCellPrintRecordList.subList(0, 10);
        }

        Boolean flag = false;
        for (MesWorkCellPrintRecord workCellPrintRecord : workCellPrintRecordList) {
            if (null == workCellPrintRecord) {
                continue;
            }

            List<PrintRecordModel> printRecordModelList = JSONObject.parseArray(workCellPrintRecord.getPrintIds(), PrintRecordModel.class);
            if (CollectionUtils.isEmpty(printRecordModelList)) {
                log.info("Pcn处理打印记录JOB,获取到的打印记录数据异常:ID[{}]", workCellPrintRecord.getId());
                workCellPrintRecord.setHandleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
                ConvertBean.serviceModelUpdate(workCellPrintRecord, MesPcnExtConstWords.JOB);
                workCellPrintRecordRepository.save(workCellPrintRecord);
                continue;
            }

            if (flag && printRecordModelList.size() >= 50) {
                return;
            }

            log.info("Pcn处理打印记录JOB,获取到打印记录数据,即将执行插入打印记录明细:ID[{}]", workCellPrintRecord.getId());
            printRecordModelList.stream().forEach(o -> {
                MesWorkCellPrintRecordDetail workCellPrintRecordDetail = new MesWorkCellPrintRecordDetail();
                workCellPrintRecordDetail.setObjectCode(workCellPrintRecord.getObjectCode());
                workCellPrintRecordDetail.setObjectName(workCellPrintRecord.getObjectName());
                workCellPrintRecordDetail.setSourceModule(workCellPrintRecord.getSourceModule());
                workCellPrintRecordDetail.setPid(o.getPid());
                workCellPrintRecordDetail.setSn(o.getSn());
                workCellPrintRecordDetail.setTagType(workCellPrintRecord.getTagType());
                workCellPrintRecordDetail.setWorkCenterCode(workCellPrintRecord.getWorkCenterCode());
                workCellPrintRecordDetail.setWorkOrderNo(workCellPrintRecord.getWorkOrderNo());
                workCellPrintRecordDetail.setPartNo(workCellPrintRecord.getPartNo());
                workCellPrintRecordDetail.setPartNameRdd(workCellPrintRecord.getPartNameRdd());
                workCellPrintRecordDetail.setOrganizeCode(workCellPrintRecord.getOrganizeCode());
                ConvertBean.serviceModelInitialize(workCellPrintRecordDetail, workCellPrintRecord.getCreateUser());
                workCellPrintRecordDetail.setPrintDatetime(workCellPrintRecord.getCreateDatetime());
                workCellPrintRecordDetailRepository.insert(workCellPrintRecordDetail);
            });

            workCellPrintRecord.setHandleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
            ConvertBean.serviceModelUpdate(workCellPrintRecord, MesPcnExtConstWords.JOB);
            workCellPrintRecordRepository.save(workCellPrintRecord);
            log.info("Pcn处理打印记录JOB,获取到打印记录数据,完成插入打印记录明细:ID[{}]", workCellPrintRecord.getId());
            flag = true;
        }

    }
}
