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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IProduceSnRepairDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnRepairModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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(ProduceSnExtModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct pse.id) ");

        packHqlForQueryProduceSnRepair(hql, model);

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

        packParameterForQueryProduceSnRepair(query, model);

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

    @Override
    public List<ProduceSnExtModel> queryProduceSnRepairByPager(ProduceSnExtModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ProduceSnExtModel.class.getName() + "(pse.id, pse.serialNumber, pse.partNo, pse.partNameRdd, pse.qty, pse.qcStatus, " +
                " pse.snStatus, pse.printCount, pse.printStatus, psr.workCenterCode, psr.workCellCode, pse.routeCode, pse.processCode, pse.nextProcessCode, " +
                " pse.snType, pse.inWorkCenterTime, pse.processLabelTemplate, pse.qualityAssureTemplate, pse.bigProcessLabelTemplate, pse.lastWorkOrderNo, pse.organizeCode, " +
                " psr.createUser, psr.createDatetime, psr.modifyUser, psr.modifyDatetime, psr.workOrderNo, psr.outWorkCenterTime, psr.lotNo, psr.shiftCode, psr.workTime) ");

        packHqlForQueryProduceSnRepair(hql, model);

        hql.append(" order by pse.serialNumber asc ");
        Query query = entityManager.createQuery(hql.toString(), ProduceSnExtModel.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, ProduceSnExtModel 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 = :workOrderNo ");
        }
    }

    private void packParameterForQueryProduceSnRepair(Query query, ProduceSnExtModel model) {
        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());
        }
    }

    @Override
    public List<ProduceSnExtModel> queryProduceSnRepairList(ProduceSnExtModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ProduceSnExtModel.class.getName() + "(pse.id, pse.serialNumber, pse.partNo, pse.partNameRdd, pse.qty, pse.qcStatus, " +
                " pse.snStatus, pse.printCount, pse.printStatus, psr.workCenterCode, psr.workCellCode, pse.routeCode, pse.processCode, pse.nextProcessCode, " +
                " pse.snType, pse.inWorkCenterTime, pse.processLabelTemplate, pse.qualityAssureTemplate, pse.bigProcessLabelTemplate, pse.lastWorkOrderNo, pse.organizeCode, " +
                " psr.createUser, psr.createDatetime, psr.modifyUser, psr.modifyDatetime, psr.workOrderNo, psr.outWorkCenterTime, psr.lotNo, psr.shiftCode, psr.workTime) ");

        packHqlForQueryProduceSnRepair(hql, model);

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

        packParameterForQueryProduceSnRepair(query, model);

        return query.getResultList();
    }

    @Override
    public List<ProduceSnExtModel> queryProduceSnRepairAndPackageNoList(ProduceSnExtModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ProduceSnExtModel.class.getName() + "(pse.id, pse.serialNumber, pse.partNo, pse.partNameRdd, pse.qty, pse.qcStatus, " +
                " pse.snStatus, pse.printCount, pse.printStatus, psr.workCenterCode, psr.workCellCode, pse.routeCode, pse.processCode, pse.nextProcessCode, " +
                " pse.snType, pse.inWorkCenterTime, pse.processLabelTemplate, pse.qualityAssureTemplate, pse.bigProcessLabelTemplate, pse.lastWorkOrderNo, pse.organizeCode, " +
                " psr.createUser, psr.createDatetime, psr.modifyUser, psr.modifyDatetime, psr.workOrderNo, psr.outWorkCenterTime, psr.lotNo, psr.shiftCode, psr.workTime, pd.packageNo) ");

        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(" left join MesPackageDetail as pd on (pse.serialNumber = pd.serialNumber and pd.organizeCode = :organizeCode and pd.isValid = :isValid and pd.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 = :workOrderNo ");
        }

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

        packParameterForQueryProduceSnRepair(query, model);

        return query.getResultList();
    }

    @Override
    public List<ProduceSnRepairModel> queryRepairSnCtNoWithPackageNoListByWorkOrderNo(String organizeCode, String workOrderNo) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ProduceSnRepairModel.class.getName() + "(psr.serialNumber, psr.ctNo, pd.packageNo) ");
        hql.append(" from MesProduceSnRepair as psr ");
        hql.append(" left join MesPackageDetail as pd on (psr.serialNumber = pd.serialNumber and pd.organizeCode = :organizeCode and pd.isValid = :isValid and pd.isDeleted = :isDeleted) ");
        hql.append(" where psr.organizeCode = :organizeCode ");
        hql.append(" and psr.isValid = :isValid ");
        hql.append(" and psr.isDeleted = :isDeleted ");
        hql.append(" and psr.workOrderNo = :workOrderNo ");
        hql.append(" order by psr.serialNumber asc ");
        Query query = entityManager.createQuery(hql.toString(), ProduceSnRepairModel.class);
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, workOrderNo);
        return query.getResultList();
    }
}
