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


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

        packHqlForQueryEsop(hql, esopExt);

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

        packParameterForQueryEsop(query, esopExt);

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

    @Override
    public List<EsopExtModel> queryEsopByPager(MesEsopExt esopExt, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + EsopExtModel.class.getName() + "(ee.id, 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) ");

        packHqlForQueryEsop(hql, esopExt);

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

        packParameterForQueryEsop(query, esopExt);

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

    private void packHqlForQueryEsop(StringBuffer hql, MesEsopExt esopExt) {
        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 = '' or ee.workOrderNo is null) ");
        if (!StringUtils.isEmpty(esopExt.getProcessCode())) {
            hql.append(" and ee.processCode like :processCode ");
        }
        if (!StringUtils.isEmpty(esopExt.getPartNo())) {
            hql.append(" and ee.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(esopExt.getWorkCenterCode())) {
            hql.append(" and ee.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(esopExt.getWorkCellCode())) {
            hql.append(" and ee.workCellCode = :workCellCode ");
        }
    }

    private void packParameterForQueryEsop(Query query, MesEsopExt esopExt) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, esopExt.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, esopExt.getIsValid());
        query.setParameter(MesExtConstWords.IS_VALID1, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(esopExt.getProcessCode())) {
            query.setParameter(MesExtConstWords.PROCESS_CODE, "%" + esopExt.getProcessCode() + "%" );
        }
        if (!StringUtils.isEmpty(esopExt.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, esopExt.getPartNo());
        }
        if (!StringUtils.isEmpty(esopExt.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, esopExt.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(esopExt.getWorkCellCode())) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, esopExt.getWorkCellCode());
        }
    }
}
