StatisticsUtil.java 16.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
package com.espeed.reading.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

import net.sf.json.JSONObject;

/**
 * 
 * 
 * @项目名称: 邮件跟踪系统
 * @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
 * @技术支持: info@21gmail.com
 * @单元名称: 邮件跟踪记录统计工具类
 * @开始时间: 2018-5-28
 * @开发人员: 杨志钊
 */
public class StatisticsUtil {

	/** 获取记录详情 */
	public static Map<String, Object> getRecord(Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (id == null) {
			resultMap.put("code", 300);
			resultMap.put("msg", "id不能为空");
			return resultMap;
		}

		String sql = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSetMetaData metaData = null;

		try {
			conn = DBUtil.getConnection();

			sql = "select * from ts_customer_access where id = ?";

			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);

			rs = ps.executeQuery();

			if (rs.first()) {
				resultMap.put("code", 200);
				metaData = rs.getMetaData();
				int count = metaData.getColumnCount();
				String name = null;
				Object value = null;
				for (int i = 1; i <= count; i++) {
					name = metaData.getColumnName(i);
					value = rs.getObject(name);

					resultMap.put(name, value);
				}

				resultMap
						.put("language_details", LanguageUtil
								.getLanguage(String.valueOf(resultMap
										.get("language"))));

			} else {
				resultMap.put("code", 300);
				resultMap.put("msg", "不存在该记录");
				return resultMap;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, conn);
		}

