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

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

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryStandardWorkOrderPackSnPagerCount(SxPackSnReportModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count( 1 ) ");

        packHqlForQueryStandardWorkOrderPackSn(hql, model);

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

        packParameterForQueryStandardWorkOrderPackSn(query, model);

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

    @Override
    public List<SxPackSnReportModel> queryStandardWorkOrderPackSnPager(SxPackSnReportModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SxPackSnReportModel.class.getName() + "( pe.packageNo, ps.partNo, ps.partNameRdd, " +
                " pe.workOrderNo, pe.workCenterCode, " +
                " ps.organizeCode, ps.createUser, ps.createDatetime, ps.modifyUser, ps.modifyDatetime, " +
                " ps.serialNumber, ps.outWorkCenterTime as outWcTime, opd.outerPackageNo, ps.shiftCode, pe.lotNo) ");

        packHqlForQueryStandardWorkOrderPackSn(hql, model);

        hql.append(" order by ps.serialNumber desc ");
        Query query = entityManager.createQuery(hql.toString(), SxPackSnReportModel.class);

        packParameterForQueryStandardWorkOrderPackSn(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 packHqlForQueryStandardWorkOrderPackSn(StringBuffer hql, SxPackSnReportModel model) {
        if (model.getWorkOrderType() != null && MesExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == model.getWorkOrderType()) {
            hql.append(" from MesProduceSnRepair as ps ");
        } else {
            hql.append(" from MesProduceSnExt as ps ");
        }
        hql.append(" inner join MesPackageDetail as pd on ( ps.serialNumber = pd.serialNumber and pd.organizeCode = :organizeCode and pd.isValid = 1 and pd.isDeleted = 2 ) ");
        hql.append(" inner join MesPackage pe on ( pe.packageNo = pd.packageNo and pe.organizeCode = :organizeCode and pe.isValid = 1 and pe.isDeleted = 2 ) ");
        hql.append(" left join MesOuterPackageDetail opd on ( opd.innerPackageNo = pd.packageNo and opd.organizeCode = :organizeCode and opd.isValid = 1 and opd.isDeleted = 2 ) ");
        hql.append(" where pe.organizeCode = :organizeCode ");
        hql.append(" and ps.isValid = 1 ");
        hql.append(" and ps.isDeleted = 2 ");
        if (model.getWorkOrderType() == null || MesExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != model.getWorkOrderType()) {
            hql.append(" and ps.workOrderNo = ps.lastWorkOrderNo ");
        }

        if (!StringUtils.isEmpty(model.getPackageNo())) {
            hql.append(" and pd.packageNo like :packageNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and ps.workOrderNo like :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and ps.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and ps.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getOutWcTimeStart())) {
            hql.append(" and ps.outWorkCenterTime >= :outWcTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getOutWcTimeEnd())) {
            hql.append(" and ps.outWorkCenterTime <= :outWcTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getCreateDatetimeStart())) {
            hql.append(" and ps.createDatetime >= :createDatetimeStart ");
        }
        if (!StringUtils.isEmpty(model.getCreateDatetimeEnd())) {
            hql.append(" and ps.createDatetime <= :createDatetimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getModifyDatetimeStart())) {
            hql.append(" and ps.modifyDatetime >= :modifyDatetimeStart ");
        }
        if (!StringUtils.isEmpty(model.getModifyDatetimeEnd())) {
            hql.append(" and ps.modifyDatetime <= :modifyDatetimeEnd ");
        }
        //if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
        //    hql.append(" and ps.workTime >= :workTimeStart ");
        //}
        //if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
        //    hql.append(" and ps.workTime <= :workTimeEnd ");
        //}

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

        if (!StringUtils.isEmpty(model.getOuterPackageNo())) {
            hql.append(" and opd.outerPackageNo like :outerPackageNo ");
        }
    }

    private void packParameterForQueryStandardWorkOrderPackSn(Query query, SxPackSnReportModel 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.getOutWcTimeStart())) {
            query.setParameter(MesExtConstWords.OUT_WC_TIME_START, model.getOutWcTimeStart() + MesExtConstWords.APPEND_ZERO_HMS);
        }
        if (!StringUtils.isEmpty(model.getOutWcTimeEnd())) {
            query.setParameter(MesExtConstWords.OUT_WC_TIME_END, model.getOutWcTimeEnd() + MesExtConstWords.APPEND_24_HMS);
        }
        if (!StringUtils.isEmpty(model.getCreateDatetimeStart())) {
            query.setParameter(MesExtConstWords.CREATE_DATE_TIME_START, model.getCreateDatetimeStart());
        }
        if (!StringUtils.isEmpty(model.getCreateDatetimeEnd())) {
            query.setParameter(MesExtConstWords.CREATE_DATE_TIME_END, model.getCreateDatetimeEnd());
        }
        if (!StringUtils.isEmpty(model.getModifyDatetimeStart())) {
            query.setParameter(MesExtConstWords.MODIFY_DATE_TIME_START, model.getModifyDatetimeStart());
        }
        if (!StringUtils.isEmpty(model.getModifyDatetimeEnd())) {
            query.setParameter(MesExtConstWords.MODIFY_DATE_TIME_END, model.getModifyDatetimeEnd());
        }
        //if (!StringUtils.isEmpty(model.getWorkTimeStart()) && !StringUtils.isEmpty(model.getWorkTimeEnd())) {
        //    query.setParameter(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart());
        //    query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());
        //}
        if (!StringUtils.isEmpty(model.getWorkTime())) {
            query.setParameter(MesExtConstWords.WORK_TIME, model.getWorkTime());
        }

        if (!StringUtils.isEmpty(model.getPackageNo())) {
            query.setParameter(MesExtConstWords.PACKAGENO, model.getPackageNo() + "%");
        }

        if (!StringUtils.isEmpty(model.getOuterPackageNo())) {
            query.setParameter(MesExtConstWords.OUTER_PACKAGE_NO, model.getOuterPackageNo() + "%");
        }
        //if (!StringUtils.isEmpty(model.getPartCategory()) && !StringUtils.isEmpty(MesExtEnumUtil.REPORT_PART_CATEGORY.valueOfDescription(model.getPartCategory()))) {
        //    if (!MesExtEnumUtil.REPORT_PART_CATEGORY.PART.getValue().equals(model.getPartCategory())) {
        //        query.setParameter(MesExtConstWords.CATEGORY_CODE2, model.getPartCategory());
        //    }
        //}
    }

    @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, " +
                " pe.lastWorkOrderNo, pe.workCenterCode, " +
                " pd.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() == MesExtEnumUtil.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() == MesExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() ) {
            if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
                hql.append(" and pe.lastWorkOrderNo = :workOrderNo ");
            }
            if (!StringUtils.isEmpty(model.getWorkTime())) {
                hql.append(" and ps.workTime = :workTime ");
            }
            if (!StringUtils.isEmpty(model.getShiftCode())) {
                hql.append(" and ps.shiftCode = :shiftCode ");
            }
        } else {
            if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
                hql.append(" and pe.workOrderNo = :workOrderNo ");
            }
            if (!StringUtils.isEmpty(model.getWorkTime())) {
                hql.append(" and pe.workTime = :workTime ");
            }
            if (!StringUtils.isEmpty(model.getShiftCode())) {
                hql.append(" and pe.shiftCode = :shiftCode ");
            }
        }

        Query query = entityManager.createQuery(hql.toString(), SxPackSnReportModel.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.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(model.getWorkTime())) {
            query.setParameter(MesExtConstWords.WORK_TIME, model.getWorkTime());
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        return query.getResultList();
    }
}
