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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IEsopExtDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEsopExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.EsopExtModel;
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;

/**
 * @Author: wangjie
 * @CreateDate: 2019/11/21 11:29 上午
 * @Description:
 **/
@Service
public class EsopExtDao implements IEsopExtDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryEsopByPagerCount(MesEsopExt esopExt) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct ee.id) from MesEsopExt as ee ");
        hql.append(" left join MesFile as f on (ee.fileId = f.id and f.organizeCode = :organizeCode and f.isValid = :isValid1 and f.isDeleted = :isDeleted) ");
        hql.append(" where ee.organizeCode = :organizeCode ");
        hql.append(" and ee.isValid = :isValid ");
        hql.append(" and ee.isDeleted = :isDeleted ");
        hql.append(" and ee.workOrderNo = :workOrderNo ");
        if (!StringUtils.isEmpty(esopExt.getPartNo())) {
            hql.append(" and ee.partNo = :partNo ");
        }
        Query query = entityManager.createQuery(hql.toString(), Long.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, esopExt.getIsValid());
        query.setParameter(MesPcnExtConstWords.IS_VALID1, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, esopExt.getWorkOrderNo());
        if (!StringUtils.isEmpty(esopExt.getPartNo())) {
            query.setParameter(MesPcnExtConstWords.PART_NO, esopExt.getPartNo());
        }
        return ((Long) query.getSingleResult()).intValue();
    }

    @Override
    public List<EsopExtModel> queryEsopByPager(MesEsopExt esopExt, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select distinct new " + EsopExtModel.class.getName() + "(ee.id, ee.workOrderNo, ee.partNo, ee.workCenterCode, ee.workCellCode, ee.processCode, " +
                " ee.fileId, f.fileName, f.fileUrl, f.groupName, ee.fileOriginName, f.fileSize, f.fileType, ee.bcSeq, ee.bcTimes, ee.uploadDatetime, " +
                " ee.organizeCode, ee.isValid, ee.isDeleted, ee.createUser, ee.createDatetime, ee.modifyUser, ee.modifyDatetime) ");
        hql.append(" from MesEsopExt as ee ");
        hql.append(" left join MesFile as f on (ee.fileId = f.id and f.organizeCode = :organizeCode and f.isValid = :isValid1 and f.isDeleted = :isDeleted) ");
        hql.append(" where ee.organizeCode = :organizeCode ");
        hql.append(" and ee.isValid = :isValid ");
        hql.append(" and ee.isDeleted = :isDeleted ");
        hql.append(" and ee.workOrderNo = :workOrderNo ");
        if (!StringUtils.isEmpty(esopExt.getPartNo())) {
            hql.append(" and ee.partNo = :partNo ");
        }
        hql.append(" group by ee.id, ee.workOrderNo, ee.partNo, ee.workCenterCode, ee.workCellCode, ee.processCode, " +
                " ee.fileId, f.fileName, f.fileUrl, f.groupName, ee.fileOriginName, f.fileSize, f.fileType, ee.bcSeq, ee.bcTimes, ee.uploadDatetime, " +
                " ee.organizeCode, ee.isValid, ee.isDeleted, ee.createUser, ee.createDatetime, ee.modifyUser, ee.modifyDatetime ");
        hql.append(" order by ee.modifyDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), EsopExtModel.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, esopExt.getIsValid());
        query.setParameter(MesPcnExtConstWords.IS_VALID1, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, esopExt.getWorkOrderNo());
        if (!StringUtils.isEmpty(esopExt.getPartNo())) {
            query.setParameter(MesPcnExtConstWords.PART_NO, esopExt.getPartNo());
        }
        query.setMaxResults(pager.getPageSize());  //设置获取的数量
        if (pager.getCurrentPage() == 0) {
            pager.setCurrentPage(1);
        }
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return query.getResultList();
    }
}
