//package com.palmnest.webapp.controller;
//
//import org.apache.commons.lang3.StringUtils;
//import org.apache.poi.hssf.usermodel.*;
//import org.apache.poi.hssf.util.HSSFColor;
//import org.junit.Test;
//
//import java.io.*;
//import java.sql.*;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class UserActiveCountTest{
//
//
//	/**
//	 * 平台用户活跃度
// 	 */
//	@Test
//	public void test2() throws Exception {
//
//		long time1 = System.currentTimeMillis();
//
//		String startTime = "'2019-04-01 00:00:00'";
//		String endTime = "'2019-04-30 23:59:59'";
//		List<HashMap> resultMap = new ArrayList<>();
//
//		// 1.查询出所有上线的项目列表
//		String orgSql = "SELECT org.id,org.`name`,org.accountType FROM tb_organization org WHERE org.isEnabled=1 AND org.ID in (SELECT organizationId FROM tb_user WHERE isEnabled=1 AND organizationId is NOT NULL GROUP BY organizationId HAVING COUNT(id) > 1)";
//		List<HashMap> orgList = jdbcConnection(orgSql);
//
//		// 2.所有服务列表
//		String serverSql = "SELECT serv.id,serv.serviceName,serv.`code` FROM tb_servicetypeinfo serv WHERE serv.isEnabled=1 AND serv.serviceStatus=0 AND serv.serviceType=0";
//		List<HashMap> serverList = jdbcConnection(serverSql);
//
//		// 3.循环遍历
//		for (HashMap map : orgList){
//			HashMap subMap = new HashMap();
//			// 项目名称
//			subMap.put("项目",map.get("name"));
//
//			// 账号类型
//			subMap.put("账号类型",(int)map.get("accountType") == 0 ?"企业" :"个人");
//
//			// 统计时间
//			subMap.put("统计时间",startTime + "至" + endTime);
//
//			// 用户总量
//			String totalUserCountSql = "SELECT COUNT(*) total FROM tb_user tu WHERE tu.isEnabled=1 AND tu.userType<>2 AND tu.organizationId="+map.get("ID") + " and tu.createTime <=" + endTime;
//			List<HashMap> list = jdbcConnection(totalUserCountSql);
//			subMap.put("用户总数",list.get(0).get("total"));
//
//			//新增用户
//			String addUserCountSql = "SELECT COUNT(*) total FROM tb_user tu WHERE tu.isEnabled=1 AND tu.userType<>2 AND tu.organizationId="+map.get("ID") +" AND tu.createTime >= "+startTime+" AND tu.createTime <="+endTime;
//			List<HashMap> list2 = jdbcConnection(addUserCountSql);
//			subMap.put("新增用户",list2.get(0).get("total"));
//
//			// 企业数量
//			String totalVirCountSql = "SELECT COUNT(*) total FROM tb_virtualarchitecture WHERE isEnabled=1 AND organizationId="+map.get("ID");
//			List<HashMap> list5 = jdbcConnection(totalVirCountSql);
//			subMap.put("企业数量",list5.get(0).get("total"));
//
//			// 新增企业
//			String addVirCountSql = "SELECT COUNT(*) total FROM tb_virtualarchitecture WHERE isEnabled=1 AND createTime >= "+startTime+" AND createTime <= "+endTime +"AND organizationId="+map.get("ID");
//			List<HashMap> list6 = jdbcConnection(addVirCountSql);
//			subMap.put("新增企业",list6.get(0).get("total"));
//
//			// 平台活跃度
//			String orgActiveCountSql = "SELECT SUM(tuac.count) total FROM tb_user_active_count tuac WHERE tuac.date >= "+startTime+" AND tuac.date <= "+endTime+" AND tuac.serviceCode='total' AND tuac.orgId="+map.get("ID");
//			List<HashMap> list3 = jdbcConnection(orgActiveCountSql);
//			subMap.put("平台活跃",list3.get(0).get("total"));
//
//			// 员工通行数
//			String employeePassSql = "SELECT COUNT(*) FROM (SELECT MAX(id) FROM	tb_lingling_log WHERE createTime >= "+startTime+" AND createTime <= "+endTime+" AND type <> 4 AND mobile IS NOT NULL GROUP BY mobile) t1 ";
//			List<HashMap> list7 = jdbcConnection(orgActiveCountSql);
//			subMap.put("员工通行",list7.get(0).get("total"));
//
//			// 各个模块活跃度
//			for (HashMap map2 : serverList){
//				String moduleActiveCountSql = "SELECT SUM(tuac.count) total FROM tb_user_active_count tuac WHERE tuac.date >= "+startTime+" AND tuac.date <= "+endTime+" AND tuac.serviceCode='"+map2.get("code")+"' AND tuac.orgId="+map.get("ID");
//				List<HashMap> list4 = jdbcConnection(moduleActiveCountSql);
//				subMap.put(map2.get("serviceName"),list4.get(0).get("total"));
//			}
//
//			resultMap.add(subMap);
//		}
//
//
////		ExcelUtils.exportExcel(file,wb, sheetName, list, titles, fieldNames);
//
//		HSSFWorkbook wb = new HSSFWorkbook();
//		HSSFSheet sheet = wb.createSheet();
//
//		// 创建第一行,填充表头
//		HSSFRow row = sheet.createRow(0);
//
//		ArrayList<String> list = new ArrayList<String>();
////		list.add("序号");
//		list.add("项目");
//		list.add("账号类型");
//		list.add("统计时间");
//		list.add("用户总数");
//		list.add("新增用户");
//		list.add("企业数量");
//		list.add("新增企业");
//		list.add("平台活跃");
////		String[] headers = { "序号", "项目","用户总数","新增用户","平台活跃"};
////		int i = headers.length;
//		for (HashMap map2 : serverList){
////			headers[i] = (String) map2.get("serviceName");
////			i++;
//			list.add((String) map2.get("serviceName"));
//		}
//
//		String[] headers = list.toArray(new String[list.size()]);
//		for (int j = 0; j < headers.length; j++) {
//			//创建单元格，每行多少数据就创建多少个单元格
//			HSSFCell cell = row.createCell(j);
//			HSSFRichTextString text = new HSSFRichTextString(headers[j]);
//			//给单元格设置内容
//			cell.setCellValue(text);
//		}
//
//
//		//遍历集合，将每个集合元素对象的每个值填充到单元格中
//		for (int j = 0;j<resultMap.size();j++){
//			row = sheet.createRow(j+1);
//			HashMap map = resultMap.get(j);
//
//			for (int k=0;k<list.size();k++) {
//				String count=  map.get(list.get(k)) == null ? "0" : map.get(list.get(k)).toString();
//				HSSFCell cell = row.createCell(k);
//				HSSFRichTextString richString = new HSSFRichTextString(count);
//				if (!"0".equals(count) && StringUtils.isNumeric(count)){
//					HSSFFont font3 = wb.createFont();
//					//定义Excel数据颜色，这里设置为蓝色
//					font3.setColor(HSSFColor.BLUE.index);
//					richString.applyFont(font3);
//				}
//				cell.setCellValue(richString);
//			}
//		}
//
//		// 输出excel
//		ByteArrayOutputStream os = new ByteArrayOutputStream();
//		try{
//			wb.write(os);
//		}
//		catch (IOException e){
//			e.printStackTrace();
//		}
//		byte[] content = os.toByteArray();
//		//Excel文件生成后存储的位置。
//		File file = new File("C:\\Users\\HIAPAD\\Desktop\\活跃数据统计.xls");
//		OutputStream fos  = null;
//		try{
//			fos = new FileOutputStream(file);
//			fos.write(content);
//			os.close();
//			fos.close();
//		}
//		catch (Exception e){
//			e.printStackTrace();
//		}
//
//		System.out.println(System.currentTimeMillis() - time1);
//		conn.close();
//	}
//
//	public static Connection conn = null;
//
//	static {
//		// 1、注册驱动
//		try {
//			Class.forName("com.mysql.jdbc.Driver");
//			// 2、创建一个连接对象
//			// 测试环境
////			conn = DriverManager.getConnection("jdbc:mysql://rm-wz949306t03an8m8s4o.mysql.rds.aliyuncs.com:3306/louyu_service_test", "lyzc_test138", "luyzc#!138Server");
//			// 正式环境
//			conn = DriverManager.getConnection("jdbc:mysql://rm-wz949306t03an8m8s4o.mysql.rds.aliyuncs.com/super_service", "superservice", "Sservice#!0710");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//	}
//
//	public List jdbcConnection(String sql) throws Exception {
//
//		// 3、创建一个sql语句发送的命令对象
//		Statement stmt = conn.createStatement();
//
//		// 4、执行SQL,拿到查询的结果集对象
//		ResultSet rs = stmt.executeQuery(sql);
//
//		// 5、输出结果集的数据
////		while (rs.next()) {
////			System.out.println("id :" + rs.getInt("id") + " name :" + rs.getString("name"));
////		}
//		List list = convertList(rs);
//
//		// 6、关闭链接、命令对象、结果集
//		rs.close();
//		stmt.close();
////		conn.close();
//
//		return list;
//	}
//
//
//
//	private static List convertList(ResultSet rs) throws SQLException {
//
//		List list = new ArrayList();
//
//		ResultSetMetaData md = rs.getMetaData();
//
//		int columnCount = md.getColumnCount(); //Map rowData;
//
//		while (rs.next()) { //rowData = new HashMap(columnCount);
//
//			Map rowData = new HashMap();
//
//			for (int i = 1; i <= columnCount; i++) {
//
//				rowData.put(md.getColumnName(i), rs.getObject(i));
//
//			}
//
//			list.add(rowData);
//
//		} return list;
//
//	}
//
//
//
//	@Test
//	public void test5() throws Exception{
//
//		// 3、创建一个sql语句发送的命令对象
////		Statement stmt = conn.createStatement();
////
////		String sql = "SELECT ID FROM tb_user WHERE organizationId=649 AND isEnabled=1 AND userType=0";
////		List<Map> list = jdbcConnection(sql);
////
////		for (Map id : list){
////			Integer idStr = (Integer) id.get("ID");
////				String insertSql = "INSERT INTO tr_perm_group_user VALUES(6,"+idStr+")";
////				// 4、执行SQL,拿到查询的结果集对象
////				stmt.executeUpdate(insertSql);
////
////		}
////
////		stmt.close();
////		System.out.println(list.size());
//	}
//}
