package com.palmnest.dao.notice.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.GenericDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.ArticleCensusDao;
import com.palmnest.model.notice.AppNoticeDetailModel;
import com.palmnest.model.notice.ArticleCensusModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * 组织架构L1 - ArticleCensusDaoImpl
 *
 * @author: jiangyong
 * @email: jiangyong@avic-s.com
 * @date: 2019/12/14 14:31
 */
@Repository("articleCensusDao")
public class ArticleCensusDaoImpl extends GenericDaoHibernate<ArticleCensusModel,Integer> implements ArticleCensusDao {
    public ArticleCensusDaoImpl() {
        super(ArticleCensusModel.class);
    }


    @Override
    public Integer getTotal(Map<String, Object> map) {

        StringBuffer sbf = new StringBuffer();
        sbf.append("  SELECT COALESCE(SUM(IFNULL(count,0)),0) count FROM tb_article_census  ");
        sbf.append("  where enabled =1");

        if (map.get("artId") != null) {
            sbf.append(" and art_id =" + map.get("artId"));
        }
        if (map.get("orgId") != null) {
            sbf.append(" and org_id =" + map.get("orgId"));
        }

        if (map.get("readerId") != null) {
            sbf.append(" and reader_id =" + map.get("readerId"));
        }

        if (map.get("readerType") != null) {
            sbf.append(" and reader_type =" + map.get("readerType"));
        }

        if (map.get("type") != null) {
            sbf.append(" and type =" + map.get("type"));
        }

        if (map.get("platformType") != null) {
            sbf.append(" and platform_type =" + map.get("platformType"));
        }

        if (map.get("dayInfo") != null) {
            sbf.append(" and day_info like '%" + map.get("dayInfo")+"%'");
        }

        if (map.get("createdBy") != null) {
            sbf.append(" and created_by =" + map.get("createdBy"));
        }
        if (map.get("updatedBy") != null) {
            sbf.append(" and updated_by =" + map.get("updatedBy"));
        }
        return ((BigDecimal) getSession().createSQLQuery(sbf.toString()).uniqueResult()).intValue();
    }

    @Override
    public List<ArticleCensusModel> getListByParam(Map<String, Object> map) {


        StringBuffer sbf = new StringBuffer();
        sbf.append("  FROM ArticleCensusModel  ");
        sbf.append("  where enabled =1");

        if (map.get("artId") != null) {
            sbf.append(" and art_id =" + map.get("artId"));
        }
        if (map.get("orgId") != null) {
            sbf.append(" and org_id =" + map.get("orgId"));
        }

        if (map.get("readerId") != null) {
            sbf.append(" and reader_id =" + map.get("readerId"));
        }

        if (map.get("readerType") != null) {
            sbf.append(" and reader_type =" + map.get("readerType"));
        }

        if (map.get("type") != null) {
            sbf.append(" and type =" + map.get("type"));
        }

        if (map.get("platformType") != null) {
            sbf.append(" and platform_type =" + map.get("platformType"));
        }

        if (map.get("dayInfo") != null) {
            sbf.append(" and day_info like '%" + map.get("dayInfo")+"%'");
        }

        if (map.get("createdBy") != null) {
            sbf.append(" and created_by =" + map.get("createdBy"));
        }
        if (map.get("updatedBy") != null) {
            sbf.append(" and updated_by =" + map.get("updatedBy"));
        }

        return getSession().createQuery(sbf.toString()).list();
    }
}
