xushengqiang 6 lat temu
rodzic
commit
886d07e6d4

+ 5 - 1
pom.xml

@@ -129,7 +129,11 @@
 			<artifactId>aliyun-java-sdk-core</artifactId>
 			<version>2.1.7</version>
 		</dependency>
-
+		<dependency>
+			<groupId>org.apache.poi</groupId>
+			<artifactId>poi</artifactId>
+			<version>3.13</version>
+		</dependency>
 	</dependencies>
 
 	<dependencyManagement>

+ 1 - 1
src/main/java/cn/efunbox/audio/config/Config.java

@@ -50,7 +50,7 @@ public class Config extends WebMvcConfigurerAdapter{
 
         registry.addInterceptor(adminInterceptor())
                 .addPathPatterns("/**", "/device/update", "/device/delete")
-                .excludePathPatterns("/device/**", "/error", "/admin/login", "/audio/search","/file/**","/audio/searchList");
+                .excludePathPatterns("/device/**", "/error", "/admin/login", "/audio/search","/file/**","/audio/searchList","/statistics");
 
         registry.addInterceptor(ignoreOptionsInterceptor())
                 .addPathPatterns("/**");

+ 12 - 6
src/main/java/cn/efunbox/audio/controller/AudioController.java

@@ -271,12 +271,8 @@ public class AudioController {
             }
         }
 
-        Record record = new Record();
-        record.setIdDevice(device.getId());
-        record.setIdChannel(device.getIdChannel());
-        record.setIdAudio(audio.getId());
-        record.setCreated(new Timestamp(new Date().getTime()));
-        recordService.Insert(record);
+        insertRecord(audio.getId(),device);
+
 
         //阿里oss临时授权
         audio.setUrl(aliStsOssAuth.AuthUrl(audio.getUrl()));
@@ -285,6 +281,15 @@ public class AudioController {
         return;
     }
 
