package com.yeshi.fanli.controller.admin; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.yeshi.utils.JsonUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.yeshi.fanli.controller.admin.utils.AdminUtils; import com.yeshi.fanli.entity.admin.count.DailyCountCoupon; import com.yeshi.fanli.entity.admin.count.DailyCountMoments; import com.yeshi.fanli.entity.admin.count.DailyCountOrder; import com.yeshi.fanli.entity.admin.count.DailyCountUser; import com.yeshi.fanli.service.inter.count.DailyCountCouponService; import com.yeshi.fanli.service.inter.count.DailyCountMomentsService; import com.yeshi.fanli.service.inter.count.DailyCountOrderService; import com.yeshi.fanli.service.inter.count.DailyCountUserService; import com.yeshi.fanli.util.MoneyBigDecimalUtil; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.TimeUtil; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Controller @RequestMapping("admin/new/api/v1/countCharts") public class CountChartsAdminController { @Resource private DailyCountCouponService dailyCountCouponService; @Resource private DailyCountUserService dailyCountUserService; @Resource private DailyCountOrderService dailyCountOrderService; @Resource private DailyCountMomentsService dailyCountMomentsService; /** * 订单相关图表数据 * @param callback * @param dateType * @param year * @param startTime * @param endTime * @param type 查询类型 * @param out */ @RequestMapping(value = "getOrderCharts") public void getOrderCharts(String callback, Integer dateType, String year, String startTime, String endTime, String typeArray, String channelArray, PrintWriter out) { try { // 数据验证处理 verifyQueryData(callback, dateType, year, startTime, endTime, out); Date beginDate = getBeginDate(dateType, year, startTime); Date endDate = getEndDate(dateType, year, endTime); Gson gson = new Gson(); List dateList = AdminUtils.getDateList(dateType, startTime, endTime, year); // 渠道 List typelList = null; if (typeArray != null && typeArray.trim().length() > 4) { typelList = gson.fromJson(typeArray, new TypeToken>() {}.getType()); } if (typelList == null || typelList.size() == 0) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("传递类型错误")); return; } // 渠道 List channelList = null; if (channelArray != null && channelArray.trim().length() > 4) { channelList = gson.fromJson(channelArray, new TypeToken>() {}.getType()); } JSONArray line_list = new JSONArray(); if (channelList != null && channelList.size() > 0) { String type = typelList.get(0); for (String channel: channelList) { List listData = dailyCountOrderService.getDailyCountList(type, beginDate, endDate, channel); // 处理数据 List list = machineDailyCountOrder(dateList, dateType, listData); JSONObject innerList = new JSONObject(); innerList.put("name", channel); innerList.put("data", gson.toJson(list)); line_list.add(innerList); } } else { for (String type: typelList) { List listData = dailyCountOrderService.getDailyCountList(type, beginDate, endDate, null); // 处理数据 List list = machineDailyCountOrder(dateList, dateType, listData); JSONObject innerList = new JSONObject(); innerList.put("name", dailyCountOrderService.getTypeEnumDesc(type)); innerList.put("data", gson.toJson(list)); line_list.add(innerList); } } JSONObject data = new JSONObject(); data.put("line_list", line_list); data.put("xAxis_list", gson.toJson(dateList)); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("系统异常")); e.printStackTrace(); } } private List machineDailyCountOrder(List dateList, Integer dateType, List listData) { List list = new ArrayList<>(); for (String date: dateList) { BigDecimal result = new BigDecimal(0); if (listData != null) { BigDecimal totalDay = new BigDecimal(0); BigDecimal totalValid = new BigDecimal(0); for (DailyCountOrder history: listData) { if (dateType == 1) { String gernalTime = TimeUtil.getGernalTime(history.getDay().getTime()); if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); continue; } } else if (dateType == 2){ String gernalTime = TimeUtil.getMonthOnlyMM(history.getDay()); if(gernalTime.startsWith("0")) { gernalTime = gernalTime.substring(1, 2); } if (gernalTime.equalsIgnoreCase(date)) { if (!history.isRate()) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } else { totalDay = MoneyBigDecimalUtil.add(totalDay, history.getTotalDay()); totalValid = MoneyBigDecimalUtil.add(totalValid, history.getTotalValid()); } } } else if (dateType == 3) { String gernalTime = TimeUtil.getYearOnlyYYYY(history.getDay()); if (gernalTime.equalsIgnoreCase(date)) { if (!history.isRate()) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } else { totalDay = MoneyBigDecimalUtil.add(totalDay, history.getTotalDay()); totalValid = MoneyBigDecimalUtil.add(totalValid, history.getTotalValid()); } } } } // 判断是否计算比例 if ((dateType == 2 || dateType == 3) && totalDay.compareTo(BigDecimal.valueOf(0)) > 0){ result = MoneyBigDecimalUtil.div(totalValid, totalDay); result = MoneyBigDecimalUtil.mul(result, BigDecimal.valueOf(100)); } } list.add(result); } return list; } /** * 用户相关图表数据 * @param callback * @param dateType * @param year * @param startTime * @param endTime * @param type 查询类型 * @param out */ @RequestMapping(value = "getUserCharts") public void getUserCharts(String callback, Integer dateType, String year, String startTime, String endTime, String typeArray, String channelArray, PrintWriter out) { try { // 数据验证处理 verifyQueryData(callback, dateType, year, startTime, endTime, out); Date beginDate = getBeginDate(dateType, year, startTime); Date endDate = getEndDate(dateType, year, endTime); Gson gson = new Gson(); List dateList = AdminUtils.getDateList(dateType, startTime, endTime, year); List typelList = null; if (typeArray != null && typeArray.trim().length() > 4) { typelList = gson.fromJson(typeArray, new TypeToken>() {}.getType()); } if (typelList == null || typelList.size() == 0) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("传递类型错误")); return; } // 渠道 List channelList = null; if (channelArray != null && channelArray.trim().length() > 4) { channelList = gson.fromJson(channelArray, new TypeToken>() {}.getType()); } JSONArray line_list = new JSONArray(); if (channelList != null && channelList.size() > 0) { String type = typelList.get(0); for (String channel: channelList) { List listData = dailyCountUserService.getDailyCountList(type, beginDate, endDate, channel); // 处理数据 List list = machineDailyCountUser(dateList, dateType, listData); JSONObject innerList = new JSONObject(); innerList.put("name", channel); innerList.put("data", gson.toJson(list)); line_list.add(innerList); } } else { for (String type: typelList) { List listData = dailyCountUserService.getDailyCountList(type, beginDate, endDate, null); // 处理数据 List list = machineDailyCountUser(dateList, dateType, listData); JSONObject innerList = new JSONObject(); innerList.put("name", dailyCountUserService.getTypeEnumDesc(type)); innerList.put("data", gson.toJson(list)); line_list.add(innerList); } } JSONObject data = new JSONObject(); data.put("line_list", line_list); data.put("xAxis_list", gson.toJson(dateList)); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("系统异常")); e.printStackTrace(); } } private List machineDailyCountUser(List dateList, Integer dateType, List listData) { List list = new ArrayList<>(); for (String date: dateList) { BigDecimal result = new BigDecimal(0); if (listData != null) { BigDecimal totalDay = new BigDecimal(0); BigDecimal totalValid = new BigDecimal(0); for (DailyCountUser history: listData) { if (dateType == 1) { String gernalTime = TimeUtil.getGernalTime(history.getDay().getTime()); if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); continue; } } else if (dateType == 2){ String gernalTime = TimeUtil.getMonthOnlyMM(history.getDay()); if(gernalTime.startsWith("0")) { gernalTime = gernalTime.substring(1, 2); } if (gernalTime.equalsIgnoreCase(date)) { if (!history.isRate()) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } else { totalDay = MoneyBigDecimalUtil.add(totalDay, history.getTotalDay()); totalValid = MoneyBigDecimalUtil.add(totalValid, history.getTotalValid()); } } } else if (dateType == 3) { String gernalTime = TimeUtil.getYearOnlyYYYY(history.getDay()); if (gernalTime.equalsIgnoreCase(date)) { if (!history.isRate()) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } else { totalDay = MoneyBigDecimalUtil.add(totalDay, history.getTotalDay()); totalValid = MoneyBigDecimalUtil.add(totalValid, history.getTotalValid()); } } } } // 判断是否计算比例 if ((dateType == 2 || dateType == 3) && totalDay.compareTo(BigDecimal.valueOf(0)) > 0){ result = MoneyBigDecimalUtil.div(totalValid, totalDay); result = MoneyBigDecimalUtil.mul(result, BigDecimal.valueOf(100)); } } list.add(result); } return list; } /** * 券相关图表数据 * @param callback * @param dateType * @param year * @param startTime * @param endTime * @param type 查询类型 * @param out */ @RequestMapping(value = "getCouponCharts") public void getCouponCharts(String callback, Integer dateType, String year, String startTime, String endTime, String typeArray, PrintWriter out) { try { // 数据验证处理 verifyQueryData(callback, dateType, year, startTime, endTime, out); Date beginDate = getBeginDate(dateType, year, startTime); Date endDate = getEndDate(dateType, year, endTime); Gson gson = new Gson(); List dateList = AdminUtils.getDateList(dateType, startTime, endTime, year); // 渠道 List typelList = null; if (typeArray != null && typeArray.trim().length() > 4) { typelList = gson.fromJson(typeArray, new TypeToken>() {}.getType()); } JSONArray line_list = new JSONArray(); if (typelList != null && typelList.size() > 0) { for (String type: typelList) { List listData = dailyCountCouponService.getDailyCountList(type, beginDate, endDate); // 处理数据 List list = machineDailyCountCoupon(dateList, dateType, listData); JSONObject innerList = new JSONObject(); innerList.put("name", dailyCountCouponService.getTypeEnumDesc(type)); innerList.put("data", gson.toJson(list)); line_list.add(innerList); } } JSONObject data = new JSONObject(); data.put("line_list", line_list); data.put("xAxis_list", gson.toJson(dateList)); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("系统异常")); e.printStackTrace(); } } private List machineDailyCountCoupon(List dateList, Integer dateType, List listData) { List list = new ArrayList<>(); for (String date: dateList) { BigDecimal result = new BigDecimal(0); if (listData != null) { for (DailyCountCoupon history: listData) { if (dateType == 1) { String gernalTime = TimeUtil.getGernalTime(history.getDay().getTime()); if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); continue; } } else if (dateType == 2){ String gernalTime = TimeUtil.getMonthOnlyMM(history.getDay()); if(gernalTime.startsWith("0")) { gernalTime = gernalTime.substring(1, 2); } if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } } else if (dateType == 3) { String gernalTime = TimeUtil.getYearOnlyYYYY(history.getDay()); if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } } } } list.add(result); } return list; } @RequestMapping(value = "getMomentsCharts") public void getMomentsCharts(String callback, Integer dateType, String year, String startTime, String endTime, String typeArray, PrintWriter out) { try { // 数据验证处理 verifyQueryData(callback, dateType, year, startTime, endTime, out); Date beginDate = getBeginDate(dateType, year, startTime); Date endDate = getEndDate(dateType, year, endTime); Gson gson = new Gson(); List dateList = AdminUtils.getDateList(dateType, startTime, endTime, year); // 渠道 List typelList = null; if (typeArray != null && typeArray.trim().length() > 4) { typelList = gson.fromJson(typeArray, new TypeToken>() {}.getType()); } if (typelList == null || typelList.size() == 0) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("传递类型错误")); return; } JSONArray line_list = new JSONArray(); for (String type: typelList) { List listData = dailyCountMomentsService.getDailyCountList(type, beginDate, endDate); // 处理数据 List list = machineDailyCountMoments(dateList, dateType, listData); JSONObject innerList = new JSONObject(); innerList.put("name", dailyCountMomentsService.getTypeEnumDesc(type)); innerList.put("data", gson.toJson(list)); line_list.add(innerList); } JSONObject data = new JSONObject(); data.put("line_list", line_list); data.put("xAxis_list", gson.toJson(dateList)); JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data)); } catch (Exception e) { JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("系统异常")); e.printStackTrace(); } } private List machineDailyCountMoments(List dateList, Integer dateType, List listData) { List list = new ArrayList<>(); for (String date: dateList) { BigDecimal result = new BigDecimal(0); if (listData != null) { for (DailyCountMoments history: listData) { if (dateType == 1) { String gernalTime = TimeUtil.getGernalTime(history.getDay().getTime()); if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } } else if (dateType == 2){ String gernalTime = TimeUtil.getMonthOnlyMM(history.getDay()); if(gernalTime.startsWith("0")) { gernalTime = gernalTime.substring(1, 2); } if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } } else if (dateType == 3) { String gernalTime = TimeUtil.getYearOnlyYYYY(history.getDay()); if (gernalTime.equalsIgnoreCase(date)) { result = MoneyBigDecimalUtil.add(result, history.getTotal()); } } } } list.add(result); } return list; } private void verifyQueryData(String callback, Integer dateType, String year, String startTime, String endTime, PrintWriter out) throws Exception { String validateMsg = null; if (dateType == null) { validateMsg = "请选择视图类型"; JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(validateMsg)); return; } if (dateType == 1 && (StringUtil.isNullOrEmpty(startTime) || StringUtil.isNullOrEmpty(endTime))) { validateMsg = "请选择时间区间"; JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(validateMsg)); return; } } private Date getBeginDate(Integer dateType, String year, String startTime) throws Exception{ Date beginDate = null; if (dateType == 1) { beginDate = TimeUtil.parse(startTime); } else if (dateType == 2) { Calendar calendar=Calendar.getInstance(); int currentYear = calendar.get(Calendar.YEAR); if (!StringUtil.isNullOrEmpty(year)) { currentYear = Integer.parseInt(year); } calendar.clear(); calendar.set(Calendar.YEAR, currentYear); beginDate =calendar.getTime(); } else if (dateType == 3) { beginDate = TimeUtil.parse("2018-01-01"); } return beginDate; } private Date getEndDate(Integer dateType, String year, String endTime) throws Exception{ Date endDate = null; if (dateType == 1) { endDate = TimeUtil.parse(endTime); } else if (dateType == 2) { Calendar calendar=Calendar.getInstance(); int currentYear = calendar.get(Calendar.YEAR); if (!StringUtil.isNullOrEmpty(year)) { currentYear = Integer.parseInt(year); } calendar.clear(); calendar.set(Calendar.YEAR, currentYear); calendar.roll(Calendar.DAY_OF_YEAR, -1); endDate=calendar.getTime(); } else if (dateType == 3) { endDate = new Date(); } return endDate; } }