package cn.com.countrygarden.bi.main.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.sunshine.common.repository.DataRepositoryJDBC;

@Repository("tbHSPRIncidentAcceptRepository")
public class TbHSPRIncidentAcceptRepository extends DataRepositoryJDBC {
	protected final Logger log = LoggerFactory.getLogger(TbHSPRIncidentAcceptRepository.class);

	public TbHSPRIncidentAcceptRepository() {
		this.masterTable = "HM_wygl_new_1853..Tb_HSPR_IncidentAccept";// 表名
		// this.masterTablePK = "";// 表的主键
	}

	/**
	 * 查询排名前的若干个记录，默认十个
	 * 
	 * @param paramMap
	 * @return
	 */
	public List<Map<String, Object>> queryTopXDataList(Map<String, Object> paramMap) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		String topNum = String.valueOf(paramMap.get("topNum"));
		int topInt = 10;
		try {
			topInt = Integer.parseInt(topNum);
		} catch (NumberFormatException e) {
			topInt = 10;
		}
		String sql = "SELECT o.OrganName,  c.CommName, p.TypeName, a.IncidentContent, f.DueAmount"
				+ " FROM HM_wygl_new_1853..Tb_HSPR_IncidentAccept a INNER JOIN("
				+ "  SELECT TOP "+topInt+" IncidentID, DueAmount FROM HM_wygl_new_1853..Tb_HSPR_Fees"
				+ " WHERE ISNULL(IncidentID, 0) > 0 ORDER BY  AccountsDueDate DESC"
				+ " ) f ON f.IncidentID = a.IncidentID"
				+ " INNER JOIN HM_wygl_new_1853..Tb_HSPR_IncidentType t ON t.CommID = a.CommID"
				+ " AND CHARINDEX( CONVERT(VARCHAR(20), t.TypeID), a.TypeID ) > 0"
				+ " INNER JOIN HM_wygl_new_1853..Tb_HSPR_IncidentType p ON p.CommID = t.CommID"
				+ " AND p.TypeCode = SUBSTRING(t.TypeCode, 1, 4)"
				+ " INNER JOIN HM_wygl_new_1853..Tb_HSPR_Community c ON c.CommID = a.CommID"
				+ " INNER JOIN HM_wygl_new_1853..Tb_Sys_Organ o ON o.OrganCode = c.OrganCode"
				+ " ORDER BY  a.IncidentDate DESC";
		resultList = this.jdbcTemp.queryForList(sql);
		return resultList;
	}
}
