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


import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IPartCheckDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartCheckDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.PartCheckDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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;

/**
 * @Description : MES_PCN物料明细Dao
 * @Reference :
 * @Author : xiangming.liao
 * @CreateDate : 2020-02-21
 * @Modify:
 **/
@Service
public class PartCheckDao implements IPartCheckDao {

    @Autowired
    private EntityManager entityManager;


    @Override
    public int queryPartCheckByPagerCount(MesPartCheckDetail partCheckDetail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(pcd.id) ");

        packHqlForQueryPartCheck(hql, partCheckDetail);

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

        packParameterForQueryPartCheck(query, partCheckDetail);

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

    private void packHqlForQueryPartCheck(StringBuffer hql, MesPartCheckDetail partCheckDetail) {
        hql.append(" from MesPartCheckDetail as pcd ");
        hql.append(" left join MesPartExt as pe on (pcd.itemPartNo = pe.partNo and pe.organizeCode = :organizeCode and pe.isValid = :isValid and pe.isDeleted = :isDeleted) ");
        hql.append(" left join MesFile as f on (pe.fileId = f.id and f.organizeCode = :organizeCode and f.isValid = :isValid and f.isDeleted = :isDeleted) ");
        hql.append(" where pcd.organizeCode = :organizeCode ");
        hql.append(" and pcd.isValid = :isValid ");
        hql.append(" and pcd.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(partCheckDetail.getOrderNo())) {
            hql.append(" and pcd.orderNo = :orderNo ");
        }
        if (!StringUtils.isEmpty(partCheckDetail.getWorkCellCode())) {
            hql.append(" and pcd.workCellCode = :workCellCode ");
        }
    }

    private void packParameterForQueryPartCheck(Query query, MesPartCheckDetail partCheckDetail) {
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, partCheckDetail.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, partCheckDetail.getIsValid());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(partCheckDetail.getOrderNo())) {
            query.setParameter(MesPcnExtConstWords.ORDER_NO, partCheckDetail.getOrderNo());
        }
        if (!StringUtils.isEmpty(partCheckDetail.getWorkCellCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CELL_CODE, partCheckDetail.getWorkCellCode());
        }
    }

    @Override
    public List<PartCheckDetailModel> queryPartCheckByPager(MesPartCheckDetail partCheckDetail, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + PartCheckDetailModel.class.getName() + "(pcd.id, pcd.workOrderNo, pcd.workCenterCode," +
                " pcd.workCellCode,pcd.processCode, pcd.processName, pcd.itemPartNo, pcd.itemPartName, pcd.shiftCode," +
                " pcd.partNo,pcd.partNameRdd, pcd.orderNo, pcd.orderStatus, pcd.orderDate, pcd.checkUser," +
                " pcd.checkTime,pcd.shiftGroupCheck, pcd.shiftGroupCheckUser, pcd.shiftGroupCheckTime, pcd.qualityCheck, pcd.qualityCheckUser," +
                " pcd.qualityCheckTime,pcd.generateTime, pcd.complateTime, pe.fileId, f.fileName, f.fileUrl," +
                " f.groupName,f.fileOriginName, f.fileSize, f.fileType," +
                " pcd.organizeCode, pcd.isValid, pcd.isDeleted, pcd.createUser, pcd.createDatetime, pcd.modifyUser, pcd.modifyDatetime) ");

        packHqlForQueryPartCheck(hql, partCheckDetail);

        hql.append(" order by pcd.modifyDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), PartCheckDetailModel.class);

        packParameterForQueryPartCheck(query, partCheckDetail);

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