package cn.estsh.i3plus.ext.mes.pcn.apiservice.daoimpl;

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IMesProductionRecordDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionRecord;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description : 加工记录
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/28 10:29
 * @Modify:
 **/
@Service
@Slf4j
public class MesProductionRecordImpl implements IMesProductionRecordDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesProductionRecord> findMesProductionRecordNoReport(String organizeCode, Integer workOrderType, Integer pageSize, List<String> workCenterList, boolean isHasOrder) {
        StringBuilder hql = new StringBuilder();
        hql.append(" select record from MesProductionRecord record left join MesWorkCenter center on record.workCenterCode = center.workCenterCode where 1=1");
        hql.append(" and record.organizeCode = :organizeCode and record.isValid = :isValid and record.isDeleted = :isDeleted ");
        hql.append(" and center.organizeCode = :organizeCode and center.isValid = :isValid and center.isDeleted = :isDeleted ");
        hql.append(" and center.centerType = :centerType and record.reportStatus in (:reportStatus) and record.reportType != :reportType ");
        if (!CollectionUtils.isEmpty(workCenterList)) {
            hql.append(" and record.workCenterCode not in (:workCenterList)");
        }
        if(MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue() == workOrderType){
            hql.append(" order by record.reportStatus asc,record.modifyDatetime asc ");
        }else{
            if (isHasOrder) {
                hql.append(" and record.workOrderNo !='' and record.workOrderNo is not null ");
            } else {
                hql.append(" and (record.workOrderNo = '' or  record.workOrderNo is null) ");
            }
            hql.append(" order by record.modifyDatetime asc ");
        }
        Query hqlQuery = entityManager.createQuery(hql.toString(), MesProductionRecord.class);
        hqlQuery.setParameter("organizeCode", organizeCode);
        hqlQuery.setParameter("isValid", CommonEnumUtil.VALID);
        hqlQuery.setParameter("isDeleted", CommonEnumUtil.FALSE);
        hqlQuery.setParameter("centerType", workOrderType);
        hqlQuery.setParameter("reportType", MesExtEnumUtil.MES_REPORT_TYPE.NO_REPORT.getValue());
        hqlQuery.setParameter("reportStatus", Stream.of(MesExtEnumUtil.REPORT_STATUS.REPORT_STATUS_10.getValue(), MesExtEnumUtil.REPORT_STATUS.REPORT_STATUS_40.getValue()).collect(Collectors.toList()));
        if (!CollectionUtils.isEmpty(workCenterList)) {
            hqlQuery.setParameter("workCenterList",workCenterList);
        }
        hqlQuery.setFirstResult(MesPcnExtConstWords.ZERO).setMaxResults(Objects.isNull(pageSize) ? MesPcnExtConstWords.ONE_HUNDRED : pageSize);
        return hqlQuery.getResultList();
    }
}
