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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IModuleExcelManageDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesModuleExcelManage;
import cn.estsh.i3plus.ext.mes.pojo.model.ModuleExcelManageModel;
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 ModuleExcelManageDao implements IModuleExcelManageDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryModuleExcelManageByPagerCount(MesModuleExcelManage moduleExcelManage) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct mem.id) ");

        packHqlForQueryModuleExcelManage(hql, moduleExcelManage);

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

        packParameterForQueryModuleExcelManage(query, moduleExcelManage);

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

    @Override
    public List<ModuleExcelManageModel> queryModuleExcelManageByPager(MesModuleExcelManage moduleExcelManage, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + ModuleExcelManageModel.class.getName() + "(mem.id, mem.moduleCode, mem.moduleName, mem.fileId, f.fileUrl, f.fileOriginName, " +
                " mem.organizeCode, mem.createUser, mem.createDatetime, mem.modifyUser, mem.modifyDatetime ) ");

        packHqlForQueryModuleExcelManage(hql, moduleExcelManage);

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

        packParameterForQueryModuleExcelManage(query, moduleExcelManage);

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

    private void packHqlForQueryModuleExcelManage(StringBuffer hql, MesModuleExcelManage moduleExcelManage) {
        hql.append(" from MesModuleExcelManage as mem ");
        hql.append(" left join MesFile as f on (mem.fileId = f.id and f.organizeCode = :organizeCode and f.isValid = :isValid1 and f.isDeleted = :isDeleted) ");
        hql.append(" where mem.organizeCode = :organizeCode ");
        hql.append(" and mem.isValid = :isValid ");
        hql.append(" and mem.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(moduleExcelManage.getModuleCode())) {
            hql.append(" and mem.moduleCode like :moduleCode ");
        }
        if (!StringUtils.isEmpty(moduleExcelManage.getModuleName())) {
            hql.append(" and mem.moduleName like :moduleName ");
        }
    }

    private void packParameterForQueryModuleExcelManage(Query query, MesModuleExcelManage moduleExcelManage) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, moduleExcelManage.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, moduleExcelManage.getIsValid());
        query.setParameter(MesExtConstWords.IS_VALID1, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(moduleExcelManage.getModuleCode())) {
            query.setParameter(MesExtConstWords.MODULE_CODE, "%" + moduleExcelManage.getModuleCode() + "%");
        }
        if (!StringUtils.isEmpty(moduleExcelManage.getModuleName())) {
            query.setParameter(MesExtConstWords.MODULE_NAME,"%" + moduleExcelManage.getModuleName() + "%");
        }
    }
}