+    private void insertRecord(Long id, Device device) {
+        Record record = new Record();
+        record.setIdDevice(device.getId());
+        record.setIdChannel(device.getIdChannel());
+        record.setIdAudio(id);
+        record.setCreated(new Timestamp(new Date().getTime()));
+        recordService.Insert(record);
+    }
+
 
     /**
      * 用户搜索接口
@@ -428,6 +433,7 @@ public class AudioController {
             HttpUtil.responseApiCode(request, response, ApiCode.ACCESS_DENIED);
             return;
         }
+        insertRecord(audio.getId(),device);
         HttpUtil.responseOkData(request, response, audio);
         return;
     }

+ 191 - 0
src/main/java/cn/efunbox/audio/controller/StatisticsController.java

@@ -0,0 +1,191 @@
+package cn.efunbox.audio.controller;
+
+import cn.efunbox.audio.service.StatisticsService;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.usermodel.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.util.CollectionUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * StatisticsController
+ * Created by xusq on 2018/7/10.
+ */
+@Controller
+@RequestMapping("/statistics")
+public class StatisticsController {
+
+
+    @Autowired
+    private StatisticsService statisticsService;
+
+    @ResponseBody
+    @RequestMapping
+    public void statistics(String beginDate, String endDate, HttpServletResponse response) throws IOException {
+
+        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate)) {
+            return;
+        }
+        beginDate += " 00:00:00";
+        endDate += " 23:59:59";
+
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        //设置为居中
+        HSSFCellStyle style = workbook.createCellStyle();
+        HSSFFont font = workbook.createFont();
+        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
+        style.setFont(font);
+        style.setWrapText(true);
+
+        HSSFSheet registerSheet = workbook.createSheet("注册数量统计");
+
+        createRegisterTitle(registerSheet, style);
+
+        List<Map<String, Object>> registerCount = statisticsService.registerCount(beginDate, endDate);
+
+        if (!CollectionUtils.isEmpty(registerCount)) {
+            int rowNum = 1;
+            for (Map<String, Object> data : registerCount) {
+                HSSFRow dataRow = registerSheet.createRow(rowNum);
+                dataRow.createCell(0).setCellValue(data.get("channel") + "");
+                dataRow.createCell(1).setCellValue(data.get("date") + "");
+                HSSFCell cell = dataRow.createCell(2);
+                cell.setCellValue(data.get("count") + "");
+                rowNum++;
+            }
+        }
+
+        HSSFSheet viewSheet = workbook.createSheet("播放量统计");
+        createViewTitle(viewSheet, style);
+        List<Map<String, Object>> viewCount = statisticsService.viewCount(beginDate, endDate);
+
+        if (!CollectionUtils.isEmpty(viewCount)) {
+            int rowNum = 1;
+            for (Map<String, Object> data : viewCount) {
+                HSSFRow dataRow = viewSheet.createRow(rowNum);
+                dataRow.createCell(0).setCellValue(data.get("channel") + "");
+                dataRow.createCell(1).setCellValue(data.get("date") + "");
+                dataRow.createCell(2).setCellValue(data.get("count") + "");
+                rowNum++;
+            }
+        }
+
+        HSSFSheet resourcesSheet = workbook.createSheet("音频播放统计");
+        createResourcesTitle(resourcesSheet, style);
+
+        List<Map<String, Object>> resourcesCount = statisticsService.resourcesCount(beginDate, endDate);
+        if (!CollectionUtils.isEmpty(resourcesCount)) {
+            int rowNum = 1;
+            for (Map<String, Object> data : resourcesCount) {
+                HSSFRow dataRow = resourcesSheet.createRow(rowNum);
+                dataRow.createCell(0).setCellValue(data.get("channel") + "");
+                dataRow.createCell(1).setCellValue( data.get("date") + "");
+                dataRow.createCell(2).setCellValue(data.get("audio") + "");
+                dataRow.createCell(3).setCellValue(data.get("count") + "");
+                rowNum++;
+            }
+        }
+
+        String outName = String.format("attachment;filename=\"%s.xls\"", new String("智能语音统计报表".getBytes(),"ISO-8859-1"));
+        response.setContentType("application/vnd.ms-excel");
+        response.setHeader("Content-disposition", outName);
+        OutputStream out = null;
+        try {
+            out = response.getOutputStream();
+            workbook.write(out);
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            if (out != null) {
+                out.close();
+                out.flush();
+            }
+            workbook.close();
+        }
+
+    }
+
+    private void createRegisterTitle(HSSFSheet hssfSheet, HSSFCellStyle style) {
+
+
+        HSSFRow row = hssfSheet.createRow(0);
+
+        int columnNum = 3;
+        for (int i = 0; i < columnNum; i++) {
+            hssfSheet.setColumnWidth(i, 26 * 256);
+        }
+
+        HSSFCell cell;
+        cell = row.createCell(0);
+        cell.setCellValue("渠道名称");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(1);
+        cell.setCellValue("日期");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(2);
+        cell.setCellValue("注册用户数");
+        cell.setCellStyle(style);
+    }
+
+    private void createViewTitle(HSSFSheet hssfSheet, HSSFCellStyle style) {
+
+
+        HSSFRow row = hssfSheet.createRow(0);
+
+        int columnNum = 3;
+        for (int i = 0; i < columnNum; i++) {
+            hssfSheet.setColumnWidth(i, 26 * 256);
+        }
+
+        HSSFCell cell;
+        cell = row.createCell(0);
+        cell.setCellValue("渠道名称");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(1);
+        cell.setCellValue("日期");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(2);
+        cell.setCellValue("播放数量");
+        cell.setCellStyle(style);
+    }
+
+    private void createResourcesTitle(HSSFSheet hssfSheet, HSSFCellStyle style) {
+
+
+        HSSFRow row = hssfSheet.createRow(0);
+
+        int columnNum = 4;
+        for (int i = 0; i < columnNum; i++) {
+            hssfSheet.setColumnWidth(i, 26 * 256);
+        }
+
+        HSSFCell cell;
+        cell = row.createCell(0);
+        cell.setCellValue("渠道名称");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(1);
+        cell.setCellValue("日期");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(2);
+        cell.setCellValue("音频名称");
+        cell.setCellStyle(style);
+
+        cell = row.createCell(3);
+        cell.setCellValue("播放数量");
+        cell.setCellStyle(style);
+    }
+}

