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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpOrderJobDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpEquipmentModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpOrderJobModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-14 11:16
 */
@Service
public class EpOrderJobDao implements IEpOrderJobDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<EpOrderJobModel> queryMesOrderJob(EpOrderJobModel orderJob) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new "+ EpOrderJobModel.class.getName()+" ( ")
                .append("moj.id,moj.taskType,moj.orderType,moj.orderNo,moj.workCenterCode,moj.shiftCode,moj.workOrderNo,")
                .append("moj.orderStatus,moj.orderDate,moj.generateTime,moj.complateTime,mwoe.partNo,mwoe.partNameRdd,")
                .append("moj.organizeCode,moj.isValid,moj.isDeleted,moj.createUser,moj.createDatetime,moj.modifyUser,moj.modifyDatetime,detail.equNo as equipmentCode,detail.equName as equipmentName,equip.memo ) ")
                .append(" FROM MesOrderJob AS moj ")
                .append(" LEFT JOIN MesWorkOrderExt mwoe ")
                .append(" ON moj.workOrderNo=mwoe.orderNo ")
                .append(" LEFT JOIN MesEpEquipmentCheckDetail detail ")
                .append(" ON detail.orderJobNo=moj.orderNo ")
                .append(" LEFT JOIN MesEquipment equip ")
                .append(" ON equip.equipmentCode=detail.equNo ")
                .append(" WHERE moj.organizeCode = :organizeCode ")
                .append(" AND moj.isValid = :isValid AND moj.isDeleted = :isDeleted")
                .append(" AND moj.orderStatus in :orderStatus");
        if (!StringUtils.isEmpty(orderJob.getOrderType())) {
            hql.append(" AND moj.orderType = :orderType");
        }
//        if (!StringUtils.isEmpty(orderJob.getTaskType())) {
//            hql.append(" AND moj.taskType = :taskType");
//        }
        if (!StringUtils.isEmpty(orderJob.getWorkCenterCode())) {
            hql.append(" AND moj.workCenterCode = :workCenterCode");
        }
        if (!StringUtils.isEmpty(orderJob.getShiftCode())) {
            hql.append(" AND moj.shiftCode = :shiftCode");
        }
        Query query = entityManager.createQuery(hql.toString(), EpOrderJobModel.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, orderJob.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(orderJob.getOrderType())) {
            query.setParameter(MesPcnExtConstWords.ORDER_TYPE, orderJob.getOrderType());
        }
//        if (!StringUtils.isEmpty(orderJob.getTaskType())) {
//            query.setParameter(MesPcnExtConstWords.TASK_TYPE, orderJob.getTaskType());
//        }
        if (!StringUtils.isEmpty(orderJob.getWorkCenterCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CENTER_CODE, orderJob.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(orderJob.getShiftCode())) {
            query.setParameter(MesPcnExtConstWords.SHIFT_CODE, orderJob.getShiftCode());
        }
        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue());
        orderStatusList.add(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue());
        query.setParameter(MesPcnExtConstWords.ORDER_STATUS, orderStatusList);
        return query.getResultList();
    }

    @Override
    public List<EpOrderJobModel> queryMesOrderJobNoMemo(EpOrderJobModel orderJob) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new "+ EpOrderJobModel.class.getName()+" ( ")
                .append("moj.id,moj.taskType,moj.orderType,moj.orderNo,moj.workCenterCode,moj.shiftCode,moj.workOrderNo,")
                .append("moj.orderStatus,moj.orderDate,moj.generateTime,moj.complateTime,mwoe.partNo,mwoe.partNameRdd,")
                .append("moj.organizeCode,moj.isValid,moj.isDeleted,moj.createUser,moj.createDatetime,moj.modifyUser,moj.modifyDatetime ) ")
                .append(" FROM MesOrderJob AS moj ")
                .append(" LEFT JOIN MesWorkOrderExt mwoe ")
                .append(" ON moj.workOrderNo=mwoe.orderNo ")
//                .append(" LEFT JOIN MesEpEquipmentCheckDetail detail ")
//                .append(" ON detail.orderJobNo = moj.orderNo ")
                .append(" WHERE moj.organizeCode = :organizeCode ")
                .append(" AND moj.isValid = :isValid AND moj.isDeleted = :isDeleted")
                .append(" AND moj.orderStatus in :orderStatus");
        if (!StringUtils.isEmpty(orderJob.getOrderType())) {
            hql.append(" AND moj.orderType = :orderType");
        }
//        if (!StringUtils.isEmpty(orderJob.getTaskType())) {
//            hql.append(" AND moj.taskType = :taskType");
//        }
        if (!StringUtils.isEmpty(orderJob.getWorkCenterCode())) {
            hql.append(" AND moj.workCenterCode = :workCenterCode");
        }
        if (!StringUtils.isEmpty(orderJob.getShiftCode())) {
            hql.append(" AND moj.shiftCode = :shiftCode");
        }
        Query query = entityManager.createQuery(hql.toString(), EpOrderJobModel.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, orderJob.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(orderJob.getOrderType())) {
            query.setParameter(MesPcnExtConstWords.ORDER_TYPE, orderJob.getOrderType());
        }
//        if (!StringUtils.isEmpty(orderJob.getTaskType())) {
//            query.setParameter(MesPcnExtConstWords.TASK_TYPE, orderJob.getTaskType());
//        }
        if (!StringUtils.isEmpty(orderJob.getWorkCenterCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CENTER_CODE, orderJob.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(orderJob.getShiftCode())) {
            query.setParameter(MesPcnExtConstWords.SHIFT_CODE, orderJob.getShiftCode());
        }
        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue());
        orderStatusList.add(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue());
        query.setParameter(MesPcnExtConstWords.ORDER_STATUS, orderStatusList);
        return query.getResultList();
    }

    @Override
    public EpEquipmentModel queryEpEquipmentCheckDetail(String organizeCode,String orderNo) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new "+ EpEquipmentModel.class.getName()+" ( ")
                .append("detail.orderJobNo,equip.equipmentCode as equNo,equip.equipmentName as equName,equip.memo ) ")
                .append(" FROM MesEpEquipmentCheckDetail AS detail ")
                .append(" LEFT JOIN MesEquipment equip ")
                .append(" ON detail.equNo = equip.equipmentCode AND detail.organizeCode = equip.organizeCode")
//                .append(" LEFT JOIN MesEpEquipmentCheckDetail detail ")
//                .append(" ON detail.orderJobNo = moj.orderNo ")
                .append(" WHERE detail.organizeCode = :organizeCode ")
                .append(" AND detail.isValid = :isValid AND equip.isDeleted = :isDeleted")
                .append(" AND detail.isValid = :isValid AND equip.isDeleted = :isDeleted");
        if (!StringUtils.isEmpty(orderNo)) {
            hql.append(" AND detail.orderJobNo = :orderJobNo");
        }

        Query query = entityManager.createQuery(hql.toString(), EpEquipmentModel.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesPcnExtConstWords.IS_VALID, MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(orderNo)) {
            query.setParameter("orderJobNo", orderNo);
        }
//        if (!StringUtils.isEmpty(orderJob.getTaskType())) {
//            query.setParameter(MesPcnExtConstWords.TASK_TYPE, orderJob.getTaskType());
//        }
        return (EpEquipmentModel)query.getSingleResult();
    }
}
