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

import cn.estsh.i3plus.ext.mes.apiservice.dao.jx.IJxManageCodeReportDao;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxManageCodeModel;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxManageCodeResultModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-jx
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-12-21 13:39
 */
@Service
public class JxManageCodeReportDao implements IJxManageCodeReportDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<JxManageCodeResultModel> queryManageCodeMcRecordReportPager(JxManageCodeModel model, Pager pager) {

        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + JxManageCodeResultModel.class.getName() + "( mr.id, mr.workOrderNo, mr.workCenterCode, ps.workCellCode, mr.manageCode, mr.createDatetime as bindTime, ps.snStatus ) ");

        packHqlForQueryManageCodeList(hql, model);

        hql.append(" order by mr.workCenterCode, mr.workOrderNo, mr.manageCode ");

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

        packParameterForQueryManageCodeList(query, model);

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

    private void packHqlForQueryManageCodeList(StringBuffer hql, JxManageCodeModel model) {
        hql.append(" from MesProduceSnMcRecord as mr ");
        hql.append(" inner join MesProduceSn ps on ( mr.serialNumber = ps.serialNumber and ps.organizeCode = :organizeCode and ps.isValid = :isValid and ps.isDeleted = :isDeleted) ");
        hql.append(" where mr.organizeCode = :organizeCode ");
        hql.append(" and mr.isValid = :isValid ");
        hql.append(" and mr.isDeleted = :isDeleted ");
        hql.append(" and mr.snStatus = :snStatus ");
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and mr.workOrderNo like :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and mr.workCenterCode like :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getManageCode())) {
            hql.append(" and mr.manageCode like :manageCode ");
        }
    }

    private void packParameterForQueryManageCodeList(Query query, JxManageCodeModel model) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, MesExtEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesExtConstWords.SN_STATUS, MesExtEnumUtil.MC_RECORD_SN_STATUS.LINE_ON.getValue());
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, "%"+ model.getWorkOrderNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, "%"+ model.getWorkCenterCode() + "%");
        }
        if (!StringUtils.isEmpty(model.getManageCode())) {
            query.setParameter(MesExtConstWords.MANAGE_CODE, "%"+ model.getManageCode() + "%" );
        }
    }

    @Override
    public int queryManageCodeMcRecordReportPagerCount(JxManageCodeModel model) {

        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct mr.id) ");

        packHqlForQueryManageCodeList(hql, model);

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

        packParameterForQueryManageCodeList(query, model);

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