Parcourir la source

分公司折线图数据格式组装

yangzian il y a 3 ans
Parent
commit
fad008d545

+ 73 - 0
jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java

@@ -1851,6 +1851,79 @@ public class DateUtils extends PropertyEditorSupport {
 
 
 
+    /**
+     * 获取 两个日期之间的所有 日期
+     *
+     * @param startDate
+     * @param endDate
+     * @return
+     */
+    public static List<String> getAllDatesOfTwoTimes(String startDate, String endDate) {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        List<String> dateList = new ArrayList<String>();
+        try {
+            Date dateOne = sdf.parse(startDate);
+            Date dateTwo = sdf.parse(endDate);
+
+            Calendar calendar = Calendar.getInstance();
+            calendar.setTime(dateOne);
+
+            dateList.add(startDate);
+            while (calendar.getTime().before(dateTwo)) { //倒序时间,顺序after改before其他相应的改动。
+                calendar.add(Calendar.DAY_OF_MONTH, 1);
+                dateList.add(sdf.format(calendar.getTime()));
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return dateList;
+    }
+
+
+    /**
+     * 时间 去重 排序
+     * @param list
+     * @return
+     */
+    public static List<String> sortlistTest(List<String> list) {
+        //去重
+        List<String> newlist = new ArrayList<String>();
+        for (int i = 0; i < list.size(); i++) {
+            if (!newlist.contains(list.get(i))) {
+                newlist.add(list.get(i));
+            }
+        }
+        String tmp;
+        for (int i = 1; i < newlist.size(); i++) {
+            tmp = newlist.get(i);
+            int j = i - 1;
+            for (; j >= 0 && (DateCompare(tmp, newlist.get(j)) < 0); j--) {
+                newlist.set(j + 1, newlist.get(j));
+            }
+            newlist.set(j + 1, tmp);
+        }
+        return newlist;
+    }
+
+
+    public static long DateCompare(String s1, String s2) {
+        try {
+
+            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+            Date d1 = simpleDateFormat.parse(s1);
+            Date d2 = simpleDateFormat.parse(s2);
+            // LocalDateTime d1 = LocalDateTime.parse(s1, DateTimeFormatter.ISO_LOCAL_DATE);
+            //LocalDateTime d2 = LocalDateTime.parse(s2, DateTimeFormatter.ISO_LOCAL_DATE);
+            //排序规则
+            return ((d1.getTime()) - (d2.getTime()));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return 0;
+    }
+
+
+
 
 
     public static void main(String[] args) {

+ 2 - 2
jeecg-boot-material-view/src/main/java/org/jeecg/ctop/material/mapper/xml/BossDataExhibitionMapper.xml

@@ -501,7 +501,7 @@
 <!--  分媒体公司消耗-账户维度-分日数据 -折线图-头条-->
     <select id="getMediaTypeAccountTrendBytedance" resultType="org.jeecg.ctop.material.entity.AccountTrendPojo">
         SELECT
-            stat_datetime as time,
+            DATE_FORMAT(stat_datetime,'%Y-%m-%d') as time,
             SUM(cost) as cost,
             CASE
                 company_id
@@ -528,7 +528,7 @@
 <!--  分媒体公司消耗-账户维度-分日数据-折线图-快手 -->
     <select id="getMediaTypeAccountTrendKuaishou" resultType="org.jeecg.ctop.material.entity.AccountTrendPojo">
         SELECT
-        stat_date as time,
+        DATE_FORMAT(stat_date,'%Y-%m-%d') as time,
         SUM(charge) as cost,
         CASE
             company_id

+ 85 - 4
jeecg-boot-material-view/src/main/java/org/jeecg/ctop/material/service/impl/BossDataExhibitionServiceImpl.java

@@ -199,6 +199,11 @@ public class BossDataExhibitionServiceImpl implements IBossDataExhibitionService
      */
     @Override
     public Result getAccountTrendByMediaType(String mediaType, String startTime, String endTime) {
+        Map<String,Object> resultMap = new HashMap();
+        // 获取两个日期之间 所有的 时间 年-月-日
+        List<String> betweenDaysList = DateUtils.getAllDatesOfTwoTimes(startTime,endTime);
+        resultMap.put("time",betweenDaysList);
+
         Integer stat_tim = DateUtils.getDateInteger(startTime);
         Integer end_tim = DateUtils.getDateInteger(endTime);
         List<AccountTrendPojo> resultList = Arrays.asList();
@@ -208,14 +213,90 @@ public class BossDataExhibitionServiceImpl implements IBossDataExhibitionService
         }else if (StringUtils.equals("2",mediaType)){
             resultList = bossDataExhibitionMapper.getMediaTypeAccountTrendKuaishou(stat_tim,end_tim);
         }
-        if (!Check.isNull(resultList)){
-            Map<String,List<AccountTrendPojo>> resultMap = resultList.stream().collect(Collectors.groupingBy(AccountTrendPojo::getArea));
-            return Result.successMsg(("1".equals(mediaType) ? "【头条】" : "【快手】")+"===分公司-分日-账户消耗数据查询成功。",resultMap);
+
+        if (Check.isNull(resultList)) {
+            return Result.error("账户消耗数据查询为空");
+        }
+
+        /**
+         * 数据组装
+         */
+        //根据 区域分组
+        Map<String,List<AccountTrendPojo>> map = resultList.stream().collect(Collectors.groupingBy(AccountTrendPojo::getArea));
+
+        //1-华北
+        List<AccountTrendPojo> huaBeiList = !map.containsKey("华北") ? null : map.get("华北");
+
+        List<String> huaBeiCostList = new ArrayList();
+        if (!Check.isNull(huaBeiList)){
+            //获取数据 时间 集合
+            List<String> huabeiTime = huaBeiList.stream().map(AccountTrendPojo::getTime).collect(Collectors.toList());
+            // 补充数据
+            huaBeiList = getDateSupplement(betweenDaysList,huabeiTime,huaBeiList);
+            // 获取属性 cost 返回数组
+            huaBeiCostList = huaBeiList.stream().map(AccountTrendPojo::getCost).collect(Collectors.toList());
+        }
+        resultMap.put("huaBei",huaBeiCostList);
+
+        //2-华南
+        List<String> huaNanCostList = new ArrayList();
+        List<AccountTrendPojo> huaNanList = !map.containsKey("华南") ? null : map.get("华南");
+        if (!Check.isNull(huaNanList)){
+            List<String> huaNanTime = huaNanList.stream().map(AccountTrendPojo::getTime).collect(Collectors.toList());
+            huaNanList = getDateSupplement(betweenDaysList,huaNanTime,huaNanList);
+            huaNanCostList = huaNanList.stream().map(AccountTrendPojo::getCost).collect(Collectors.toList());
+        }
+        resultMap.put("huaNan",huaNanCostList);
+
+        //3-华东
+        List<String> huaDongCostList = new ArrayList();
+        List<AccountTrendPojo> huaDongList = !map.containsKey("华东") ? null : map.get("华东");
+        if (!Check.isNull(huaDongList)){
+            List<String> huaDongTime = huaDongList.stream().map(AccountTrendPojo::getTime).collect(Collectors.toList());
+            huaDongList = getDateSupplement(betweenDaysList,huaDongTime,huaDongList);
+            huaDongCostList = huaDongList.stream().map(AccountTrendPojo::getCost).collect(Collectors.toList());
+        }
+        resultMap.put("huaDong",huaDongCostList);
+
+        //4-骄阳
+        List<String> jiaoYangCostList = new ArrayList();
+        List<AccountTrendPojo> jiaoYangList = !map.containsKey("骄阳") ? null : map.get("骄阳");
+        if (!Check.isNull(jiaoYangList)){
+            List<String> jiaoYangTime = jiaoYangList.stream().map(AccountTrendPojo::getTime).collect(Collectors.toList());
+            jiaoYangList = getDateSupplement(betweenDaysList,jiaoYangTime,jiaoYangList);
+            jiaoYangCostList = jiaoYangList.stream().map(AccountTrendPojo::getCost).collect(Collectors.toList());
         }
-        return Result.error("暂无数据。");
+        resultMap.put("jiaoYang",jiaoYangCostList);
+
+        return Result.successMsg(("1".equals(mediaType) ? "【头条】" : "【快手】")+"===分公司-分日-账户消耗数据查询成功。",resultMap);
+
+    }
+
+
+    /**
+     * 补充数据 没有时间的补充时间并添加数据为0
+     * @param betweenDaysList  两个日期中间的所有日期集合
+     * @param nowList 现有日期的集合
+     * @param accountTrendPojoList 数据集合
+     * @return
+     */
+    public List<AccountTrendPojo> getDateSupplement(List<String> betweenDaysList,List<String> nowList,List<AccountTrendPojo> accountTrendPojoList){
+        // 时间差集(所有时间段 - 已有的时间段)
+        List<String>  reduceList = betweenDaysList.stream().filter(item -> !nowList.contains(item)).collect(Collectors.toList());
+        reduceList.forEach(str ->{
+                    AccountTrendPojo pojo = new AccountTrendPojo();
+                    pojo.setTime(str);
+                    pojo.setCost("0");
+                    accountTrendPojoList.add(pojo);
+                }
+        );
+        //时间排序
+        accountTrendPojoList.sort(Comparator.comparing(AccountTrendPojo::getTime));
+        return accountTrendPojoList;
     }
 
 
+
     /**
      *
      * @description: 项目消耗排行top10