package com.ibeetl.admin.console.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.json.JSON; import cn.hutool.json.JSONUtil; import com.ibeetl.admin.console.dao.CuserConsoleDao; import com.ibeetl.admin.console.dao.EchartConsoleDao; import com.ibeetl.admin.console.model.EchartModel; import com.ibeetl.admin.console.model.ItemRateModel; import com.ibeetl.admin.console.util.DoubleUtil; import com.ibeetl.admin.core.util.DateUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * 图表数据 */ @Service @Slf4j @EnableScheduling @Configuration public class EchartService { static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH"); static SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd"); static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy"); static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy-MM"); @Autowired EchartConsoleDao dao; @Autowired CuserConsoleDao cuserConsoleDao; @Autowired RedisService redisService; public Map queryData(int page, int limit){ // 统计top数据 EchartModel em = getTopData(); // 按周查询统计第二栏数据 Map weekMap = queryWeek(); // 按月查询统计第二栏数据 Map monthMap = queryMonth(); // 查询仓库数据 List storageList = getStorageData(page, limit ); // 查询物品类型的总量和占比 Map itemRateDate = getItemRateDate(); // 获取15天数据 List list = query15daydata(); Map mx = new HashMap<>(); mx.put("topdata", em); mx.put("weekMap", weekMap); mx.put("monthMap", monthMap); mx.put("storageList", storageList); mx.put("itemtypeMap", itemRateDate); mx.put("15daydata", list); //查询分类下所有入库的金额 // String s = dao.querySumReclayMoney(); // mx.put("reclyMoney",Convert.toBigDecimal(s)); return mx; } /** * 获取15的数据 * @return */ List query15daydata(){ Date date = new Date(); String startTime = dateX(date, -15)+ " 00:00:00"; String endTime = sdf2.format(date) + " 23:59:59"; List list = dao.query15daydata(startTime, endTime); List timelist = new ArrayList<>(); for (int i = 0; i < 15; i++){ timelist.add(dateX(date, -i)); } boolean b = true; for(String time : timelist){ b = true; for(EchartModel em : list){ if(time.equals(em.getCreateTime())){ b = false; break; } } if(b){ EchartModel echartModel = new EchartModel(); echartModel.setMoneyCount(0.0); echartModel.setWeightCount(0.0); echartModel.setUserCount(0); echartModel.setStorageWeightCount(0.0); echartModel.setStorageMoneyCount(0.0); echartModel.setCreateTime(time); list.add(echartModel); } } for(EchartModel ex : list){ ex.setStorageWeightCount((double) Math.round(ex.getStorageWeightCount() * 100) / 100); ex.setWeightCount((double) Math.round(ex.getWeightCount() * 100) / 100); } list.sort((x, y) ->x.getCreateTime().compareTo(y.getCreateTime())); return list; } Map getItemRateDate(){ ItemRateModel model = new ItemRateModel(); // 查询全部 List allList = dao.queryItemtype(model); allList = itemRateOp(allList, model); Date date = new Date(); // 查询当年 model.setStartTime(sdf4.format(date) + "-01-01 00:00:00"); model.setEndTime(sdf4.format(date) + "-12-30 23:59:59"); List yearList = dao.queryItemtype(model); yearList = itemRateOp(yearList, model); // 查询当月 model.setStartTime(sdf5.format(date) + "-01 00:00:00"); model.setEndTime(sdf5.format(date) + "-30 23:59:59"); List monthList = dao.queryItemtype(model); monthList = itemRateOp(monthList, model); Map m = new HashMap<>(); m.put("all", allList); m.put("year", yearList); m.put("month", monthList); if(CollUtil.isEmpty(monthList)){ List monthList2 = dao.queryItemtypeEmpty(model); m.put("month", monthList2); } return m; } List itemRateOp(List list, ItemRateModel model){ for(ItemRateModel irm : list){ model.setStorageMoney(irm.getStorageMoney()+model.getStorageMoney()); model.setStorageWeight(irm.getStorageWeight()+ model.getStorageWeight()); } for(ItemRateModel irm : list){ irm.setMoneyRage(DoubleUtil.divide(irm.getStorageMoney(), model.getStorageMoney())); irm.setWeightRage(DoubleUtil.divide(irm.getStorageWeight(), model.getStorageWeight())); } return list; } List getStorageData(int page, int limit){ EchartModel echartModel = new EchartModel(); echartModel.setPage(page); echartModel.setLimit(limit); // 查询入库总金额和入库总重量 List list = dao.queryDataByStorageId(echartModel); // 查询上周总数 Map map = getLastWeek(7); echartModel.setStartTime(sdf2.format(map.get("monday"))+" 00:00:00"); echartModel.setEndTime(sdf2.format(map.get("sunday"))+" 23:59:59"); List em = dao.queryDataByStorageId(echartModel); // 查询上上周的 map = getLastWeek(14); echartModel.setStartTime(sdf2.format(map.get("monday"))+" 00:00:00"); echartModel.setEndTime(sdf2.format(map.get("sunday"))+" 23:59:59"); List em1 = dao.queryDataByStorageId(echartModel); List> temp = new ArrayList<>(); for(EchartModel ex : em){ for(EchartModel exx : em1){ if(ex.getSysStorageId().equals(exx.getSysStorageId())){ Map m = new HashMap<>(); m.put("moneyRate", DoubleUtil.divide(ex.getStorageMoneyCount()-exx.getStorageMoneyCount(),exx.getStorageMoneyCount())); m.put("weightRate", DoubleUtil.divide(ex.getStorageWeightCount()-exx.getStorageWeightCount(),exx.getStorageWeightCount())); m.put("storage", ex.getSysStorageId()); temp.add(m); } } } for(EchartModel ex : list){ for(Map li : temp){ if(li.get("storage").equals(ex.getSysStorageId())){ ex.setStorageMoneyWeekRate(Double.parseDouble(li.get("moneyRate").toString())); break; } } ex.setStorageWeightCount((double) Math.round(ex.getStorageWeightCount() * 100) / 100); } return list; } /** * 获取顶部数据 * @return */ EchartModel getTopData(){ EchartModel echartModel = new EchartModel(); // 查询总的 EchartModel em1 = dao.queryData(echartModel); // 查询今日的 Date date = new Date(); echartModel.setStartTime(sdf2.format(date) + " 00:00:00"); echartModel.setEndTime(sdf2.format(date) + " 23:59:59"); EchartModel em2 = dao.queryData(echartModel); em1.setStorageMoneyDayCount(em2.getStorageMoneyCount()); //查询日活 /* Map dayCountMap = cuserConsoleDao.queryUserDayCount(); if(null!=dayCountMap.get("userDayCount")){ em1.setVisitCount(Integer.parseInt(dayCountMap.get("userDayCount").toString())); }*/ em1.setVisitCount(em2.getVisitCount()); //只要用户下了单则统计出来 //em1.setOrderDayCount(em2.getOrderCount()); String dayOrderCount = dao.queryOrderDayCount(echartModel); if(null!=dayOrderCount&&!"".equals(dayOrderCount)){ em1.setOrderDayCount(Integer.parseInt(dayOrderCount)); }else{ em1.setOrderDayCount(0); } DecimalFormat df = new DecimalFormat("#.00"); df.format(em2.getStorageWeightCount()); em1.setStorageWeightDayCount(Double.parseDouble(df.format(em2.getStorageWeightCount()))); // 查询昨天的 String s = dateX(date, -1); echartModel.setStartTime(s + " 00:00:00"); echartModel.setEndTime(s + " 23:59:59"); EchartModel em3 = dao.queryData(echartModel); // 查询前天的 s = dateX(date, -2); echartModel.setStartTime(s + " 00:00:00"); echartModel.setEndTime(s + " 23:59:59"); EchartModel em4 = dao.queryData(echartModel); // 计算日环比增长率 em1.setStorageMoneyDayRate(DoubleUtil.divide(em3.getStorageMoneyCount()-em4.getStorageMoneyCount(), em4.getStorageMoneyCount())); // 查询上周的 Map map = getLastWeek(7); echartModel.setStartTime(sdf2.format(map.get("monday"))+" 00:00:00"); echartModel.setEndTime(sdf2.format(map.get("sunday"))+" 23:59:59"); EchartModel em5 = dao.queryData(echartModel); // 查询上上周的 map = getLastWeek(14); echartModel.setStartTime(sdf2.format(map.get("monday"))+" 00:00:00"); echartModel.setEndTime(sdf2.format(map.get("sunday"))+" 23:59:59"); EchartModel em6 = dao.queryData(echartModel); // 计算周环比增长 em1.setStorageMoneyWeekRate(DoubleUtil.divide(em5.getStorageMoneyCount()-em6.getStorageMoneyCount(),em6.getStorageMoneyCount())); // 计算日增长率 em1.setWeightCount((double) Math.round(em1.getWeightCount() * 100) / 100); em1.setStorageWeightCount((double) Math.round(em1.getStorageWeightCount() * 100) / 100); return em1; } Map queryWeek(){ List> list = new ArrayList<>(); for(int i = 0, h = 7; i <= 12; i++){ list.add(getLastWeek(h * i)); } EchartModel echartModel = new EchartModel(); // 用户数据集合 List> userList = new ArrayList<>(); // 活跃量集合 List> visitList = new ArrayList<>(); // 订单量集合 List> orderCountList = new ArrayList<>(); // 订单金额总数集合 List> orderMoneyCountList = new ArrayList<>(); // 入库金额总数集合 List> storageMoneyCountList = new ArrayList<>(); // 订单重量总数集合 List> orderWeightCountList = new ArrayList<>(); // 入库重量总数集合 List> storageWeightCountList = new ArrayList<>(); // Collections.reverse(list); for(Map map : list){ echartModel.setStartTime(sdf2.format(map.get("monday"))+" 00:00:00"); echartModel.setEndTime(sdf2.format(map.get("sunday"))+" 23:59:59"); EchartModel em = dao.queryData(echartModel); Map m = new HashMap<>(); m.put("time", sdf2.format(map.get("monday"))); m.put("data", em.getUserCount()+""); userList.add(m); Map m2 = new HashMap<>(); m2.put("time", sdf2.format(map.get("monday"))); m2.put("data", weekVistNum(sdf2.format(map.get("monday")),sdf2.format(map.get("sunday")))); //m2.put("data", em.getVisitCount()+""); visitList.add(m2); Map m3 = new HashMap<>(); m3.put("time", sdf2.format(map.get("monday"))); m3.put("data", em.getOrderCount()+""); orderCountList.add(m3); Map m4 = new HashMap<>(); m4.put("time", sdf2.format(map.get("monday"))); m4.put("data", DoubleUtil.roundTwo(em.getMoneyCount()+"")); orderMoneyCountList.add(m4); Map m5 = new HashMap<>(); m5.put("time", sdf2.format(map.get("monday"))); m5.put("data", DoubleUtil.roundTwo(em.getStorageMoneyCount()+"")); storageMoneyCountList.add(m5); Map m6 = new HashMap<>(); m6.put("time", sdf2.format(map.get("monday"))); m6.put("data", DoubleUtil.roundThree(em.getWeightCount()+"")); /*m4.put("time",sdf2.format(map.get("monday"))); m4.put("data", echartModel1.getWeightCount()+"");*/ orderWeightCountList.add(m6); Map m7 = new HashMap<>(); m7.put("time", sdf2.format(map.get("monday"))); m7.put("data", DoubleUtil.roundThree(em.getStorageWeightCount()+"")); storageWeightCountList.add(m7); } Map m = new HashMap<>(); m.put("userCountWeek", userList); m.put("visitCountWeek", visitList); m.put("orderCountWeek", orderCountList); m.put("orderMoneyCountWeek", orderMoneyCountList); m.put("storageMoneyCountWeek", storageMoneyCountList); m.put("orderWeightCountWeek", orderWeightCountList); m.put("storageWeightCountWeek", storageWeightCountList); return m; } Map queryMonth(){ List> list = new ArrayList<>(); String[] latest12Month = getLatest12Month(); for(String s : latest12Month){ Map m = new HashMap<>(); m.put("monday", s + "-01 00:00:00"); m.put("sunday", s + "-31 23:59:59"); list.add(m); } EchartModel echartModel = new EchartModel(); // 用户数据集合 List> userList = new ArrayList<>(); // 活跃量集合 List> visitList = new ArrayList<>(); // 订单量集合 List> orderCountList = new ArrayList<>(); // 订单金额总数集合 List> orderMoneyCountList = new ArrayList<>(); // 入库金额总数集合 List> storageMoneyCountList = new ArrayList<>(); // 订单重量总数集合 List> orderWeightCountList = new ArrayList<>(); // 入库重量总数集合 List> storageWeightCountList = new ArrayList<>(); for(Map map : list){ echartModel.setStartTime(map.get("monday")); echartModel.setEndTime(map.get("sunday")); EchartModel em = dao.queryData(echartModel); Map m = new HashMap<>(); m.put("time", map.get("monday").split(" ")[0]); m.put("data", em.getUserCount()+""); userList.add(m); Map m2 = new HashMap<>(); m2.put("time", map.get("monday").split(" ")[0]); m2.put("data", weekVistNum(map.get("monday").split(" ")[0],map.get("sunday").split(" ")[0])); visitList.add(m2); Map m3 = new HashMap<>(); m3.put("time", map.get("monday").split(" ")[0]); m3.put("data", em.getOrderCount()+""); orderCountList.add(m3); Map m4 = new HashMap<>(); m4.put("time", map.get("monday").split(" ")[0]); m4.put("data", em.getMoneyCount()+""); orderMoneyCountList.add(m4); Map m5 = new HashMap<>(); m5.put("time", map.get("monday").split(" ")[0]); m5.put("data", em.getStorageMoneyCount()+""); storageMoneyCountList.add(m5); Map m6 = new HashMap<>(); m6.put("time", map.get("monday").split(" ")[0]); m6.put("data", em.getWeightCount()+""); orderWeightCountList.add(m6); Map m7 = new HashMap<>(); m7.put("time", map.get("monday").split(" ")[0]); m7.put("data", em.getStorageWeightCount()+""); storageWeightCountList.add(m7); } Map m = new HashMap<>(); m.put("userCountMonth", userList); m.put("visitCountMonth", visitList); m.put("orderCountMonth", orderCountList); m.put("orderMoneyCountMonth", orderMoneyCountList); m.put("storageMoneyCountMonth", storageMoneyCountList); m.put("orderWeightCountMonth", orderWeightCountList); m.put("storageWeightCountMonth", storageWeightCountList); return m; } String weekVistNum(String startTime,String endTime){ String haveVisit = dao.haveVisitLog(startTime,endTime); List weekVistNum = null; if(null!=haveVisit&&!"".equals(haveVisit)){ weekVistNum = dao.weekVistNum(startTime,endTime); } if(null!=weekVistNum){ return weekVistNum.size()+""; }else{ return "0"; } } /** * 获取前面的时间 * @param date * @param days -1为前一天,-2为前2天,以此类推 * @return */ String dateX(Date date, int days){ Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, days); Date yesterday = c.getTime(); return sdf2.format(yesterday); } Map getLastWeek(int days) { // TODO Auto-generated method stub Map map = new HashMap(); Calendar cal = Calendar.getInstance(); int n = cal.get(Calendar.DAY_OF_WEEK) - 1; if (n == 0) { n = 7; } // 上周一的日期 cal.add(Calendar.DATE, -(days + (n - 1))); Date monday = cal.getTime(); map.put("monday", monday); cal.add(Calendar.DATE, 1); Date tuesday = cal.getTime(); map.put("tuesday", tuesday); cal.add(Calendar.DATE, 1); Date wednesday = cal.getTime(); map.put("wednesday", wednesday); cal.add(Calendar.DATE, 1); Date thursday = cal.getTime(); map.put("thursday", thursday); cal.add(Calendar.DATE, 1); Date friday = cal.getTime(); map.put("friday", friday); cal.add(Calendar.DATE, 1); Date saturday = cal.getTime(); map.put("saturday", saturday); cal.add(Calendar.DATE, 1); Date sunday = cal.getTime(); map.put("sunday", sunday); return map; } static String[] getLatest12Month(){ String[] latest12Months = new String[12]; Calendar cal = Calendar.getInstance(); //要先+1,才能把本月的算进去 cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1); for(int i=0; i<12; i++){ //逐次往前推1个月 cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1); latest12Months[11-i] = cal.get(Calendar.YEAR)+ "-" +fillZero(cal.get(Calendar.MONTH)+1); } return latest12Months; } static String fillZero(int i){ String month = ""; if(i<10){ month = "0" + i; }else{ month = String.valueOf(i); } return month; } /** * 每60分钟执行一次 */ // @Scheduled(cron = "0 */60 * * * ?") //@Scheduled(cron = "${charsTime}") @Scheduled(cron = "${charsTime}") public void index() throws Exception{ Integer integer = dao.qeuryConfigInfo("ECHAR_X"); if(integer == null){ log.info("----------字典值配置ECHAR_X=0--------"); } if(integer == 1){ // 第一次启动 Date d = new Date(); String startTime = sdf6.format(d) + ":00"; String endTime = sdf6.format(d) + ":59"; startEchar(startTime, endTime, 1); dao.updateConfigInfo("ECHAR_X"); }else if(integer == 0){ // 第二次启动 // 查询最后一条数据的时间 String time = dao.queryTime(); System.out.println("查询最后一条数据的时间==="+time); //为保持时间一致,获取数据库时间。 String dbTime = dao.queryDbTime(); startEchar(time, dbTime, 1); } } public static void main(String[] args) { } void startEchar(String startTime, String endTime, int flag){ log.info("--------统计任务开始执行: {}----------", sdf3.format(new Date())); Date d = new Date(); // String startTime = sdf.format(d) + " :00:00"; // String endTime = sdf.format(d) + " :59:59"; EchartModel echartModel = moneyEchart(startTime, endTime); EchartModel em = userEchart(startTime, endTime); echartModel.setUserCount(em.getUserCount()); em = orderEchart(startTime, endTime); /*if(em.getOrderCount()==0){ em = orderEchartTemp(startTime, endTime); }*/ echartModel.setOrderCount(em.getOrderCount()); //统计接口修改 //查询日活 Map dayCountMap = cuserConsoleDao.queryUserDayCount1(); if(null!=dayCountMap&&null!=dayCountMap.get("userDayCount")){ echartModel.setVisitCount(Integer.parseInt(dayCountMap.get("userDayCount").toString())); } List list = storageEchart(startTime, endTime); if(null!=list&&list.size()>0){ for(EchartModel model : list){ echartModel.setStorageMoneyCount(model.getStorageMoneyCount()); echartModel.setStorageWeightCount(model.getStorageWeightCount()); echartModel.setSysStorageId(model.getSysStorageId()); if(flag == 0){ echartModel.setCreateTime(startTime); echartModel.setDateHours(startTime); }else{ echartModel.setCreateTime(sdf3.format(d)); echartModel.setDateHours(sdf.format(d)+":00:00"); } dao.insertEchart(echartModel); } } log.info("--------统计任务结束: {}----------", sdf3.format(new Date())); } /** * 金钱统计 */ EchartModel moneyEchart(String startTime, String endTime){ return dao.moneyEchart(startTime, endTime); } /** * 用户统计 */ EchartModel userEchart(String startTime, String endTime){ return dao.userEchart(startTime, endTime); } /** * 订单统计 */ EchartModel orderEchart(String startTime, String endTime){ return dao.orderEchart(startTime, endTime); } EchartModel orderEchartTemp(String startTime, String endTime){ return dao.orderEchartTemp(startTime, endTime); } /** * 库存统计 */ List storageEchart(String startTime, String endTime){ return dao.storageEchart(startTime, endTime); } /** * 访问统计 */ public int visitEchart(Date date){ Map map = redisService.getMapValue("xzx:user:loginInfo"); String dataNow = sdf2.format(date); int visit = 0; for(String key : map.keySet()){ JSON json = JSONUtil.parse(map.get(key)); if(json.getByPath("time").toString().split(" ")[0].compareTo(dataNow) <= 0){ visit++; } } return visit; } Boolean compareDate(String dataNow,String dateRedis){ if(dateRedis.compareTo(dataNow) <= 0){ return true; } return false; } /** * 统计以前的数据 * 这个方法需要延时执行并且只能执行一次 */ // @Scheduled(initialDelay = 1000*60*60*2,fixedRate = 1000*60*60*24) @Scheduled(cron = "${charsTime}") @Transactional(rollbackFor = {}) void indexed() throws Exception{ //Thread.sleep(1000*60); // 获取字典值的数据 Integer integer = dao.qeuryConfigInfo("ECHAR_FLAG"); if(integer == null || integer == 0){ log.info("--------请先配置ECHAR_FLAG的值或此任务已经执行--------"); return; } // 获取订单里面的第一条数据的时间 ItemRateModel order = dao.queryOrder(); if(StringUtils.isEmpty(order.getCreateTime())){ log.info("-------订单为0,此条件不会执行-------"); dao.updateConfigInfo("ECHAR_FLAG"); return; } // 获取统计表的第一条数据的时间 ItemRateModel model = dao.queryModel(); if(model == null){ return; } if(StringUtils.isEmpty(model.getCreateTime())){ model.setCreateTime(sdf3.format(new Date())); } Calendar calendar = Calendar.getInstance(); List list = new ArrayList<>(); while (true){ calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)-1); String dataNow = sdf3.format(calendar.getTime()); if(!compareDate(dataNow, order.getCreateTime())){ log.info("----------获取的时间比订单是小,break---------"); break; } ItemRateModel irm = new ItemRateModel(); irm.setStartTime(sdf.format(calendar.getTime())+":00:00"); irm.setEndTime(sdf.format(calendar.getTime()) +":59:59"); list.add(irm); } log.info("-------统计以前的数据时间集合(老版本)------ {}", list.size()); for(ItemRateModel im : list){ startEchar(im.getStartTime(), im.getEndTime(), 0); } dao.updateConfigInfo("ECHAR_FLAG"); log.info("----------统计以前的数据完成(老版本)-------"); } }