package com.pj.project.sta_data;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pj.current.satoken.AuthConst;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;

import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 统计数据相关接口
 * @author kong
 *
 */
@RestController
@RequestMapping("/StaData/")
public class StaDataController {

	// 数据统计需要的权限 
	static final String CONSOLE_PLATE = "console-plate";
	
	@Autowired
	StaDataMapper staDataMapper;
	
	// 返回统计数据 
	@RequestMapping("getStaData")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getStaData() {
		SoMap soMap = staDataMapper.getStaData();
		return AjaxJson.getSuccessData(soMap);
	}

	// 指定年份每月用户登录量
	@RequestMapping("getUserLoginSta")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getUserLoginSta(String year){
		List<SoMap> mapList = staDataMapper.getUserLoginSta(year);
		return AjaxJson.getSuccessData(ref(mapList));
	}

	// 指定月份每日用户登录量
	@RequestMapping("getUserLoginStaByMonth")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getUserLoginStaByMonth(int year, int month){
		String yearMonth = month > 10 ? (year + "-" + month) : (year + "-0" + month);
		List<SoMap> mapList = staDataMapper.getUserLoginStaByMonth(yearMonth);
		return AjaxJson.getSuccessData(ref2(year, month, mapList));
	}

	// 查询总登录数据，按照url分组 
	@RequestMapping("getUserLoginStaByUrlGroup")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getUserLoginStaByUrlGroup(){
		List<SoMap> mapList = staDataMapper.getUserLoginStaByUrlGroup();
		return AjaxJson.getSuccessData(mapList);
	}

	// 查询总登录数据，按照client分组 
	@RequestMapping("getUserLoginStaByClientGroup")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getUserLoginStaByClientGroup(){
		List<SoMap> mapList = staDataMapper.getUserLoginStaByClientGroup();
		return AjaxJson.getSuccessData(mapList);
	}
	
	// 指定年份每月用户注册量
	@RequestMapping("getUserRegSta")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getUserRegSta(String year){
		List<SoMap> mapList = staDataMapper.getUserRegSta(year);
		return AjaxJson.getSuccessData(ref(mapList));
	}

	// 指定时间段的用户登录数据 
	// f 时间格式（1=年月，2=月日）
	@RequestMapping("getLoginData")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getLoginData(String startTime, String endTime, int f){
		String dateFormat = (f == 1 ? "%Y-%m" : "%m-%d");
		List<SoMap> mapList = staDataMapper.getLoginData(startTime, endTime, dateFormat);
		return AjaxJson.getSuccessData(refList(mapList));
	}

	// 指定时间段的用户注册数据 
	// f 时间格式（1=年月，2=月日）
	@RequestMapping("getRegData")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getRegData(String startTime, String endTime, int f){
		String dateFormat = (f == 1 ? "%Y-%m" : "%m-%d");
		List<SoMap> mapList = staDataMapper.getRegData(startTime, endTime, dateFormat);
		return AjaxJson.getSuccessData(refList(mapList));
	}

	// 聚合以上两个接口 
	@RequestMapping("getRegLoginData")
	@SaCheckPermission({AuthConst.IN_SYSTEM, CONSOLE_PLATE})
	public AjaxJson getRegLoginData(String startTime, String endTime, int f){
		return AjaxJson.getSuccess()
				.set("regData", getRegData(startTime, endTime, f).getData())
				.set("loginData", getLoginData(startTime, endTime, f).getData())
				;
	}

	
	
	

	// 将纵向列旋转一下 
	private static SoMap refList(List<SoMap> mapList) {
		SoMap dataMap = new SoMap();
		// 赋值 
		for (SoMap map : mapList) {
			String key = map.getString("name");
			Object value = map.get("count");
			dataMap.put(key, value); 
		}
		// 返回
		return dataMap;
		
		
	}
	
	// 将纵向列选转一下，并配上月份 
	private static SoMap ref(List<SoMap> mapList) {
		// 初始化 
		SoMap dataMap = new SoMap();
		for (int i = 1; i <= 12; i++) {
			String m = i+ "月";
//			if(i < 10) {
//				m = "0" + m;
//			}
			dataMap.put(m, 0);
		}
		// 赋值 
		for (SoMap map : mapList) {
			String key = map.get("m") + "月";
			if(key.startsWith("0")) {
				key = key.substring(1);
			}
			dataMap.put(key, map.get("count")); 
		}
		// 返回
		return dataMap;
	}

	// 将纵向列选转一下，并配上日 
	private static SoMap ref2(int year, int month, List<SoMap> mapList) {
		int days = getDays(year, month);
		// 初始化 
		SoMap dataMap = new SoMap();
		for (int i = 1; i <= days; i++) {
			String d = month + "-" + i;
			dataMap.put(d, 0);
		}
		// 赋值 
		for (SoMap map : mapList) {
			String key = map.getString("d");
			if(key.startsWith("0")) {
				key = key.substring(1);
			}
			key = month + "-" + key;
			dataMap.put(key, map.get("count")); 
		}
		// 返回
		return dataMap;
	}
	
	// 获取指定年份月份有多少天 
	private static int getDays(int year, int month) {
		// 获取日历对象
		Calendar c = Calendar.getInstance();
		// 设置日历对象为指定年月日，为指定月份的第一天
		c.set(year, month, 1);
		// 设置日历对象，指定月份往前推一天，也就是最后一天
		c.add(Calendar.DATE, -1);
		// 获取这一天输出即可
		int day = c.get(Calendar.DATE);
		return day;
	}

}
