/**
 * 
 */
package org.cs.mgr.admin.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.cs.core.dao.impl.BaseDao;
import org.cs.mgr.admin.dao.ITbNewsDao;
import org.cs.mgr.admin.model.TbNews;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.springframework.stereotype.Repository;

/**
 * @ClassName TbNewsDao 
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author Administrator
 * @Date 2017-4-18上午9:38:41
 * @version 1.0.0
 */
@Repository
public class TbNewsDao extends BaseDao<TbNews> implements ITbNewsDao {

	@Override
	public Pager getList(Pager page, String classCode, String show, String userType) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder("SELECT tn.news_image as \"news_image\", tn.news_title as \"news_title\", tn.id as \"id\", tn.news_show as \"news_show\", tn.create_time as \"create_time\", tn.news_sort as \"news_sort\" , tn.open_flag as \"open_flag\" FROM tb_news tn WHERE 1=1 ");
		if(StringUtil.isNotBlank(classCode)){
			params.put("classCode", classCode);
			sql.append(" and tn.class_code = :classCode ");
		}
		if(StringUtil.isNotBlank(show)){
			params.put("show", show);
			sql.append(" and tn.news_show = :show ");
		}
		if(StringUtil.isNotBlank(userType)){
			if("1".equals(userType)){
				params.put("open_flag", "1");
				sql.append(" and tn.open_flag = :open_flag ");
			}
		}
		sql.append(" order by create_time desc, news_sort asc ");
	    return this.findPageBySql(sql.toString(), page, params);
	}
	
	@Override
	public Map<String, Object> queryDetail(Map<String, String> map) {
		String id = map.get("id");
		StringBuffer sb = new StringBuffer("SELECT tn.news_image, tn.news_title, tn.id, tn.create_time, tn.news_content FROM tb_news tn WHERE id =  "+id);
		return this.findBySql(sb.toString()).get(0);
	}

	@Override
	public Pager getMapList(Pager page, String classCode, String show, Map<String, String> map) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder("SELECT tn.news_image as \"news_image\", tn.news_title as \"news_title\", tn.id as \"id\", tn.news_show as \"news_show\", tn.create_time as \"create_time\", tn.news_sort as \"news_sort\" , tn.open_flag as \"open_flag\" FROM tb_news tn WHERE 1=1 ");
		if(StringUtil.isNotBlank(classCode)){
			params.put("classCode", classCode);
			sql.append(" and tn.class_code = :classCode ");
		}
		if(StringUtil.isNotBlank(show)){
			params.put("show", show);
			sql.append(" and tn.news_show = :show ");
		}
		
//		open_flag/
//		非集团用户只能看公开公告
		String userType = map.get("userType");
		if(StringUtil.isNotBlank(userType)){
			if("1".equals(userType)){
				params.put("open_flag", "1");
				sql.append(" and tn.open_flag = :open_flag ");
			}
		}
		sql.append(" order by create_time desc, news_sort asc ");
		
		
	    return this.findPageBySql(sql.toString(), page, params);
	}
}
