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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IExteriorScratchProofCellDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesExteriorScratchProofCell;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pojo.model.MesExteriorScratchProofCellModel;
import cn.estsh.i3plus.ext.mes.pojo.model.MesPartExtModel;
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 cn.estsh.impp.framework.boot.auth.AuthUtil;
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
 * @CreateDate: 2021-03-12
 * @Description:
 **/
@Service
public class ExteriorScratchProofCellDao implements IExteriorScratchProofCellDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryExteriorScratchProofCellByPagerCount(MesExteriorScratchProofCell exteriorScratchProofCell) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(pe.id) ");

        packHqlForQuery(hql, exteriorScratchProofCell);

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

        packParameterForQuery(query, exteriorScratchProofCell);

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

    private void packHqlForQuery(StringBuffer hql, MesExteriorScratchProofCell exteriorScratchProofCell) {
        hql.append(" from MesExteriorScratchProofCell as pe ");
        hql.append(" left join MesFile as f on (pe.fileId = f.id and f.organizeCode = :organizeCode) ");
        hql.append(" where pe.organizeCode = :organizeCode ");
        hql.append(" and pe.isValid = :isValid ");
        hql.append(" and pe.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(exteriorScratchProofCell.getWorkCenterCode())) {
            hql.append(" and pe.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(exteriorScratchProofCell.getWorkCellCode())) {
            hql.append(" and pe.workCellCode = :workCellCode ");
        }

    }

    private void packParameterForQuery(Query query, MesExteriorScratchProofCell exteriorScratchProofCell) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, AuthUtil.getOrganize().getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, exteriorScratchProofCell.getIsValid());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(exteriorScratchProofCell.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, exteriorScratchProofCell.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(exteriorScratchProofCell.getWorkCellCode())) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, exteriorScratchProofCell.getWorkCellCode());
        }
    }

    @Override
    public List<MesExteriorScratchProofCellModel> queryExteriorScratchProofCellByPager(MesExteriorScratchProofCell exteriorScratchProofCell, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + MesExteriorScratchProofCellModel.class.getName() + "(pe.id, pe.workCenterCode, pe.workCellCode, pe.fileId, " +
                " pe.fileOriginName, pe.uploadDatetime, pe.organizeCode, pe.createUser, pe.createDatetime, pe.modifyUser, pe.modifyDatetime, f.fileUrl) ");

        packHqlForQuery(hql, exteriorScratchProofCell);

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

        packParameterForQuery(query, exteriorScratchProofCell);

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