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

import cn.estsh.i3plus.ext.mes.apiservice.dao.ep.IEpMesOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpOutPutStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-04-06 16:12
 */
@Service
public class EpMesOutPutStatisticsDao implements IEpMesOutPutStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<EpOutPutStatisticsModel> getMesOutPutStatisticsByTimeAndWorkOrderType(String organizeCode, String startTime , String endTime, Integer workOrderType) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + EpOutPutStatisticsModel.class.getName() +
                "( ops.workCenterCode, ops.shiftCode, ops.workTime, ops.workOrderNo, ops.partNo, " +
                " ops.partNameRdd, ops.startTime, ops.endTime, ops.orderStartTime, ops.qty, ops.planQty, ops.remark, wo.completeQty, wo.modifyDatetime, wo.workOrderStatus )");

        packHqlForQuery(hql, startTime, endTime, workOrderType);
        Query query = entityManager.createQuery(hql.toString(), EpOutPutStatisticsModel.class);
        packParameterForQuery(query, organizeCode, startTime, endTime, workOrderType);
        return query.getResultList();
    }

    @Override
    public List<EpOutPutStatisticsModel> queryOutPutStatisticsByTimeAndIsOqc(String organizeCode, String startTime, String endTime, Integer workOrderType) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + EpOutPutStatisticsModel.class.getName() +
                "( ops.workCenterCode, ops.shiftCode, ops.workTime, ops.workOrderNo, ops.partNo, " +
                " ops.partNameRdd, ops.startTime, ops.endTime, ops.orderStartTime, ops.qty, ops.planQty, ops.remark, wo.completeQty, wo.modifyDatetime, wo.workOrderStatus )");

        packHqlForIsOqcQuery(hql, startTime, endTime, workOrderType);
        Query query = entityManager.createQuery(hql.toString(), EpOutPutStatisticsModel.class);
        packParameterForIsOqcQuery(query, organizeCode, startTime, endTime, workOrderType);
        return query.getResultList();
    }

    private void packHqlForQuery(StringBuffer hql, String startTime, String endTime, Integer workType) {
        hql.append(" from MesOutPutStatistics as ops ");
        hql.append(" left join MesWorkOrder as wo on (wo.orderNo = ops.workOrderNo and wo.organizeCode = :organizeCode and wo.isValid = :isValid and wo.isDeleted = :isDeleted) ");
        hql.append(" where ops.organizeCode = :organizeCode ");
        hql.append(" and ops.isValid = :isValid ");
        hql.append(" and ops.isDeleted = :isDeleted ");
        if (workType != null) {
            hql.append(" and wo.workOrderType = :workOrderType ");
        }
        if (!StringUtils.isEmpty(startTime)) {
            hql.append(" and ops.workTime >= :startTime ");
        }
        if(!StringUtils.isEmpty(endTime)){
            hql.append(" and ops.workTime < :endTime ");
        }
    }

    private void packHqlForIsOqcQuery(StringBuffer hql, String startTime, String endTime, Integer workType) {
        hql.append(" from MesOutPutStatistics as ops ");
        hql.append(" left join MesWorkOrder as wo on (wo.orderNo = ops.workOrderNo and wo.organizeCode = :organizeCode and wo.isValid = :isValid and wo.isDeleted = :isDeleted) ");
        hql.append(" left join MesPartExt as pt on (pt.partNoExt = ops.partNo and pt.organizeCode = :organizeCode and pt.isValid = :isValid and pt.isDeleted = :isDeleted) ");
        hql.append(" where ops.organizeCode = :organizeCode ");
        hql.append(" and ops.isValid = :isValid ");
        hql.append(" and ops.isDeleted = :isDeleted ");
        hql.append(" and pt.isOqc = :isOqc ");
        if (workType != null) {
            hql.append(" and wo.workOrderType != :workOrderType ");
        }
        if (!StringUtils.isEmpty(startTime)) {
            hql.append(" and ops.workTime >= :startTime ");
        }
        if(!StringUtils.isEmpty(endTime)){
            hql.append(" and ops.workTime < :endTime ");
        }
    }

    private void packParameterForQuery(Query query, String organizeCode,String startTime, String endTime, Integer workOrderType) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(workOrderType)) {
            query.setParameter(MesExtConstWords.WORK_ORDER_TYPE, workOrderType );
        }
        if (!StringUtils.isEmpty(startTime)) {
            query.setParameter(MesExtConstWords.START_TIME, startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            query.setParameter(MesExtConstWords.END_TIME, endTime);
        }
    }

    private void packParameterForIsOqcQuery(Query query, String organizeCode,String startTime, String endTime, Integer workOrderType) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesExtConstWords.IS_OQC, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        if (!StringUtils.isEmpty(workOrderType)) {
            query.setParameter(MesExtConstWords.WORK_ORDER_TYPE, workOrderType );
        }
        if (!StringUtils.isEmpty(startTime)) {
            query.setParameter(MesExtConstWords.START_TIME, startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            query.setParameter(MesExtConstWords.END_TIME, endTime);
        }
    }
}
