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


import cn.estsh.i3plus.ext.mes.apiservice.dao.ISxRepairRecordDao;
import cn.estsh.i3plus.ext.mes.pojo.model.SxRepairRecordReportModel;
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 SxRepairRecordDao implements ISxRepairRecordDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryRepairRecordByPagerCount(SxRepairRecordReportModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct bcd.id) ");

        packHqlForQueryRepairRecord(hql, model);

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

        packParameterForQueryRepairRecord(query, model);

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

    @Override
    public List<SxRepairRecordReportModel> queryRepairRecordByPager(SxRepairRecordReportModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SxRepairRecordReportModel.class.getName() + "(bcd.id, bcd.workTime, bcd.shiftCode, bcd.workOrderNo, bcd.workCenterCode, " +
                " bcd.workCellCode, bcd.serialNumber, bcd.productSn, bcd.partNo, bcd.partNameRdd, bcd.defectCode, bcd.defectName, bcd.defectType, bcd.defectTypeName, bcd.defectActionTime, " +
                " bcd.repairStatus, bcd.createUser, rre.dcCode, rre.dcName, rre.repairCode, rre.repairName,bcd.approvalStatus, rre.memo, rre.repairJudgeCode,rre.repairJudgeName, rre.repairDatetime, rre.modifyUser) ");

        packHqlForQueryRepairRecord(hql, model);

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

        packParameterForQueryRepairRecord(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 packHqlForQueryRepairRecord(StringBuffer hql, SxRepairRecordReportModel model) {
        hql.append(" from MesBadCellDetail as bcd ");
        hql.append(" left join MesRepairRecordExt as rre on (bcd.id = rre.defectRecordId and rre.organizeCode = :organizeCode and rre.isValid = :isValid and rre.isDeleted = :isDeleted) ");
        hql.append(" where bcd.organizeCode = :organizeCode ");
        hql.append(" and bcd.isValid = :isValid ");
        hql.append(" and bcd.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and bcd.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and bcd.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and bcd.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and bcd.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getSerialNumber())) {
            hql.append(" and bcd.serialNumber = :serialNumber ");
        }
        if (!StringUtils.isEmpty(model.getProductSn())) {
            hql.append(" and bcd.productSn = :productSn ");
        }
        if (!StringUtils.isEmpty(model.getDefectCode())) {
            hql.append(" and bcd.defectCode = :defectCode ");
        }
        if (!StringUtils.isEmpty(model.getDefectType())) {
            hql.append(" and bcd.defectType = :defectType ");
        }
        if (!StringUtils.isEmpty(model.getRepairStatus())) {
            hql.append(" and bcd.repairStatus = :repairStatus ");
        }
        if (!StringUtils.isEmpty(model.getDcCode())) {
            hql.append(" and rre.dcCode = :dcCode ");
        }
        if (!StringUtils.isEmpty(model.getRepairCode())) {
            hql.append(" and rre.repairCode = :repairCode ");
        }
        if (!StringUtils.isEmpty(model.getRepairJudgeCode())) {
            hql.append(" and rre.repairJudgeCode = :repairJudgeCode ");
        }
    }

    private void packParameterForQueryRepairRecord(Query query, SxRepairRecordReportModel 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.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.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getSerialNumber())) {
            query.setParameter(MesExtConstWords.SERIAL_NUMBER, model.getSerialNumber());
        }
        if (!StringUtils.isEmpty(model.getProductSn())) {
            query.setParameter(MesExtConstWords.PRODUCT_SN, model.getProductSn());
        }
        if (!StringUtils.isEmpty(model.getDefectCode())) {
            query.setParameter(MesExtConstWords.DEFECT_CODE, model.getDefectCode());
        }
        if (!StringUtils.isEmpty(model.getDefectType())) {
            query.setParameter(MesExtConstWords.DEFECT_TYPE, model.getDefectType());
        }
        if (!StringUtils.isEmpty(model.getRepairStatus())) {
            query.setParameter(MesExtConstWords.REPAIR_STATUS, model.getRepairStatus());
        }
        if (!StringUtils.isEmpty(model.getDcCode())) {
            query.setParameter(MesExtConstWords.DC_CODE, model.getDcCode());
        }
        if (!StringUtils.isEmpty(model.getRepairCode())) {
            query.setParameter(MesExtConstWords.REPAIR_CODE, model.getRepairCode());
        }
        if (!StringUtils.isEmpty(model.getRepairJudgeCode())) {
            query.setParameter(MesExtConstWords.REPAIR_JUDGE, model.getRepairJudgeCode());
        }
    }
}