		resultMap.put("code", 200);
		resultMap.put("msg", ConstantUtil.SUCCESS);
		return resultMap;
	}

	public static void main(String[] args) {
		System.out.println(JSONObject.fromObject(getCount(5, "showNew", "7438")));
	}

	/** 获取统计 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getCount(Integer type, String value,
			String userIds) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> result = null;
		List<Object> resultList = null;
		if (type == null) {
			resultMap.put("code", 300);
			resultMap.put("msg", "type不能为空");
			return resultMap;
		}

		String sql = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSetMetaData metaData = null;
		JSONObject jsonObject = null;
		int count = 0;
		String date = null;
		String preDate = null;
		String name = null;

		try {
			conn = DBUtil.getConnection();
			switch (type) {
			case 0:// 概况

				resultMap.put("today", StatisticsUtil.getDayCount(0, userIds,
						conn, ps, rs, metaData));
				resultMap.put("yesterday", StatisticsUtil.getDayCount(1,
						userIds, conn, ps, rs, metaData));
				resultMap.put("yesterday_now", StatisticsUtil.getDayCount(2,
						userIds, conn, ps, rs, metaData));
				resultMap.put("predict", StatisticsUtil.getDayCount(3, userIds,
						conn, ps, rs, metaData));
				resultMap.put("day_90", StatisticsUtil.getDayCount(5, userIds,
						conn, ps, rs, metaData));

				resultMap.put("total", StatisticsUtil.getDayCount(7, userIds,
						conn, ps, rs, metaData));

				// 历史最高
				result = new HashMap<String, Object>();
				result.put("pv", StatisticsUtil.getMaxCount(0, userIds, conn,
						ps, rs, metaData));// 浏览次数
				result.put("uv", StatisticsUtil.getMaxCount(1, userIds, conn,
						ps, rs, metaData));// 独立访客
				result.put("ip", StatisticsUtil.getMaxCount(2, userIds, conn,
						ps, rs, metaData));// ip
				result.put("sv", StatisticsUtil.getMaxCount(3, userIds, conn,
						ps, rs, metaData));// 访问次数,session会话数
				result.put("ev", StatisticsUtil.getMaxCount(4, userIds, conn,
						ps, rs, metaData));// 独立邮箱个数

				jsonObject = (JSONObject) result.get("uv");
				date = jsonObject.getString("date");

				// 查询新/旧独立访客
				sql = "SELECT COUNT(DISTINCT identity) from ts_customer_access WHERE create_time < ? "
						+ "and identity in(SELECT DISTINCT identity FROM ts_customer_access "
						+ "where user_id in("
						+ StringEscapeUtils.escapeSql(userIds)
						+ ") and create_time >= '"
						+ date
						+ " 00:00:00' and create_time <= '"
						+ date
						+ " 23:59:59')";

				ps = conn.prepareStatement(sql);
				ps.setString(1, date);

				rs = ps.executeQuery();

				if (rs.first()) {
					count = jsonObject.getInt("count");
					date = jsonObject.getString("date");

					jsonObject = new JSONObject();
					jsonObject.put("date", date);
					jsonObject.put("count", rs.getInt(1));

					result.put("old_uv", jsonObject);// 旧独立访客

					jsonObject = new JSONObject();
					jsonObject.put("date", date);
					jsonObject.put("count", count - rs.getInt(1));
					result.put("new_uv", jsonObject);// 新独立访客

				} else {
					jsonObject = new JSONObject();
					jsonObject.put("date",
							DateFormat.getNowDate().split(" ")[0]);
					jsonObject.put("count", 0);

					result.put("new_uv", jsonObject);// 新独立访客
					result.put("old_uv", jsonObject);// 旧独立访客
				}

				resultMap.put("max", result);

				break;
			case 1:// 今日按小时分组统计
			case 2:// 昨日按小时分组统计
			case 3:// 上周同期按小时分组统计
			case 4:// 近2日按小时分组统计
			case 5:// 近7日按小时分组统计
			case 6:// 近30日按小时分组统计
				if (type == 1) {
					date = "to_days(create_time) = to_days(now())";
					preDate = DateFormat.getNowDate().split(" ")[0];
					sql = "SELECT date_format(create_time, '%H') as date,COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev FROM ts_customer_access WHERE user_id in("
							+ StringEscapeUtils.escapeSql(userIds)
							+ ") and to_days(create_time) = to_days(now()) GROUP BY date_format(create_time, '%H')";
				} else if (type == 2) {
					date = "to_days(now()) - to_days(create_time) <= 1";
					preDate = DateFormat.getDateAfter(-1).split(" ")[0];
					sql = "SELECT date_format(create_time, '%H') as date,COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev FROM ts_customer_access WHERE user_id in("
							+ StringEscapeUtils.escapeSql(userIds)
							+ ") and to_days(now()) - to_days(create_time) <= 1 GROUP BY date_format(create_time, '%H')";
				} else if (type == 3) {
					preDate = DateFormat.getDateAfter(-7).split(" ")[0];

					sql = "SELECT date_format(create_time, '%H') as date,COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev FROM ts_customer_access WHERE user_id in("
							+ StringEscapeUtils.escapeSql(userIds)
							+ ") and create_time >= '" + preDate
							+ " 00:00:00' and create_time <= '" + preDate
							+ " 23:59:59' GROUP BY date_format(create_time, '%H')";
				} else if (type == 4) {
					sql = "SELECT date_format(create_time, '%Y-%m-%d %H') as date,COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev FROM ts_customer_access WHERE user_id in("
							+ StringEscapeUtils.escapeSql(userIds)
							+ ") and DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= date(create_time) GROUP BY date_format(create_time, '%Y-%m-%d %H')";
				} else if (type == 5) {
					sql = "SELECT date_format(create_time, '%Y-%m-%d %H') as date,COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev FROM ts_customer_access WHERE user_id in("
							+ StringEscapeUtils.escapeSql(userIds)
							+ ") and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time) GROUP BY date_format(create_time, '%Y-%m-%d %H')";
				} else if (type == 6) {
					sql = "SELECT date_format(create_time, '%Y-%m-%d') as date,COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev FROM ts_customer_access WHERE user_id in("
							+ StringEscapeUtils.escapeSql(userIds)
							+ ") and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) GROUP BY date_format(create_time, '%Y-%m-%d')";
				}

				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();

				resultList = new ArrayList<Object>();

				while (rs.next()) {
					result = new HashMap<String, Object>();
					metaData = rs.getMetaData();
					count = metaData.getColumnCount();
					for (int i = 1; i <= count; i++) {
						name = metaData.getColumnName(i);

						result.put(name, rs.getObject(name));
					}

					resultList.add(result);
				}
				
				if (value != null && value.equals("showNew") && resultList.size() > 0) {
					List<Object> newList = new ArrayList<Object>();
					
					for (Object object : resultList) {
						result = (Map<String, Object>) object;
						
						switch (type) {
						case 1:
						case 2:
						case 3:
							date = "create_time >= '" + preDate + result.get("date") + ":00:00"
							+ " 00:00:00' and create_time <= '" + preDate + result.get("date") + ":59:59'";
							
							preDate += " " + result.get("date") + ":00:00";
							
							
							break;
						case 4:
						case 5:
							date = "create_time >= '" + result.get("date") + ":00:00"
									+ " 00:00:00' and create_time <= '" + result.get("date") + ":59:59'";
							
							preDate = result.get("date") + ":00:00";
							break;
						case 6:
							date = "create_time >= '" + result.get("date") + " 00:00:00"
									+ " 00:00:00' and create_time <= '" + result.get("date") + " 23:59:59'";
							
							preDate = result.get("date") + " 00:00:00";
							break;
						}

						count = StatisticsUtil.getOldUv(userIds, preDate,
								date, conn, ps, rs);

						result.put("old_uv", count);
						result.put("new_uv", Integer.valueOf(result.get("uv").toString()) - count);
						
						newList.add(result);
					}
					
					resultList = newList;
				}

				resultMap.put("list", resultList);
				break;

			default:
				resultMap.put("code", 300);
				resultMap.put("msg", "不支持该业务类型");
				return resultMap;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, conn);
		}

		resultMap.put("code", 200);
		resultMap.put("msg", ConstantUtil.SUCCESS);
		return resultMap;
	}

	/** 以时间分组获取统计 */
	public static Map<String, Integer> getDayCount(Integer type,
			String userIds, Connection conn, PreparedStatement ps,
			ResultSet rs, ResultSetMetaData metaData) throws Exception {
		Map<String, Integer> resultMap = new HashMap<String, Integer>();

		resultMap.put("pv", 0);// 浏览次数
		resultMap.put("uv", 0);// 独立访客
		resultMap.put("ip", 0);// ip
		resultMap.put("new_uv", 0);// 新独立访客
		resultMap.put("old_uv", 0);// 旧独立访客
		resultMap.put("sv", 0);// 访问次数,session会话数
		resultMap.put("ev", 0);// 独立邮箱个数

		String sql = null;
		int count = 0;
		String name = null;
		String date = null;
		String preDate = null;

		switch (type) {
		case 0:// 今日概况
			date = "to_days(create_time) = to_days(now())";
			preDate = DateFormat.getNowDate().split(" ")[0];
			break;
		case 1:// 昨日概况
			date = "to_days(now()) - to_days(create_time) <= 1";
			preDate = DateFormat.getDateAfter(-1).split(" ")[0];
			break;
		case 2:// 昨日此时
			preDate = DateFormat.getDateAfter(-1);
			date = "create_time >= '" + preDate.split(" ")[0]
					+ " 00:00:00' and create_time <= '" + preDate + "'";
			preDate = preDate.split(" ")[0];
			break;
		case 3:// 前三日(用于预计)不算当天
			preDate = DateFormat.getDateAfter(-3).split(" ")[0];

			date = "create_time >= '" + preDate
					+ " 00:00:00' and create_time <= '"
					+ DateFormat.getDateAfter(-1).split(" ")[0] + " 23:59:59'";
			break;
		case 4:// 近七日
			date = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time)";
			preDate = DateFormat.getDateAfter(-7).split(" ")[0];
			break;
		case 5:// 近九十日
			date = "DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= date(create_time)";
			preDate = DateFormat.getDateAfter(-90).split(" ")[0];
			break;
		case 6:// 历史最高
			sql = "SELECT date_format(create_time, '%Y-%m-%d') AS create_time,COUNT(*) AS count from ts_customer_access where user_id in("
					+ StringEscapeUtils.escapeSql(userIds)
					+ ") group by date_format(create_time, '%Y-%m-%d') ORDER BY count desc LIMIT 1";

			ps = conn.prepareStatement(sql);

			rs = ps.executeQuery();

			if (rs.first()) {
				preDate = rs.getString("create_time");
				date = "create_time >= '" + preDate
						+ " 00:00:00' and create_time <= '" + preDate
						+ " 23:59:59'";
			} else {
				date = "to_days(create_time) = to_days(now())";
				preDate = DateFormat.getNowDate().split(" ")[0];
			}
			break;
		case 7:// 历史累计
			date = "1 = 1";
			preDate = DateFormat.getDateAfter(1).split(" ")[0];
			break;
		}

		sql = "select COUNT(*) as pv,COUNT(DISTINCT identity) as uv,COUNT(DISTINCT ip) as ip,COUNT(DISTINCT jsession_id) as sv,COUNT(DISTINCT access_email) as ev "
				+ "from ts_customer_access where user_id in("
				+ StringEscapeUtils.escapeSql(userIds) + ") and " + date;

		ps = conn.prepareStatement(sql);

		rs = ps.executeQuery();

		if (rs.first()) {
			metaData = rs.getMetaData();
			count = metaData.getColumnCount();
			for (int i = 1; i <= count; i++) {
				name = metaData.getColumnName(i);

				if (type == 3) {
					resultMap.put(name,
							StatisticsUtil.divided(rs.getInt(name), 3));
				} else if (type == 5) {
					resultMap.put(name,
							StatisticsUtil.divided(rs.getInt(name), 90));
				} else {
					resultMap.put(name, rs.getInt(name));
				}
			}

			// 查询新/旧独立访客
			sql = "SELECT COUNT(DISTINCT identity) from ts_customer_access WHERE create_time < ? "
					+ "and identity in(SELECT DISTINCT identity FROM ts_customer_access "
					+ "where user_id in("
					+ StringEscapeUtils.escapeSql(userIds)
					+ ") and "
					+ date
					+ ")";

			ps = conn.prepareStatement(sql);
			ps.setString(1, preDate);

			rs = ps.executeQuery();

			if (rs.first()) {
				count = rs.getInt(1);

				if (type == 3) {
					resultMap.put("old_uv", StatisticsUtil.divided(count, 3));
				} else if (type == 5) {
					resultMap.put("old_uv", StatisticsUtil.divided(count, 90));
				} else {
					resultMap.put("old_uv", count);
				}

				resultMap.put("new_uv",
						resultMap.get("uv") - resultMap.get("old_uv"));
			}

		}

		return resultMap;
	}

	/** 相除有余数加一 */
	private static int divided(int x, int y) {

		return x % y == 0 ? x / y : (x / y) + 1;
	}

	/** 获取历史最高值 */
	public static JSONObject getMaxCount(Integer type, String userIds,
			Connection conn, PreparedStatement ps, ResultSet rs,
			ResultSetMetaData metaData) throws Exception {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("date", DateFormat.getNowDate().split(" ")[0]);
		jsonObject.put("count", 0);

		String sql = null;
		String countType = null;

		switch (type) {
		case 0:
			countType = "*";
			break;
		case 1:
			countType = "DISTINCT identity";
			break;
		case 2:
			countType = "DISTINCT ip";
			break;
		case 3:
			countType = "DISTINCT jsession_id";
			break;
		case 4:
			countType = "DISTINCT access_email";
			break;

		}

		sql = "SELECT date_format(create_time, '%Y-%m-%d') AS create_time,COUNT("
				+ countType
				+ ") AS count from ts_customer_access where user_id in("
				+ StringEscapeUtils.escapeSql(userIds)
				+ ") group by date_format(create_time, '%Y-%m-%d') ORDER BY count desc LIMIT 1";

		ps = conn.prepareStatement(sql);

		rs = ps.executeQuery();

		if (rs.first()) {
			jsonObject = new JSONObject();
			jsonObject.put("date", rs.getString("create_time"));
			jsonObject.put("count", rs.getInt("count"));
		}

		return jsonObject;
	}

	/** 获取旧访客数量 */
	public static int getOldUv(String userIds, String preDate, String date,
			Connection conn, PreparedStatement ps, ResultSet rs)
			throws Exception {
		int uv = 0;
		// 查询新/旧独立访客
		String sql = "SELECT COUNT(DISTINCT identity) from ts_customer_access WHERE create_time <= ? "
				+ "and identity in(SELECT DISTINCT identity FROM ts_customer_access "
				+ "where user_id in("
				+ StringEscapeUtils.escapeSql(userIds)
				+ ") and " + date + ")";

		ps = conn.prepareStatement(sql);
		ps.setString(1, preDate);

		rs = ps.executeQuery();

		if (rs.first()) {
			uv = rs.getInt(1);
		}

		return uv;
	}

}