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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IOrderJobDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesOrderJobModel;
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.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/22 17:36
 * @Description:
 **/
@Service
public class OrderJobDao implements IOrderJobDao {
    public static final Logger LOGGER = LoggerFactory.getLogger(OrderJobDao.class);

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesOrderJobModel> queryMesOrderJob(MesOrderJobModel orderJob) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new "+ MesOrderJobModel.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, moj.serialNumber)")
                .append(" FROM MesOrderJob AS moj ")
                .append(" LEFT JOIN MesWorkOrderExt mwoe ")
                .append(" ON moj.workOrderNo=mwoe.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(), MesOrderJobModel.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 int queryMesOrderJobCount(MesOrderJobModel orderJob) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT COUNT(1)")
                .append(" FROM MesOrderJob AS moj ")
                .append(" WHERE moj.organizeCode = :organizeCode ")
                .append(" AND moj.isValid = :isValid AND moj.isDeleted = :isDeleted");
        if (!StringUtils.isEmpty(orderJob.getOrderStatus())) {
            hql.append(" AND moj.orderStatus = :orderStatus");
        } else {
            hql.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");
        }
        if (!StringUtils.isEmpty(orderJob.getWorkOrderNo())) {
            hql.append(" AND moj.workOrderNo = :workOrderNo");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderNo())) {
            hql.append(" AND moj.orderNo = :orderNo");
        }
        if (!StringUtils.isEmpty(orderJob.getWorkCellCode())) {
            hql.append(" AND moj.cellCollect like :workCellCode");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateStart())) {
            hql.append(" and moj.orderDate >= :orderDateStart ");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateEnd())) {
            hql.append(" and moj.orderDate <= :orderDateEnd ");
        }
        Query query = entityManager.createQuery(hql.toString(), Long.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());
        }
        if (!StringUtils.isEmpty(orderJob.getWorkOrderNo())) {
            query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, orderJob.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(orderJob.getOrderNo())) {
            query.setParameter(MesPcnExtConstWords.ORDER_NO, orderJob.getOrderNo());
        }
        if (!StringUtils.isEmpty(orderJob.getWorkCellCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CELL_CODE, "%" + orderJob.getWorkCellCode() + "%");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateStart())) {
            query.setParameter(MesPcnExtConstWords.ORDER_DATE_START, orderJob.getOrderDateStart());
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateEnd())) {
            query.setParameter(MesPcnExtConstWords.ORDER_DATE_END, orderJob.getOrderDateEnd());
        }
        if (!StringUtils.isEmpty(orderJob.getOrderStatus())) {
            query.setParameter(MesPcnExtConstWords.ORDER_STATUS, orderJob.getOrderStatus());
        } else {
            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 ((Long) query.getSingleResult()).intValue();
    }

    @Override
    public List<MesOrderJobModel> queryMesOrderJob(MesOrderJobModel orderJob,Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new "+ MesOrderJobModel.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,")
                .append("moj.organizeCode,moj.isValid,moj.isDeleted,moj.createUser,moj.createDatetime,moj.modifyUser,moj.modifyDatetime)")
                .append(" FROM MesOrderJob AS moj ")
                .append(" WHERE moj.organizeCode = :organizeCode ")
                .append(" AND moj.isValid = :isValid AND moj.isDeleted = :isDeleted");
        if (!StringUtils.isEmpty(orderJob.getOrderStatus())) {
            hql.append(" AND moj.orderStatus = :orderStatus");
        } else {
            hql.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");
        }
        if (!StringUtils.isEmpty(orderJob.getWorkOrderNo())) {
            hql.append(" AND moj.workOrderNo = :workOrderNo");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderNo())) {
            hql.append(" AND moj.orderNo = :orderNo");
        }
        if (!StringUtils.isEmpty(orderJob.getWorkCellCode())) {
            hql.append(" AND moj.cellCollect like :workCellCode");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateStart())) {
            hql.append(" and moj.orderDate >= :orderDateStart ");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateEnd())) {
            hql.append(" and moj.orderDate <= :orderDateEnd ");
        }
        Query query = entityManager.createQuery(hql.toString(), MesOrderJobModel.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());
        }
        if (!StringUtils.isEmpty(orderJob.getWorkOrderNo())) {
            query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, orderJob.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(orderJob.getOrderNo())) {
            query.setParameter(MesPcnExtConstWords.ORDER_NO, orderJob.getOrderNo());
        }
        if (!StringUtils.isEmpty(orderJob.getWorkCellCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CELL_CODE, "%" + orderJob.getWorkCellCode() + "%");
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateStart())) {
            query.setParameter(MesPcnExtConstWords.ORDER_DATE_START, orderJob.getOrderDateStart());
        }
        if (!StringUtils.isEmpty(orderJob.getOrderDateEnd())) {
            query.setParameter(MesPcnExtConstWords.ORDER_DATE_END, orderJob.getOrderDateEnd());
        }
        if (!StringUtils.isEmpty(orderJob.getOrderStatus())) {
            query.setParameter(MesPcnExtConstWords.ORDER_STATUS, orderJob.getOrderStatus());
        } else {
            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);
        }
        query.setMaxResults(pager.getPageSize());
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return query.getResultList();
    }
}
