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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IProduceSnRepairDao;
import cn.estsh.i3plus.ext.mes.pojo.model.ProduceSnRepairModel;
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 ProduceSnRepairDao implements IProduceSnRepairDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryProduceSnRepairByPagerCount(ProduceSnRepairModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(psr.id) ");

        packHqlForQueryProduceSnRepair(hql, model);

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

        packParameterForQueryProduceSnRepair(query, model);

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

    @Override
    public List<ProduceSnRepairModel> queryProduceSnRepairByPager(ProduceSnRepairModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ProduceSnRepairModel.class.getName() + "(pse.serialNumber, pse.partNo, pse.partNameRdd, pse.qcStatus, " +
                " pse.snStatus, psr.workCenterCode, psr.workCellCode, psr.snType, psr.outWorkCenterTime, psr.workOrderNo, psr.lotNo, psr.workTime, psr.shiftCode, " +
                " psr.createUser, psr.createDatetime, psr.modifyUser, psr.modifyDatetime, psr.id ) ");

        packHqlForQueryProduceSnRepair(hql, model);

        hql.append(" order by pse.serialNumber asc ");
        Query query = entityManager.createQuery(hql.toString(), ProduceSnRepairModel.class);

        packParameterForQueryProduceSnRepair(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 packHqlForQueryProduceSnRepair(StringBuffer hql, ProduceSnRepairModel model) {
        hql.append(" from MesProduceSnRepair as psr ");
        hql.append(" left join MesProduceSnExt as pse on (pse.serialNumber = psr.serialNumber and pse.organizeCode = :organizeCode and pse.isValid = :isValid and pse.isDeleted = :isDeleted) ");
        hql.append(" where psr.organizeCode = :organizeCode ");
        hql.append(" and psr.isValid = :isValid ");
        hql.append(" and psr.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and psr.workOrderNo like :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and psr.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and psr.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getSerialNumber())) {
            hql.append(" and psr.serialNumber = :serialNumber ");
        }
        if (!StringUtils.isEmpty(model.getCreateDatetimeStart())) {
            hql.append(" and psr.createDatetime >= :createDatetimeStart ");
        }
        if (!StringUtils.isEmpty(model.getCreateDatetimeEnd())) {
            hql.append(" and psr.createDatetime <= :createDatetimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getOutWorkCenterTimeStart())) {
            hql.append(" and psr.outWorkCenterTime >= :outWorkCenterTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getOutWorkCenterTimeEnd())) {
            hql.append(" and psr.outWorkCenterTime <= :outWorkCenterTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and psr.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and psr.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getQcStatus())) {
            hql.append(" and pse.qcStatus = :qcStatus ");
        }
        if (!StringUtils.isEmpty(model.getSnStatus())) {
            hql.append(" and pse.snStatus = :snStatus ");
        }
    }

    private void packParameterForQueryProduceSnRepair(Query query, ProduceSnRepairModel 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.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.getOutWorkCenterTimeStart())) {
            query.setParameter(MesExtConstWords.OUT_WORK_CENTER_TIME_START, model.getOutWorkCenterTimeStart());
        }
        if (!StringUtils.isEmpty(model.getOutWorkCenterTimeEnd())) {
            query.setParameter(MesExtConstWords.OUT_WORK_CENTER_TIME_END, model.getOutWorkCenterTimeEnd());
        }
        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.getQcStatus())) {
            query.setParameter(MesExtConstWords.QC_STATUS, model.getQcStatus());
        }
        if (!StringUtils.isEmpty(model.getSnStatus())) {
            query.setParameter(MesExtConstWords.SN_STATUS, model.getSnStatus());
        }
    }
}
