package com.nbclass.szxgl.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nbclass.exception.ParameterException;
import com.nbclass.system.model.User;
import com.nbclass.szxgl.constant.ActivityConstants;
import com.nbclass.szxgl.mapper.GraphicStatisticsMapper;
import com.nbclass.szxgl.service.GraphicStatisticsService;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class GraphicStatisticsServiceImpl implements GraphicStatisticsService {

	@Resource
	private GraphicStatisticsMapper mapper;

	@Override
	public JSONObject getProjectNumDataByPie(String starttime, String endtime) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		if (user == null) {
			throw new ParameterException("登录失效，请重新登录!");
		}
		String username = user.getUsername();
		if (ActivityConstants.isAdmin(user.getUsername()))
			username = "admin";

		JSONObject result = new JSONObject();
		List<Map<String, Object>> list = mapper.getProjectNumDataByPie(starttime, endtime, username);
		result.put("list", list);
		return result;
	}

	@Override
	public JSONObject getProjectNumDataByLine(String starttime, String endtime) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		if (user == null) {
			throw new ParameterException("登录失效，请重新登录!");
		}
		String username = user.getUsername();
		if (ActivityConstants.isAdmin(user.getUsername()))
			username = "admin";

		JSONObject result = new JSONObject();
		ArrayList<String> legendData = new ArrayList<String>();
		ArrayList<String> xAxisData = new ArrayList<String>();
		JSONArray seriesData = new JSONArray();

		Map<String, ArrayList<Long>> areaNums = new LinkedHashMap<String, ArrayList<Long>>(); // 使用LinkedHashMap按插入顺序排序
		List<Map<String, Object>> list = mapper.getProjectNumDataByLine(starttime, endtime, username);
		if (!list.isEmpty()) {
			for (Map<String, Object> map : list) {
				String weeks = (String) map.get("weeks");
				String area = (String) map.get("area");
				long num = (long) map.get("num");
				if (!xAxisData.contains(weeks))
					xAxisData.add(weeks);

				if (areaNums.containsKey(area)) {

					areaNums.get(area).add(num);
				} else {
					ArrayList<Long> tmpList = new ArrayList<Long>();
					tmpList.add(num);
					areaNums.put(area, tmpList);
				}
			}
			Iterator<Entry<String, ArrayList<Long>>> it = areaNums.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, ArrayList<Long>> entry = it.next();
				legendData.add(entry.getKey());
				JSONObject seriesObj = new JSONObject();
				seriesObj.put("name", entry.getKey());
				seriesObj.put("type", "line");
				seriesObj.put("data", entry.getValue());
				seriesData.add(seriesObj);
			}
		}
		result.put("legendData", legendData);
		result.put("xAxisData", xAxisData);
		result.put("seriesData", seriesData);
		return result;
	}

	public JSONObject queryPersonProject(String starttime, String endtime,String deptId,String userId) {
//		User user = (User) SecurityUtils.getSubject().getPrincipal();
//		if(user == null){
//			throw new ParameterException("登录失效，请重新登录!");
//		}
//		String username = user.getUsername(); 
//		if(ActivityConstants.isAdmin(user.getUsername()))username="admin";

		JSONObject result = new JSONObject();
		List<Map<String, Object>> list = mapper.queryPersonProject(starttime, endtime,deptId,userId);
		result.put("list", list);
		return result;
	}

	public JSONObject queryMonthProjectLine(String year, String deptId) {
		JSONObject result = new JSONObject();
		List<Map> monthList = mapper.queryMonthProjectLine(year, StringUtils.isBlank(deptId) ? null : deptId);
		List<Integer> list = new ArrayList();
		Calendar now = Calendar.getInstance();
		String yearStr = now.get(Calendar.YEAR) + "";
		int month = now.get(Calendar.MONTH) + 1;
		if (yearStr.equals(year)) {
			for (int i = 1; i <= month && i <= 12; i++) {
				list.add(0);
			}
		} else {
			for (int i = 1;i <= 12; i++) {
				list.add(0);
			}
		}
		for (Map map : monthList) {
			for (int i = 1; i <= 12; i++) {
				if (map.get("month") != null && Integer.parseInt(map.get("month").toString()) == i)
					list.set(i - 1, Integer.parseInt(map.get("num").toString()));
			}
		}
		result.put("list", list);
		return result;
	}

	@Override
	public JSONObject queryDepartmentLable() {
		JSONObject result = new JSONObject();
		List<Map> list = mapper.queryDepartmentLable();
		result.put("list", list);
		return result;
	}

}
