package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.bu3;

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.bu3.ISxBuPackageExtDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu3.SxPackSnReportModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
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 SxBuPackageExtDao implements ISxBuPackageExtDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SxPackSnReportModel> queryStandardWorkOrderPackSnList(SxPackSnReportModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SxPackSnReportModel.class.getName() + "( opd.outerPackageNo, pe.packageNo, pe.partNo, pe.partNameRdd, " +
                " ps.workOrderNo, ps.workCenterCode, " +
                " ps.serialNumber, ps.outWorkCenterTime as outWcTime, ps.shiftCode, ps.workTime ) ");

        hql.append(" from MesPackageExt as pe ");
        hql.append(" inner join MesPackageDetail as pd on (pe.packageNo = pd.packageNo and pd.organizeCode = :organizeCode and pd.isValid = :isValid and pd.isDeleted = :isDeleted) ");
        if (model.getWorkOrderType() == MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue()) {
            hql.append(" left join MesProduceSnRepair as ps on ( ps.serialNumber = pd.serialNumber and ps.organizeCode = :organizeCode and ps.isValid = :isValid and ps.isDeleted = :isDeleted ) ");
        } else {
            hql.append(" left join MesProduceSnExt as ps on ( ps.serialNumber = pd.serialNumber and ps.organizeCode = :organizeCode and ps.isValid = :isValid and ps.isDeleted = :isDeleted ) ");
        }

        hql.append(" left join MesOuterPackageDetail as opd on ( opd.innerPackageNo = pe.packageNo and opd.organizeCode = :organizeCode and opd.isValid = :isValid and opd.isDeleted = :isDeleted ) ");

        hql.append(" where pe.organizeCode = :organizeCode ");
        hql.append(" and pe.isValid = :isValid ");
        hql.append(" and pe.isDeleted = :isDeleted ");
        if ( model.getWorkOrderType() == MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() ) {
            if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
                hql.append(" and pe.lastWorkOrderNo = :workOrderNo ");
            }
        } else {
            if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
                hql.append(" and pe.workOrderNo = :workOrderNo ");
            }
        }

        if (!StringUtils.isEmpty(model.getWorkTime())) {
            hql.append(" and ps.workTime = :workTime ");
        }
        if (!StringUtils.isEmpty(model.getWorkTime())) {
            hql.append(" and ps.shiftCode = :shiftCode ");
        }

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

        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(model.getWorkTime())) {
            query.setParameter(MesPcnExtConstWords.WORK_TIME, model.getWorkTime());
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesPcnExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        return query.getResultList();
    }
}
