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

import cn.estsh.i3plus.ext.mes.apiservice.dao.bu.IMesOuterPackageRetrospectDao;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.MesOuterPackageRetrospectModel;
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-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-01-29 14:34
 */
@Service
public class MesOuterPackageRetrospectDao implements IMesOuterPackageRetrospectDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesOuterPackageRetrospectModel> queryMesOuterPackageRetrospectByPager(MesOuterPackageRetrospectModel outerPackageRetrospectModel, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + MesOuterPackageRetrospectModel.class.getName() + "( op.workCenterCode, op.workCenterName, op.outerPackageNo, opd.innerPackageNo, ");
        hql.append(" op.shiftCode, ms.shiftName, op.workTime, op.partNo, op.partNameRdd, op.workOrderNo, op.workOrderType, op.ctNo, op.isSealed, op.organizeCode, op.isValid, ");
        hql.append(" op.isDeleted, op.createUser, op.createDatetime, op.modifyUser, op.modifyDatetime, op.id ) ");

        packHqlForQuery(hql, outerPackageRetrospectModel);

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

        packParameterForQuery(query, outerPackageRetrospectModel);

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

    @Override
    public int queryMesOuterPackageRetrospectCount(MesOuterPackageRetrospectModel outerPackageRetrospectModel) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count( 1 ) ");

        packHqlForQuery(hql, outerPackageRetrospectModel);

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

        packParameterForQuery(query, outerPackageRetrospectModel);

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

    private void packHqlForQuery(StringBuffer hql, MesOuterPackageRetrospectModel model) {
        hql.append(" from MesOuterPackage op ");
        hql.append(" left join MesOuterPackageDetail opd on (opd.outerPackageNo = op.outerPackageNo and opd.organizeCode = :organizeCode and opd.isValid = :isValid and opd.isDeleted = :isDeleted  and opd.bindStatus = :bindStatus) ");
        hql.append(" left join MesShift ms on (ms.workCenterCode = op.workCenterCode and ms.shiftCode = op.shiftCode and ms.organizeCode = :organizeCode and ms.isValid = :isValid and ms.isDeleted = :isDeleted) ");
        hql.append(" where op.organizeCode = :organizeCode ");
        hql.append(" and op.isValid = :isValid ");
        hql.append(" and op.isDeleted = :isDeleted ");
        //hql.append(" and opd.bindStatus = :bindStatus ");
        if (!StringUtils.isEmpty(model.getOuterPackageNo())) {
            hql.append(" and op.outerPackageNo like :outerPackageNo ");
        }
        if (!StringUtils.isEmpty(model.getInnerPackageNo())) {
            hql.append(" and opd.innerPackageNo like :innerPackageNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and op.workCenterCode like :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and op.partNo like :partNo ");
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            hql.append(" and op.shiftCode = :shiftCode ");
        }
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and op.workOrderNo like :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and op.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and op.workTime <= :workTimeEnd ");
        }
    }

    private void packParameterForQuery(Query query, MesOuterPackageRetrospectModel model) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesExtConstWords.BIND_STATUS, MesExtEnumUtil.PACKAGE_BIND_STATUS.BINGDING.getValue());
        if (!StringUtils.isEmpty(model.getOuterPackageNo())) {
            query.setParameter(MesExtConstWords.OUTER_PACKAGE_NO, model.getOuterPackageNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getInnerPackageNo())) {
            query.setParameter(MesExtConstWords.INNER_PACKAGE_NO, model.getInnerPackageNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode() + "%");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            query.setParameter(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());
        }
    }
}
