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


import cn.estsh.i3plus.ext.mes.apiservice.dao.IShiftProdCenterDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenter;
import cn.estsh.i3plus.ext.mes.pojo.model.EsopExtModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ShiftProdCenterModel;
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 ShiftProdCenterDao implements IShiftProdCenterDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryShiftProdCenterByPagerCount(ShiftProdCenterModel shiftProdCenterModel) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct spc.id) ");

        packHqlForQueryShiftProdCenter(hql, shiftProdCenterModel);

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

        packParameterForQueryShiftProdCenter(query, shiftProdCenterModel);

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

    @Override
    public List<ShiftProdCenterModel> queryShiftProdCenterByPager(ShiftProdCenterModel shiftProdCenterModel, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ShiftProdCenterModel.class.getName() + "(spc.id, spc.workCenterCode, spc.shiftCode, spc.shiftName, " +
                " spc.startTime, spc.endTime, spc.prodCode, sp.prodName, sp.prodCategory, sp.isProdTime, sp.prodType, se.startTime as shiftStartTime, se.endTime as shiftEndTime, " +
                " spc.organizeCode, spc.isValid, spc.isDeleted, spc.createUser, spc.createDatetime, spc.modifyUser, spc.modifyDatetime) ");

        packHqlForQueryShiftProdCenter(hql, shiftProdCenterModel);

        hql.append(" order by spc.workCenterCode, spc.shiftCode, spc.startTime ");
        Query query = entityManager.createQuery(hql.toString(), ShiftProdCenterModel.class);

        packParameterForQueryShiftProdCenter(query, shiftProdCenterModel);

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

    private void packHqlForQueryShiftProdCenter(StringBuffer hql, ShiftProdCenterModel shiftProdCenterModel) {
        hql.append(" from MesShiftProdCenter as spc ");
        hql.append(" left join MesShiftExt as se on (spc.shiftCode = se.shiftCode and spc.workCenterCode = se.workCenterCode and se.organizeCode = :organizeCode and se.isValid = :isValid1 and se.isDeleted = :isDeleted) ");
        hql.append(" left join MesShiftProd as sp on (spc.prodCode = sp.prodCode and sp.organizeCode = :organizeCode and sp.isValid = :isValid1 and sp.isDeleted = :isDeleted) ");
        hql.append(" where spc.organizeCode = :organizeCode ");
        hql.append(" and spc.isValid = :isValid ");
        hql.append(" and spc.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(shiftProdCenterModel.getWorkCenterCode())) {
            hql.append(" and spc.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getShiftCode())) {
            hql.append(" and spc.shiftCode = :shiftCode ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdCode())) {
            hql.append(" and sp.prodCode = :prodCode ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdName())) {
            hql.append(" and sp.prodName = :prodName ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdCategory())) {
            hql.append(" and sp.prodCategory = :prodCategory ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdType())) {
            hql.append(" and sp.prodType = :prodType ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getIsProdTime())) {
            hql.append(" and sp.isProdTime = :isProdTime ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getStartTimeStart())) {
            hql.append(" and spc.startTime >= :startTimeStart ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getStartTimeEnd())) {
            hql.append(" and spc.startTime <= :startTimeEnd ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getEndTimeStart())) {
            hql.append(" and spc.endTime >= :endTimeStart ");
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getEndTimeEnd())) {
            hql.append(" and spc.endTime <= :endTimeEnd ");
        }
    }

    private void packParameterForQueryShiftProdCenter(Query query, ShiftProdCenterModel shiftProdCenterModel) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, shiftProdCenterModel.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, shiftProdCenterModel.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(shiftProdCenterModel.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, shiftProdCenterModel.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, shiftProdCenterModel.getShiftCode());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdCode())) {
            query.setParameter(MesExtConstWords.PROD_CODE, shiftProdCenterModel.getProdCode());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdName())) {
            query.setParameter(MesExtConstWords.PROD_NAME, shiftProdCenterModel.getProdName());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdCategory())) {
            query.setParameter(MesExtConstWords.PROD_CATEGORY, shiftProdCenterModel.getProdCategory());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getProdType())) {
            query.setParameter(MesExtConstWords.PROD_TYPE, shiftProdCenterModel.getProdType());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getIsProdTime())) {
            query.setParameter(MesExtConstWords.IS_PROD_TIME, shiftProdCenterModel.getIsProdTime());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getStartTimeStart())) {
            query.setParameter(MesExtConstWords.START_TIME_START, shiftProdCenterModel.getStartTimeStart());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getStartTimeEnd())) {
            query.setParameter(MesExtConstWords.START_TIME_END, shiftProdCenterModel.getStartTimeEnd());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getEndTimeStart())) {
            query.setParameter(MesExtConstWords.END_TIME_START, shiftProdCenterModel.getEndTimeStart());
        }
        if (!StringUtils.isEmpty(shiftProdCenterModel.getEndTimeEnd())) {
            query.setParameter(MesExtConstWords.END_TIME_END, shiftProdCenterModel.getEndTimeEnd());
        }
    }
}
