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

import cn.estsh.i3plus.ext.mes.apiservice.dao.jx.IJxOqcCheckReportDao;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxOqcCheckDetailReportModel;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxOqcCheckReportModel;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxOqcCheckStatisticsReportModel;
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;

/**
 * @Author: wangjie
 * @CreateDate: 2019/11/21 11:29 上午
 * @Description:
 **/
@Service
public class JxOqcCheckReportDao implements IJxOqcCheckReportDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<JxOqcCheckReportModel> queryUnQcOqcCheckListByPart(JxOqcCheckStatisticsReportModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + JxOqcCheckReportModel.class.getName() + "(oc.workTime, oc.oqcNo, oc.workOrderNo, oc.workOrderType, oc.partNo, oc.partNameRdd, " +
                " oc.workCenterCode, oc.shiftCode, oc.oqcNoType, oc.serialNumber, oc.packageNo, oc.qty, oc.spotCheckQty, oc.oqcQty, oc.oqcType, " +
                " oc.checkResult, oc.checkUser, oc.checkTime, oc.badnessTypeCode, oc.badnessClassifyCode, oc.badnessClassifyDetailName, " +
                " oc.createUser, oc.createDatetime, oc.modifyUser, oc.modifyDatetime, p.categoryCode3, p.salesTo ) ");

        packHqlForQueryUnQcOqcCheckListByPart(hql, model);

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

        packParameterForQueryUnQcOqcCheckListByPart(query, model);

        return query.getResultList();
    }

    @Override
    public List<JxOqcCheckDetailReportModel> queryOqcCheckDetailReportPager(JxOqcCheckDetailReportModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + JxOqcCheckDetailReportModel.class.getName() + "(ocd.workTime, ocd.workOrderNo, ocd.partNo, ocd.partNameRdd, ocd.workCenterCode, ocd.shiftCode, " +
                " ocd.oqcNo, ocd.serialNumber, ocd.packageNo, ocd.seq, ocd.checkItem, ocd.standard, ocd.checkTool, ocd.checkType, ocd.standardValue, ocd.upLimit,  " +
                " ocd.downLimit, ocd.remark, ocd.checkValue, ocd.checkResult, ocd.checkUser, ocd.itemCheckTime, ocd.confirmUser, ocd.confirmTime, ocd.organizeCode, " +
                " ocd.createUser, ocd.createDatetime, ocd.modifyUser, ocd.modifyDatetime, ocd.id, oc.oqcNoType) ");

        packHqlForQueryOqcCheckDetail(hql, model);

        hql.append(" order by ocd.oqcNo asc, ocd.seq asc ");
        Query query = entityManager.createQuery(hql.toString(), JxOqcCheckDetailReportModel.class);

        packParameterForQueryOqcCheckDetail(query, model);

        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 queryOqcCheckDetailReportPagerCount(JxOqcCheckDetailReportModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct ocd.id) ");

        packHqlForQueryOqcCheckDetail(hql, model);

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

        packParameterForQueryOqcCheckDetail(query, model);

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

    private void packHqlForQueryOqcCheckDetail(StringBuffer hql, JxOqcCheckDetailReportModel model) {
        hql.append(" from MesOqcCheckDetail as ocd ");
        hql.append(" left join MesOqcCheck as oc on (oc.oqcNo = ocd.oqcNo and oc.organizeCode = :organizeCode and oc.isValid = :isValid and oc.isDeleted = :isDeleted) ");
        hql.append(" where ocd.organizeCode = :organizeCode ");
        hql.append(" and ocd.isValid = :isValid ");
        hql.append(" and ocd.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and ocd.workOrderNo = :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and ocd.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and ocd.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getSerialNumber())) {
            hql.append(" and ocd.serialNumber = :serialNumber ");
        }
        if (!StringUtils.isEmpty(model.getCheckValue())) {
            hql.append(" and ocd.checkValue = :checkValue ");
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            hql.append(" and ocd.shiftCode = :shiftCode ");
        }
        if (!StringUtils.isEmpty(model.getCheckItem())) {
            hql.append(" and ocd.checkItem like :checkItem ");
        }
        if (!StringUtils.isEmpty(model.getStandard())) {
            hql.append(" and ocd.standard like :standard ");
        }
        if (!StringUtils.isEmpty(model.getOqcNo())) {
            hql.append(" and ocd.oqcNo like :oqcNo ");
        }
        if (!StringUtils.isEmpty(model.getCheckResult())) {
            hql.append(" and ocd.checkResult = :checkResult ");
        }
        if (!StringUtils.isEmpty(model.getOqcNoType())) {
            hql.append(" and oc.oqcNoType = :oqcNoType ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and ocd.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and ocd.workTime <= :workTimeEnd ");
        }
    }

    private void packParameterForQueryOqcCheckDetail(Query query, JxOqcCheckDetailReportModel 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());
        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.getSerialNumber())) {
            query.setParameter(MesExtConstWords.SERIAL_NUMBER, model.getSerialNumber());
        }
        if (!StringUtils.isEmpty(model.getCheckValue())) {
            query.setParameter(MesExtConstWords.CHECK_VALUE, model.getCheckValue());
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        if (!StringUtils.isEmpty(model.getCheckItem())) {
            query.setParameter(MesExtConstWords.CHECK_ITEM, "%" + model.getCheckItem() + "%");
        }
        if (!StringUtils.isEmpty(model.getStandard())) {
            query.setParameter(MesExtConstWords.STANDARD, "%" + model.getStandard() + "%");
        }
        if (!StringUtils.isEmpty(model.getOqcNo())) {
            query.setParameter(MesExtConstWords.OQC_NO, "%" + model.getOqcNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getCheckResult())) {
            query.setParameter(MesExtConstWords.CHECK_RESULT, model.getCheckResult());
        }
        if (!StringUtils.isEmpty(model.getOqcNoType())) {
            query.setParameter(MesExtConstWords.OQC_NO_TYPE, model.getOqcNoType());
        }
        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());
        }
    }

    private void packHqlForQueryUnQcOqcCheckListByPart(StringBuffer hql, JxOqcCheckStatisticsReportModel model) {
        hql.append(" from MesOqcCheck as oc ");
        hql.append(" left join MesPartExt as p on (oc.partNo = p.partNo and p.organizeCode = :organizeCode and p.isValid = :isValid and p.isDeleted = :isDeleted) ");
        hql.append(" where oc.organizeCode = :organizeCode ");
        hql.append(" and oc.isValid = :isValid ");
        hql.append(" and oc.isDeleted = :isDeleted ");
        hql.append(" and oc.checkResult = :checkResult ");
        hql.append(" and oc.oqcNoType != :oqcNoType ");
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and oc.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and oc.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and oc.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and p.categoryCode3 = :categoryCode3 ");
        }
        if (!StringUtils.isEmpty(model.getSalesTo())) {
            hql.append(" and p.salesTo = :salesTo ");
        }
        hql.append(" and p.isOqc = :isOqc ");
    }

    private void packParameterForQueryUnQcOqcCheckListByPart(Query query, JxOqcCheckStatisticsReportModel 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.IS_OQC, MesExtEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        query.setParameter(MesExtConstWords.CHECK_RESULT, MesExtEnumUtil.OQC_CHECK_RESULT.UNQUALIFIED.getValue());
        query.setParameter(MesExtConstWords.OQC_NO_TYPE, MesExtEnumUtil.OQC_CHECK_TYPE.TEARDOWN_INSPECTION.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.getSalesTo())) {
            query.setParameter(MesExtConstWords.SALES_TO, model.getSalesTo());
        }
        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());
        }
    }



}
