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


import cn.estsh.i3plus.ext.mes.apiservice.dao.ep.IEpPartExtDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpPartExtModel;
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;

/**
 * @Description : MES物料信息Dao
 * @Reference :
 * @Author : xiangming.liao
 * @CreateDate : 2020-02-21
 * @Modify:
 **/
@Service
public class EpPartExtDao implements IEpPartExtDao {

    @Autowired
    private EntityManager entityManager;


    @Override
    public int queryPartByPagerCount(MesPartExt partExt) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(pe.id) ");

        packHqlForQueryPart(hql, partExt);

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

        packParameterForQueryPart(query, partExt);

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

    private void packHqlForQueryPart(StringBuffer hql, MesPartExt partExt) {
        hql.append(" from MesPartExt as pe ");
        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 pe.organizeCode = :organizeCode ");
        hql.append(" and pe.isValid = :isValid ");
        hql.append(" and pe.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(partExt.getPartNo())) {
            hql.append(" and pe.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(partExt.getPartName())) {
            hql.append(" and pe.partName like :partName ");
        }
        if (!StringUtils.isEmpty(partExt.getCategoryCode1())) {
            hql.append(" and pe.categoryCode1 = :categoryCode1 ");
        }
        if (!StringUtils.isEmpty(partExt.getCategoryCode2())) {
            hql.append(" and pe.categoryCode2 = :categoryCode2 ");
        }
        if (!StringUtils.isEmpty(partExt.getCategoryCode3())) {
            hql.append(" and pe.categoryCode3 = :categoryCode3 ");
        }
    }
    private void packParameterForQueryPart(Query query, MesPartExt partExt) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, partExt.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, partExt.getIsValid());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(partExt.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, partExt.getPartNo());
        }
        if (!StringUtils.isEmpty(partExt.getPartName())) {
            query.setParameter(MesExtConstWords.PART_NAME, "%" + partExt.getPartName() + "%");
        }
        if (!StringUtils.isEmpty(partExt.getCategoryCode1())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE1, partExt.getCategoryCode1());
        }
        if (!StringUtils.isEmpty(partExt.getCategoryCode2())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE2, partExt.getCategoryCode2());
        }
        if (!StringUtils.isEmpty(partExt.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, partExt.getCategoryCode3());
        }
    }

    @Override
    public List<EpPartExtModel> queryPartByPager(MesPartExt partExt, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + EpPartExtModel.class.getName() + "(pe.id, pe.partNo, pe.partName, pe.briefText, pe.unit, " +
                " pe.categoryCode1, pe.categoryCode2, pe.categoryCode3, pe.processMatchType, pe.packageMatchType, pe.qty2, " +
                " pe.pptCode, pe.colorCode, pe.colorName, pe.salesTo, pe.isOqc, pe.isCreateSn, pe.isPSParts, pe.standardWork, " +
                " pe.japanDesc, pe.goodsCode, pe.prefix, pe.qualityAssureCode, pe.identifyCode, pe.snLastCode, pe.fileId, " +
                " pe.fileOriginName, pe.uploadDatetime, pe.organizeCode, pe.createUser, pe.createDatetime, pe.modifyUser, pe.modifyDatetime, f.fileUrl , pe.partLotNoLength) ");
        packHqlForQueryPart(hql, partExt);

        hql.append(" group by pe.id, pe.partNo, pe.partName, pe.briefText, pe.unit, " +
                " pe.categoryCode1, pe.categoryCode2, pe.categoryCode3, pe.processMatchType, pe.packageMatchType, pe.qty2, " +
                " pe.pptCode, pe.colorCode, pe.colorName, pe.salesTo, pe.isOqc, pe.isCreateSn, pe.isPSParts, pe.standardWork, " +
                " pe.japanDesc, pe.goodsCode, pe.prefix, pe.qualityAssureCode, pe.identifyCode, pe.snLastCode, pe.fileId, " +
                " pe.fileOriginName, pe.uploadDatetime, pe.organizeCode, pe.createUser, pe.createDatetime, pe.modifyUser, pe.modifyDatetime, f.fileUrl , pe.partLotNoLength");
        hql.append(" order by pe.modifyDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), EpPartExtModel.class);

        packParameterForQueryPart(query, partExt);

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


}