+ 1 - 0
src/main/java/cn/efunbox/audio/impl/AudioServiceImpl.java

@@ -79,6 +79,7 @@ public class AudioServiceImpl implements AudioService {
         list.stream().forEach(audio -> {
             Album album = albumMap.get(audio.getAlbumId());
             if (Objects.nonNull(album)) {
+
                 audio.setAlbum(album.getName());
                 if (StringUtils.isNotBlank(album.getImage())) {
                     audio.setAlbumImage(imgURL + album.getImage());

+ 56 - 0
src/main/java/cn/efunbox/audio/impl/StatisticsServiceImpl.java

@@ -0,0 +1,56 @@
+package cn.efunbox.audio.impl;
+
+import cn.efunbox.audio.service.StatisticsService;
+import org.hibernate.SQLQuery;
+import org.hibernate.transform.Transformers;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * StatisticsServiceImpl
+ * Created by xusq on 2018/7/10.
+ */
+@Service
+public class StatisticsServiceImpl implements StatisticsService {
+
+    @Autowired
+    private EntityManager entityManager;
+
+    @Override
+    public List<Map<String, Object>> registerCount(String beginDate, String endDate) {
+
+        Query query = entityManager.createNativeQuery("SELECT c.`name` AS channel,DATE(d.created) AS date,COUNT(1) AS count FROM `device` d,channel c" +
+                " WHERE d.`id_channel` = c.`id` AND d.created BETWEEN '" + beginDate + "' AND '" + endDate + "'" +
+                " GROUP BY d.id_channel,date" +
+                " ORDER BY d.id_channel,date");
+        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
+        return query.getResultList();
+    }
+
+    @Override
+    public List<Map<String, Object>> viewCount(String beginDate, String endDate) {
+
+        Query query = entityManager.createNativeQuery("SELECT c.`name` AS channel,DATE(r.created) AS date,COUNT(1) AS count FROM `record` r,channel c" +
+                " WHERE r.`id_channel` = c.`id` AND r.created BETWEEN '" + beginDate + "' AND '" + endDate + "'" +
+                " GROUP BY r.id_channel,date" +
+                " ORDER BY r.id_channel,date");
+        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
+        return query.getResultList();
+    }
+
+    @Override
+    public List<Map<String, Object>> resourcesCount(String beginDate, String endDate) {
+
+        Query query = entityManager.createNativeQuery("SELECT c.`name` AS channel,DATE(r.created) AS date,a.name AS audio,COUNT(1) AS count FROM `record` r,channel c,audio a" +
+                " WHERE r.`id_channel` = c.`id` AND r.id_audio = a.id AND r.created BETWEEN '" + beginDate + "' AND '" + endDate + "'" +
+                " GROUP BY r.id_channel,date,r.id_audio" +
+                " ORDER BY r.id_channel,date");
+        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
+        return query.getResultList();
+    }
+}

+ 17 - 0
src/main/java/cn/efunbox/audio/service/StatisticsService.java

@@ -0,0 +1,17 @@
+package cn.efunbox.audio.service;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * StatisticsService
+ * Created by xusq on 2018/7/10.
+ */
+public interface StatisticsService {
+
+    List<Map<String,Object>> registerCount(String beginDate,String endDate);
+
+    List<Map<String,Object>> viewCount(String beginDate,String endDate);
+
+    List<Map<String,Object>> resourcesCount(String beginDate,String endDate);
+}