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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IUnWorkTimeStatisticsDao;
import cn.estsh.i3plus.ext.mes.pojo.model.UnWorkTimeStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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 UnWorkTimeStatisticsDao implements IUnWorkTimeStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<UnWorkTimeStatisticsModel> queryUnWorkTimeStatistics(UnWorkTimeStatisticsModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + UnWorkTimeStatisticsModel.class.getName() + "(spcr.workTime, spcr.prodType, spcr.startTime, spcr.endTime, " +
                " spcr.workerQty, spcr.workCenterCode, spcr.shiftCode, spcr.shiftName, wce.workCenterName, wce.isDont, wce.dontClass, wce.noWorkTarget, ae.categoryCode3,spcr.atHomeNum,spcr.createDatetime) ");
        hql.append(" from MesShiftProdCenterRecord as spcr ");
        hql.append(" left join MesWorkCenterExt as wce on (spcr.workCenterCode = wce.workCenterCode and wce.organizeCode = :organizeCode and wce.isValid = :isValid and wce.isDeleted = :isDeleted) ");
        hql.append(" left join MesAreaExt as ae on (wce.areaCode = ae.areaCode and ae.organizeCode = :organizeCode and ae.isValid = :isValid and ae.isDeleted = :isDeleted) ");
        hql.append(" where spcr.organizeCode = :organizeCode ");
        hql.append(" and spcr.isValid = :isValid ");
        hql.append(" and spcr.isDeleted = :isDeleted ");

        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and spcr.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and spcr.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and spcr.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            hql.append(" and spcr.shiftCode = :shiftCode ");
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            hql.append(" and wce.isDont = :isDont ");
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            hql.append(" and wce.dontClass = :dontClass ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and ae.categoryCode3 = :categoryCode3 ");
        }

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

        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());

        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());
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            query.setParameter(MesExtConstWords.IS_DONT, model.getIsDont());
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            query.setParameter(MesExtConstWords.DONT_CLASS, model.getDontClass());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        return query.getResultList();
    }

    @Override
    public List<UnWorkTimeStatisticsModel> queryUnWorkTimeCategoryStatistics(UnWorkTimeStatisticsModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + UnWorkTimeStatisticsModel.class.getName() + "(spcr.workTime, spcr.prodType, spcr.prodCode, spcr.prodName, spcr.startTime, spcr.endTime, " +
                " spcr.workerQty, spcr.workCenterCode, spcr.shiftCode, spcr.shiftName, wce.workCenterName, wce.isDont, wce.dontClass, sp.planRate, ae.categoryCode3) ");
        hql.append(" from MesShiftProdCenterRecord as spcr ");
        hql.append(" left join MesWorkCenterExt as wce on (spcr.workCenterCode = wce.workCenterCode and wce.organizeCode = :organizeCode and wce.isValid = :isValid and wce.isDeleted = :isDeleted) ");
        hql.append(" left join MesAreaExt as ae on (wce.areaCode = ae.areaCode and ae.organizeCode = :organizeCode and ae.isValid = :isValid and ae.isDeleted = :isDeleted) ");
        hql.append(" left join MesShiftProd as sp on (spcr.prodCode = sp.prodCode and sp.organizeCode = :organizeCode and sp.isValid = :isValid and sp.isDeleted = :isDeleted) ");
        hql.append(" where spcr.organizeCode = :organizeCode ");
        hql.append(" and spcr.isValid = :isValid ");
        hql.append(" and spcr.isDeleted = :isDeleted ");

        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and spcr.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and spcr.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and spcr.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            hql.append(" and spcr.shiftCode = :shiftCode ");
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            hql.append(" and wce.isDont = :isDont ");
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            hql.append(" and wce.dontClass = :dontClass ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and ae.categoryCode3 = :categoryCode3 ");
        }

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

        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());

        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());
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            query.setParameter(MesExtConstWords.IS_DONT, model.getIsDont());
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            query.setParameter(MesExtConstWords.DONT_CLASS, model.getDontClass());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }

        return query.getResultList();
    }
}
