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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IPartCheckDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.PartCheckDetailModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ProduceSnRepairModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.pojo.base.common.Pager;
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;

/**
 * 文件描述 物料点检明细
 *
 * @author xiangming.liao
 * @date 2021年09月06日 9:56
 */
@Service
public class PartCheckDetailDao implements IPartCheckDetailDao {

    @Autowired
    private EntityManager entityManager;


    @Override
    public int queryPartCheckDetailByPagerCount(MesPartCheckDetail detail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct p.id) ");

        packHqlForQueryPartCheckDetail(hql, detail);

        Query query = entityManager.createQuery(hql.toString(), Long.class);

        packParameterForQueryPartCheckDetail(query, detail);

        return ((Long) query.getSingleResult()).intValue();
    }

    @Override
    public List<PartCheckDetailModel> queryPartCheckDetailByPager(MesPartCheckDetail detail, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new " + PartCheckDetailModel.class.getName() + "(p.id,p.workOrderNo,p.workCenterCode,p.workCellCode," +
                " p.processCode,p.processName,p.itemPartNo,p.itemPartName,p.shiftCode,p.partNo,p.partNameRdd,p.orderNo,p.orderStatus," +
                " p.orderDate,p.checkUser,p.checkTime,p.shiftGroupCheck,p.shiftGroupCheckUser,p.shiftGroupCheckTime,p.qualityCheck," +
                " p.qualityCheckUser,p.qualityCheckTime,p.generateTime,p.complateTime,p.organizeCode,p.createUser,p.createDatetime," +
                " p.modifyUser,p.modifyDatetime)");

        packHqlForQueryPartCheckDetail(hql, detail);
        hql.append(" order by p.checkTime desc");

        Query query = entityManager.createQuery(hql.toString(), PartCheckDetailModel.class);

        packParameterForQueryPartCheckDetail(query, detail);

        query.setMaxResults(pager.getPageSize());
        if (pager.getCurrentPage() == 0) {
            pager.setCurrentPage(1);
        }
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return query.getResultList();
    }

    private void packHqlForQueryPartCheckDetail(StringBuffer hql, MesPartCheckDetail detail) {
        hql.append(" from MesPartCheckDetail as p ");
        hql.append(" inner join MesOrderJob as j on (p.orderNo = j.orderNo and p.organizeCode = :organizeCode and p.isValid = :isValid and p.isDeleted = :isDeleted) ");
        hql.append(" where j.organizeCode = :organizeCode ");
        hql.append(" and j.isValid = :isValid ");
        hql.append(" and j.isDeleted = :isDeleted ");
        hql.append(" and j.orderStatus = :orderStatus ");

        if (!StringUtils.isEmpty(detail.getWorkOrderNo())) {
            hql.append(" and p.workOrderNo like :workOrderNo ");
        }
        if (!StringUtils.isEmpty(detail.getProcessCode())) {
            hql.append(" and p.processCode like :processCode ");
        }
        if (!StringUtils.isEmpty(detail.getWorkCenterCode())) {
            hql.append(" and p.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(detail.getWorkCellCode())) {
            hql.append(" and p.workCellCode = :workCellCode ");
        }
        if (!StringUtils.isEmpty(detail.getShiftCode())) {
            hql.append(" and p.shiftCode like :shiftCode ");
        }
        if (!StringUtils.isEmpty(detail.getPartNo())) {
            hql.append(" and p.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(detail.getCheckStartTime()) && !StringUtils.isEmpty(detail.getCheckEndTime())) {
            hql.append(" and p.checkTime >= :checkStartTime");
            hql.append(" and p.checkTime <= :checkEndTime");
        }

        if (!StringUtils.isEmpty(detail.getShiftGroupCheck())) {
            hql.append(" and p.shiftGroupCheck = :shiftGroupCheck ");
        }
        if (!StringUtils.isEmpty(detail.getQualityCheck())) {
            hql.append(" and p.qualityCheck = :qualityCheck ");
        }
        if (!StringUtils.isEmpty(detail.getOrderNo())) {
            hql.append(" and j.orderNo = :orderNo ");
        }
    }


    private void packParameterForQueryPartCheckDetail(Query query, MesPartCheckDetail detail) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, detail.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesExtConstWords.ORDER_STATUS, MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        if (!StringUtils.isEmpty(detail.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, "%" + detail.getWorkOrderNo() + "%");
        }
        if (!StringUtils.isEmpty(detail.getProcessCode())) {
            query.setParameter(MesExtConstWords.PROCESS_CODE, "%" + detail.getProcessCode() + "%");
        }
        if (!StringUtils.isEmpty(detail.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, detail.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(detail.getWorkCellCode())) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, detail.getWorkCellCode());
        }
        if (!StringUtils.isEmpty(detail.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, "%" + detail.getShiftCode() + "%");
        }
        if (!StringUtils.isEmpty(detail.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, detail.getPartNo());
        }
        if (!StringUtils.isEmpty(detail.getCheckStartTime()) && !StringUtils.isEmpty(detail.getCheckEndTime())) {
            query.setParameter(MesExtConstWords.CHECK_START_TIME, detail.getCheckStartTime());
            query.setParameter(MesExtConstWords.CHECK_END_TIME, detail.getCheckEndTime());
        }
        if (!StringUtils.isEmpty(detail.getShiftGroupCheck())) {
            query.setParameter(MesExtConstWords.SHIFT_GROUP_CHECK, detail.getShiftGroupCheck());
        }
        if (!StringUtils.isEmpty(detail.getQualityCheck())) {
            query.setParameter(MesExtConstWords.QUALITY_CHECK, detail.getQualityCheck());
        }
        if (!StringUtils.isEmpty(detail.getOrderNo())) {
            query.setParameter(MesExtConstWords.ORDER_NO, detail.getOrderNo());
        }
    }
}
