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

import cn.estsh.i3plus.ext.mes.apiservice.dao.bu.IMesDataTravelDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxMesDataTravel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.SxMesDataTravelExtModel;
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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-11-29 13:55
 */
@Service
public class MesDataTravelDao implements IMesDataTravelDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SxMesDataTravelExtModel> querySxMesDataTravelExtByPager(SxMesDataTravel mesDataTravelExt, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SxMesDataTravelExtModel.class.getName() + "( dt.id, dt.organizeCode, dt.isValid, dt.isDeleted, ");
        hql.append(" dt.createUser, dt.createDatetime, dt.modifyUser, dt.modifyDatetime, dt.menuCode, dt.partNo, dt.partNameRdd, dt.fieldCode, dt.beforeChange, ");
        hql.append(" dt.afterChange, dt.voucher, dt.initialWorkpieceNo, dt.skillIsConfirm, dt.skillIsConfirmUser, dt.skillIsConfirmTime, ");
        hql.append(" dt.manufacturingIsConfirm, dt.manufacturingIsConfirmUser, dt.manufacturingIsConfirmTime, dte.itemPartNo, dte.itemPartNameRdd, ");
        hql.append(" dte.processCode, dte.processName,dtm.fieldName ) ");

        packHqlForQuery(hql, mesDataTravelExt);

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

        packParameterForQuery(query, mesDataTravelExt);

        query.setMaxResults(pager.getPageSize());
        if (pager.getCurrentPage() == 0) {
            pager.setCurrentPage(1);
        }
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return query.getResultList();
    }

    @Override
    public int querySxMesDataTravelExtCount(SxMesDataTravel mesDataTravelExt) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct dt.id) ");

        packHqlForQuery(hql, mesDataTravelExt);

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

        packParameterForQuery(query, mesDataTravelExt);

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

    private void packHqlForQuery(StringBuffer hql, SxMesDataTravel model) {
        hql.append(" from SxMesDataTravel dt ");
        hql.append(" left join SxMesDataTravelWithProcessBom dte on (dt.id = dte.pid) ");
        hql.append(" left join MesTravelMenuFieldConfig dtm on dt.menuCode=dtm.menuCode and dt.fieldCode=dtm.fieldCode");
        hql.append(" where dt.organizeCode = :organizeCode ");
        hql.append(" and dt.isValid = :isValid ");
        hql.append(" and dt.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getMenuCode())) {
            hql.append(" and dt.menuCode = :menuCode ");
        }
        if (!StringUtils.isEmpty(model.getBeforeChange())) {
            hql.append(" and dt.beforeChange like :beforeChange ");
        }
        if (!StringUtils.isEmpty(model.getAfterChange())) {
            hql.append(" and dt.afterChange like :afterChange ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and dt.partNo like :partNo ");
        }
        if (!StringUtils.isEmpty(model.getVoucher())) {
            hql.append(" and dt.voucher like :voucher ");
        }
        if (!StringUtils.isEmpty(model.getInitialWorkpieceNo())) {
            hql.append(" and dt.initialWorkpieceNo like :initialWorkpieceNo ");
        }
        if (!StringUtils.isEmpty(model.getSkillIsConfirm())) {
            hql.append(" and dt.skillIsConfirm = :skillIsConfirm ");
        }
        if (!StringUtils.isEmpty(model.getManufacturingIsConfirm())) {
            hql.append(" and dt.manufacturingIsConfirm = :manufacturingIsConfirm ");
        }
        if (!StringUtils.isEmpty(model.getCreateDateTimeStart())) {
            hql.append(" and dt.createDatetime >= :createDatetimeStart ");
        }
        if (!StringUtils.isEmpty(model.getCreateDateTimeEnd())) {
            hql.append(" and dt.createDatetime <= :createDatetimeEnd ");
        }
        //增加变更字段模糊查询
        if (!StringUtils.isEmpty(model.getFieldName())) {
            hql.append(" and dtm.fieldName like :fieldName ");
        }
    }

    private void packParameterForQuery(Query query, SxMesDataTravel model) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, model.getIsValid());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(model.getMenuCode())) {
            query.setParameter(MesExtConstWords.MENU_CODE, model.getMenuCode());
        }
        if (!StringUtils.isEmpty(model.getBeforeChange())) {
            query.setParameter(MesExtConstWords.BEFORE_CHANGE, "%" + model.getBeforeChange() + "%");
        }
        if (!StringUtils.isEmpty(model.getAfterChange())) {
            query.setParameter(MesExtConstWords.AFTER_CHANGE, "%" + model.getAfterChange() + "%");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, "%" + model.getPartNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getVoucher())) {
            query.setParameter(MesExtConstWords.VOUCHER, "%" + model.getVoucher() + "%");
        }
        if (!StringUtils.isEmpty(model.getInitialWorkpieceNo())) {
            query.setParameter(MesExtConstWords.INITIAL_WORKPIECE_NO, "%" + model.getInitialWorkpieceNo() + "%");
        }
        if (!StringUtils.isEmpty(model.getSkillIsConfirm())) {
            query.setParameter(MesExtConstWords.SKILL_IS_CONFIRM, model.getSkillIsConfirm());
        }
        if (!StringUtils.isEmpty(model.getManufacturingIsConfirm())) {
            query.setParameter(MesExtConstWords.MANUFACTURING_IS_CONFIRM, model.getManufacturingIsConfirm());
        }
        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.getFieldName())) {
            query.setParameter(MesExtConstWords.FIELD_NAME, "%" + model.getFieldName() + "%");
        }
    }
}
