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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pojo.model.*;
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 OutPutStatisticsDao implements IOutPutStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<UnWorkTimeStatisticsModel> queryOutPutStatisticsForReportForm(UnWorkTimeStatisticsModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + UnWorkTimeStatisticsModel.class.getName() + "(ops.workTime, ops.workCenterCode, ops.shiftCode, " +
                " ops.qty, pe.standardWork, pe.categoryCode3, wce.isDont, wce.dontClass, ops.remakeQty) ");
        hql.append(" from MesOutPutStatistics as ops ");
        hql.append(" left join MesPartExt as pe on (ops.partNo = pe.partNo and pe.organizeCode = :organizeCode and pe.isValid = :isValid and pe.isDeleted = :isDeleted) ");
        hql.append(" left join MesWorkCenterExt as wce on (ops.workCenterCode = wce.workCenterCode and wce.organizeCode = :organizeCode and wce.isValid = :isValid and wce.isDeleted = :isDeleted) ");
        hql.append(" where ops.organizeCode = :organizeCode ");
        hql.append(" and ops.isValid = :isValid ");
        hql.append(" and ops.isDeleted = :isDeleted ");

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

        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().substring(0, 10));
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd().substring(0, 10));
        }
        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.getWorkOrderType())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_TYPE, model.getWorkOrderType());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            query.setParameter(MesExtConstWords.IS_DONT, model.getIsDont());
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            query.setParameter(MesExtConstWords.DONT_CLASS, model.getDontClass());
        }

        return query.getResultList();
    }

    @Override
    public List<CompleteRateChartModel> queryCompleteRateChart(CompleteRateChartModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + CompleteRateChartModel.class.getName() + "(ops.workTime, ops.workCenterCode, ops.workOrderNo, ops.partNo, ops.partNameRdd, " +
                " ops.planQty, ops.qty, ops.orderStartTime, pe.categoryCode3, wce.workCenterName, wce.isDont, wce.dontClass) ");
        hql.append(" from MesOutPutStatistics as ops ");
        hql.append(" left join MesPartExt as pe on (ops.partNo = pe.partNo and pe.organizeCode = :organizeCode and pe.isValid = :isValid and pe.isDeleted = :isDeleted) ");
        hql.append(" left join MesWorkCenterExt as wce on (ops.workCenterCode = wce.workCenterCode and wce.organizeCode = :organizeCode and wce.isValid = :isValid and wce.isDeleted = :isDeleted) ");
        hql.append(" where ops.organizeCode = :organizeCode ");
        hql.append(" and ops.isValid = :isValid ");
        hql.append(" and ops.isDeleted = :isDeleted ");

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

        Query query = entityManager.createQuery(hql.toString(), CompleteRateChartModel.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().substring(0, 10));
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd().substring(0, 10));
        }
        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.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        if (!StringUtils.isEmpty(model.getIsDont())) {
            query.setParameter(MesExtConstWords.IS_DONT, model.getIsDont());
        }
        if (!StringUtils.isEmpty(model.getDontClass())) {
            query.setParameter(MesExtConstWords.DONT_CLASS, model.getDontClass());
        }

        return query.getResultList();
    }

    @Override
    public int queryOqcLastCheckDetailPagerCount(OqcReportParamModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct opso.id) ");

        packHqlForQueryOqcLastCheckDetail(hql, model);

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

        packParameterForQueryOqcLastCheckDetail(query, model);

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

    @Override
    public List<OqcLastCheckDetailModel> queryOqcLastCheckDetailPager(OqcReportParamModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + OqcLastCheckDetailModel.class.getName() + "(opso.id, opso.workOrderNo, opso.partNo, opso.partNameRdd, opso.workTime, opso.workCenterCode, " +
                " opso.shiftCode, opso.planQty, opso.spotCheckQty, opso.checkLimit, opso.checkResult, opso.checkNo, opso.checkUser, opso.confirmTime, opso.remark, ops.qty, p.colorCode) ");

        packHqlForQueryOqcLastCheckDetail(hql, model);

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

        packParameterForQueryOqcLastCheckDetail(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 packHqlForQueryOqcLastCheckDetail(StringBuffer hql, OqcReportParamModel model) {
        hql.append(" from MesOutPutStatisticsOqc as opso ");
        hql.append(" left join MesOutPutStatistics as ops on (ops.id = opso.pid and ops.organizeCode = :organizeCode and ops.isValid = :isValid and ops.isDeleted = :isDeleted) ");
        hql.append(" left join MesPartExt as p on (p.partNo = opso.partNo and p.organizeCode = :organizeCode and p.isValid = :isValid and p.isDeleted = :isDeleted) ");
        hql.append(" where opso.organizeCode = :organizeCode ");
        hql.append(" and opso.isValid = :isValid ");
        hql.append(" and opso.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and opso.workOrderNo = :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and opso.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and opso.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getCheckNo())) {
            hql.append(" and opso.checkNo = :checkNo ");
        }
        if (!StringUtils.isEmpty(model.getCheckResult())) {
            hql.append(" and opso.checkResult = :checkResult ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and opso.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and opso.workTime <= :workTimeEnd ");
        }
        hql.append(" and p.isOqc = :isOqc ");
    }

    private void packParameterForQueryOqcLastCheckDetail(Query query, OqcReportParamModel model) {
        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());
        query.setParameter(MesExtConstWords.IS_OQC, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getCheckNo())) {
            query.setParameter(MesExtConstWords.CHECK_NO, model.getCheckNo());
        }
        if (!StringUtils.isEmpty(model.getCheckResult())) {
            query.setParameter(MesExtConstWords.CHECK_RESULT, model.getCheckResult());
        }
        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());
        }
    }

    @Override
    public List<OqcCheckStatisticsModel> queryOqcLastCheckDetailList(OqcReportParamModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + OqcCheckStatisticsModel.class.getName() + "(opso.partNo, opso.partNameRdd, opso.workTime, " +
                " opso.spotCheckQty, opso.checkResult, ops.qty, p.salesTo, p.categoryCode3) ");
        hql.append(" from MesOutPutStatisticsOqc as opso ");
        hql.append(" left join MesOutPutStatistics as ops on (ops.id = opso.pid and ops.organizeCode = :organizeCode and ops.isValid = :isValid and ops.isDeleted = :isDeleted) ");
        hql.append(" left join MesPartExt as p on (p.partNo = opso.partNo and p.organizeCode = :organizeCode and p.isValid = :isValid and p.isDeleted = :isDeleted) ");
        hql.append(" where opso.organizeCode = :organizeCode ");
        hql.append(" and opso.isValid = :isValid ");
        hql.append(" and opso.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and opso.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and opso.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and opso.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and p.categoryCode3 = :categoryCode3 ");
        }
        hql.append(" and p.isOqc = :isOqc ");

        Query query = entityManager.createQuery(hql.toString(), OqcCheckStatisticsModel.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());
        query.setParameter(MesExtConstWords.IS_OQC, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        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());
        }

        return query.getResultList();
    }



}
