package org.linlinjava.jz.admin.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.linlinjava.jz.db.dao.JzReportMapper;
import org.linlinjava.jz.db.dto.BuiltlistReportDto;
import org.linlinjava.jz.db.dto.ConsultTypeReport;
import org.linlinjava.jz.db.dto.TicketReportDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

@Service
public class AdminReportService {

	@Autowired
	JzReportMapper jzReportMapper;

	/**
	 * 电访任务报表
	 * 
	 * @param page
	 * @param limit
	 * @param campaignId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<BuiltlistReportDto> selectBuiltlistReport(Integer page, Integer limit, Integer campaignId,
			String startTime, String endTime) {

		PageHelper.startPage(page, limit);
		List<BuiltlistReportDto> selectByCampaignIdTime = jzReportMapper.selectBuiltlistReport(campaignId, startTime,
				endTime);
		return selectByCampaignIdTime;
	}

	/**
	 * 工单报表
	 * 
	 * @param page
	 * @param limit
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<TicketReportDto> selectTicketReport(Integer page, Integer limit, String startTime, String endTime) {

		PageHelper.startPage(page, limit);
		List<TicketReportDto> selectByCampaignIdTime = jzReportMapper.selectTicketReport(startTime, endTime);
		return selectByCampaignIdTime;
	}

	/**
	 * 咨询类型报表
	 * 
	 * @param campaignId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<ConsultTypeReport> ticketConsultTypeReport(Integer campaignId, String startTime, String endTime) {

		List<ConsultTypeReport> list = jzReportMapper.ticketConsultTypeReport(campaignId, startTime,
				endTime);

		//把 ticketType  statusName 拼在一起
		Map<String,ConsultTypeReport> dataMap=new HashMap<String, ConsultTypeReport>();

		for(ConsultTypeReport t:list) {
			String keys=t.getTicketType()+t.getStatusName();
			if(dataMap.containsKey(keys)) {

				ConsultTypeReport  cr = dataMap.get(keys);
				if(t.getConsultId()!=null) {
					cr.getConsultType().put(t.getConsultId(), t.getCounts());
				}else {
					cr.getConsultType().put(-1, t.getCounts());
				}
				

			}else {
				//不存在
				ConsultTypeReport cr=new ConsultTypeReport();
				cr.setTicketType(t.getTicketType());
				cr.setStatusName(t.getStatusName());
				Map<Object, Object> tp=new HashMap<Object, Object>();
				if(null!=t.getConsultId()) {
					tp.put(t.getConsultId(), t.getCounts());
				}else {
					tp.put(-1, t.getCounts());
				}
				cr.setConsultType(tp);
				dataMap.put(keys, cr);
			}
		}
		
		List<ConsultTypeReport> dataList = new ArrayList<ConsultTypeReport>(dataMap.values());

		return dataList;
	}

}
