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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IFourMDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFourMDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.FourMDetailModel;
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 FourMDetailDao implements IFourMDetailDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryFourMDetailByPagerCount(MesFourMDetail fourMDetail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct fmd.id) ");

        packHqlForQueryFourMDetail(hql, fourMDetail);

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

        packParameterForQueryFourMDetail(query, fourMDetail);

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

    @Override
    public List<FourMDetailModel> queryFourMDetailByPager(MesFourMDetail fourMDetail, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + FourMDetailModel.class.getName() + "(fmd.id, fmd.manageType, fmd.orderNo, fmd.workCenterCode, fmd.workCellCode, " +
                " fmd.beforeContent, fmd.afterContent, fmd.manageCode, fmd.manageContent, fmd.confirmCode, fmd.confirmContent, fmd.fristProduct, fmd.handlerContent, " +
                " fmd.teamConfirmUser, fmd.teamConfirmTime, fmd.qualityConfirmUser, fmd.qualityConfirmTime, fmd.isTeamConfirm, fmd.isQualityConfirm, fmd.modifyDatetime, wo.partNo, wo.partNameRdd ) ");

        packHqlForQueryFourMDetail(hql, fourMDetail);

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

        packParameterForQueryFourMDetail(query, fourMDetail);

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

    private void packHqlForQueryFourMDetail(StringBuffer hql, MesFourMDetail fourMDetail) {
        hql.append(" from MesFourMDetail as fmd ");
        hql.append(" left join MesWorkOrder as wo on (fmd.orderNo = wo.orderNo and wo.organizeCode = :organizeCode and wo.isValid = :isValid and wo.isDeleted = :isDeleted) ");
        hql.append(" where fmd.organizeCode = :organizeCode ");
        hql.append(" and fmd.isValid = :isValid ");
        hql.append(" and fmd.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(fourMDetail.getOrderNo())) {
            hql.append(" and fmd.orderNo = :orderNo ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getWorkCenterCode())) {
            hql.append(" and fmd.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getWorkCellCode())) {
            hql.append(" and fmd.workCellCode = :workCellCode ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getManageType())) {
            hql.append(" and fmd.manageType = :manageType ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getIsTeamConfirm())) {
            hql.append(" and fmd.isTeamConfirm = :isTeamConfirm ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getIsQualityConfirm())) {
            hql.append(" and fmd.isQualityConfirm = :isQualityConfirm ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getCreateDateTimeStart())) {
            hql.append(" and fmd.createDatetime >= :createDatetimeStart ");
        }
        if (!StringUtils.isEmpty(fourMDetail.getCreateDateTimeEnd())) {
            hql.append(" and fmd.createDatetime <= :createDatetimeEnd ");
        }
    }

    private void packParameterForQueryFourMDetail(Query query, MesFourMDetail fourMDetail) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, fourMDetail.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(fourMDetail.getOrderNo())) {
            query.setParameter(MesExtConstWords.ORDER_NO, fourMDetail.getOrderNo());
        }
        if (!StringUtils.isEmpty(fourMDetail.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, fourMDetail.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(fourMDetail.getWorkCellCode())) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, fourMDetail.getWorkCellCode());
        }
        if (!StringUtils.isEmpty(fourMDetail.getManageType())) {
            query.setParameter(MesExtConstWords.MANAGE_TYPE, fourMDetail.getManageType());
        }
        if (!StringUtils.isEmpty(fourMDetail.getIsTeamConfirm())) {
            query.setParameter(MesExtConstWords.IS_TEAM_CONFIRM, fourMDetail.getIsTeamConfirm());
        }
        if (!StringUtils.isEmpty(fourMDetail.getIsQualityConfirm())) {
            query.setParameter(MesExtConstWords.IS_QUALITY_CONFIRM, fourMDetail.getIsQualityConfirm());
        }
        if (!StringUtils.isEmpty(fourMDetail.getCreateDateTimeStart())) {
            query.setParameter(MesExtConstWords.CREATE_DATE_TIME_START, fourMDetail.getCreateDateTimeStart());
        }
        if (!StringUtils.isEmpty(fourMDetail.getCreateDateTimeEnd())) {
            query.setParameter(MesExtConstWords.CREATE_DATE_TIME_END, fourMDetail.getCreateDateTimeEnd());
        }
    }
}
