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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IMesAssistOutAndEnterDao;
import cn.estsh.i3plus.ext.mes.pojo.model.MesAssistOutAndEnterDateSumModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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 yinxiao
 * @date 2023-2023/2/6 15:52
 * @Description
 */
@Service
public class MesAssistOutAndEnterDao implements IMesAssistOutAndEnterDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryAssistOutAndEnterByPagerCount(MesAssistOutAndEnterDateSumModel assistOutAndEnter) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT count(DISTINCT assist.influenceDate)");
        hql.append(" from  MesAssistOutAndEnter as assist ");

        packHqlAssistOutAndEnter(hql, assistOutAndEnter);

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

        packParameterAssistOutAndEnter(query, assistOutAndEnter);

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

    @Override
    public List<MesAssistOutAndEnterDateSumModel> queryAssistOutAndEnter(MesAssistOutAndEnterDateSumModel assistOutAndEnter) {
        StringBuffer hql = new StringBuffer();
        hql.append("SELECT new " + MesAssistOutAndEnterDateSumModel.class.getName());
        hql.append(" (assist.influenceDate,assist.influenceTime,assist.assistOutAndEnterDetail,center.isDont,center.dontClass,center.workCenterCode,center.workCenterName");
        hql.append(" ) from  MesAssistOutAndEnter as assist ");
        hql.append(" LEFT JOIN MesWorkCenterExt as center ON center.workCenterCode = assist.workCenterCode ");
        hql.append(" and center.organizeCode = :organizeCode ");
        hql.append(" and center.isValid = :isValid ");
        hql.append(" and center.isDeleted = :isDeleted ");

        packHqlAssistOutAndEnter(hql, assistOutAndEnter);

        Query query = entityManager.createQuery(hql.toString());

        packParameterAssistOutAndEnter(query, assistOutAndEnter);

        return query.getResultList();
    }

    /**
     * 子查询统计对应援入援出明细的枚举
     */
    private void packHqlAssistInCount(StringBuffer hql) {
        hql.append(" ,(SELECT SUM(a.influenceTime) FROM MesAssistOutAndEnter as a WHERE a.influenceDate = assist.influenceDate AND " +
                " a.assistOutAndEnterDetail = " + MesExtEnumUtil.ASSIST_OUT_AND_ENTER_DETAIL.ASSIST_ENTER.getValue() + " ) as assistEnter ");
        hql.append(" ,(SELECT SUM(a.influenceTime) FROM MesAssistOutAndEnter as a WHERE a.influenceDate = assist.influenceDate AND " +
                " a.assistOutAndEnterDetail = " + MesExtEnumUtil.ASSIST_OUT_AND_ENTER_DETAIL.IN_ASSIST_OUT.getValue() + " ) as inAssistOut ");
        hql.append(" ,(SELECT SUM(a.influenceTime) FROM MesAssistOutAndEnter as a WHERE a.influenceDate = assist.influenceDate AND " +
                " a.assistOutAndEnterDetail = " + MesExtEnumUtil.ASSIST_OUT_AND_ENTER_DETAIL.NEWCOMER_ASSIST_OUT.getValue() + " ) as newcomerAssistOut ");
        hql.append(" ,(SELECT SUM(a.influenceTime) FROM MesAssistOutAndEnter as a WHERE a.influenceDate = assist.influenceDate AND " +
                " a.assistOutAndEnterDetail = " + MesExtEnumUtil.ASSIST_OUT_AND_ENTER_DETAIL.EXTERNAL_ASSIST_OUT.getValue() + " ) as externalAssistOut ");
    }

    /**
     * 拼接条件
     *
     * @param hql
     * @param assistOutAndEnter
     */
    private void packHqlAssistOutAndEnter(StringBuffer hql, MesAssistOutAndEnterDateSumModel assistOutAndEnter) {
        hql.append(" where 1 = 1 ");
        hql.append(" and assist.organizeCode = :organizeCode ");
        hql.append(" and assist.isValid = :isValid ");
        hql.append(" and assist.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(assistOutAndEnter.getWorkCenterCode())) {
            hql.append(" and assist.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getInfluenceDateStart())) {
            hql.append(" and assist.influenceDate >= :influenceDateStart ");
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getInfluenceDateEnd())) {
            hql.append(" and assist.influenceDate <= :influenceDateEnd ");
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getIsDont())) {
            hql.append(" and center.isDont = :isDont ");
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getDontClass())) {
            hql.append(" and center.dontClass = :dontClass ");
        }
    }

    /**
     * 拼接参数
     *
     * @param query
     * @param assistOutAndEnter
     */
    private void packParameterAssistOutAndEnter(Query query, MesAssistOutAndEnterDateSumModel assistOutAndEnter) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, assistOutAndEnter.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(assistOutAndEnter.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, assistOutAndEnter.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getIsDont())) {
            query.setParameter(MesExtConstWords.IS_DONT, assistOutAndEnter.getIsDont());
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getDontClass())) {
            query.setParameter(MesExtConstWords.DONT_CLASS, assistOutAndEnter.getDontClass());
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getInfluenceDateStart())) {
            query.setParameter("influenceDateStart", assistOutAndEnter.getInfluenceDateStart());
        }
        if (!StringUtils.isEmpty(assistOutAndEnter.getInfluenceDateEnd())) {
            query.setParameter("influenceDateEnd", assistOutAndEnter.getInfluenceDateEnd());
        }

    }

}
