fixed 首页任务数据导出

master
zhangjinli 2 years ago
parent bb476496d4
commit 9df23d3270

@ -8,16 +8,20 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import com.zh.project0512.annotation.AdminTokenValid;
import com.zh.project0512.mapper.AdminMapper;
import com.zh.project0512.mapper.UserEffectRecordsMapper;
import com.zh.project0512.model.Task;
import com.zh.project0512.model.UserEffectRecords;
import com.zh.project0512.model.Video;
import com.zh.project0512.model.dto.RankListDTO;
import com.zh.project0512.service.IAdminService;
import com.zh.project0512.service.IRankService;
import com.zh.project0512.utils.ExcelUtil;
import com.zh.project0512.utils.MybatisPlusUtil;
import com.zh.project0512.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
@ -26,6 +30,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
@ -35,6 +44,7 @@ import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
@ -44,6 +54,8 @@ import java.util.stream.Stream;
@RestController
@RequestMapping("/manage/statistics")
public class StatisticsController {
@Autowired
private IAdminService adminService;
@Autowired
private AdminMapper adminMapper;
@Autowired
@ -57,7 +69,7 @@ public class StatisticsController {
}
@Data
static class SSParam {
public static class SSParam {
@Schema(title = "任务Id")
private int taskId;
@Schema(title = "员工Id")
@ -76,70 +88,7 @@ public class StatisticsController {
@PostMapping("/data2")
@AdminTokenValid
public Result data2(@Validated @RequestBody SSParam param) {
int condition = param.getDateCondition();
QueryWrapper<Video> qw = new QueryWrapper<>();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM");
LocalDate now = LocalDateTime.now().toLocalDate();
LocalDate before = now.with(TemporalAdjusters.firstDayOfMonth());
switch (condition) {
case 1:
before = now;
break;
case 2:
before = now.with(DayOfWeek.MONDAY);
break;
case 3:
before = now.with(TemporalAdjusters.firstDayOfMonth());
break;
case 4:
before = now.with(TemporalAdjusters.firstDayOfYear());
break;
default:
before = param.getBeginTime() != null ? param.getBeginTime().toLocalDate() : before;
now = param.getEndTime() != null ? param.getEndTime().toLocalDate() : now;
}
if(param.getTaskId()>0){
qw.eq("taskId", param.getTaskId());
}
if(param.getUserId()>0){
qw.eq("userId", param.getUserId());
}
ArrayList<JSONObject> res = new ArrayList<>();
if (4 == condition) {
qw.between("date_format(creatAt, '%Y-%m')", before.format(df2), now.plusMonths(1).format(df2));
List<JSONObject> videoDataBydDay = adminMapper.statistics3(qw);
for (int i = 0; i <= ChronoUnit.MONTHS.between(before, now); i++) {
LocalDate today = before.plusMonths(i);
JSONObject obj = null;
// Stream<JSONObject> dat1 = videoDataBydDay.stream().filter(x -> LocalDate.parse((CharSequence) x.get("dat"), df) == today);
for (JSONObject o : videoDataBydDay) {
if (o.get("dat").equals(today.format(df2))) {
obj = o;
break;
}
}
res.add(obj == null ? new JSONObject().fluentPut("dat", today.format(df2)).fluentPut("playNumSum", 0).fluentPut("collectionNumSum", 0).fluentPut("reSendNumSum", 0)
.fluentPut("recommendNumSum", 0).fluentPut("commentNumSum", 0).fluentPut("commendNumSum", 0) : obj);
}
} else {
qw.between("date_format(creatAt, '%Y-%m-%d')", before, now.plusDays(1));
List<JSONObject> videoDataBydDay = adminMapper.statistics2(qw);
for (int i = 0; i <= ChronoUnit.DAYS.between(before, now); i++) {
LocalDate today = before.plusDays(i);
JSONObject obj = null;
// Stream<JSONObject> dat1 = videoDataBydDay.stream().filter(x -> LocalDate.parse((CharSequence) x.get("dat"), df) == today);
for (JSONObject o : videoDataBydDay) {
if (LocalDate.parse((CharSequence) o.get("dat"), df).equals(today)) {
obj = o;
break;
}
}
res.add(obj == null ? new JSONObject().fluentPut("dat", today).fluentPut("playNumSum", 0).fluentPut("collectionNumSum", 0).fluentPut("reSendNumSum", 0)
.fluentPut("recommendNumSum", 0).fluentPut("commentNumSum", 0).fluentPut("commendNumSum", 0) : obj);
}
}
return Result.success(res, "请求成功");
return Result.success(adminService.videoData(param), "请求成功");
}
@Data
@ -183,4 +132,30 @@ public class StatisticsController {
IPage page = (param.getType() != null && param.getType() == 1) ? userEffectRecordsMapper.rankByDepartment(pg, qw) : userEffectRecordsMapper.rankByUser(pg, qw);
return Result.success(page, "请求成功");
}
@Operation(summary = "导出excel", description = "视频数据导出")
@PostMapping("/excel2")
@AdminTokenValid
public void excel(HttpServletResponse response, @Validated @RequestBody SSParam param) throws IOException {
//这是表头及格式
String[][] array = {
{"时间", "播放", "点赞", "收藏", "评论", "转发", "推荐"},
{"dat", "playNumSum", "commendNumSum", "collectionNumSum", "commentNumSum", "reSendNumSum", "recommendNumSum"},
{"String", "int", "int", "int", "int", "int", "int"}
};
List<Map> l = adminService.videoData(param);
Workbook wb = ExcelUtil.writeToExcelByList(array, l);
OutputStream output = response.getOutputStream();
String fileName = "视频数据.xlsx";
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
wb.write(output);
output.close();
}
}

@ -1,8 +1,11 @@
package com.zh.project0512.service;
import com.zh.project0512.controller.manage.StatisticsController;
import com.zh.project0512.model.Admin;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.ArrayList;
/**
* <p>
*
@ -12,5 +15,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
* @since 2022-06-10
*/
public interface IAdminService extends IService<Admin> {
ArrayList videoData(StatisticsController.SSParam params);
}

@ -1,11 +1,25 @@
package com.zh.project0512.serviceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.project0512.controller.manage.StatisticsController;
import com.zh.project0512.model.Admin;
import com.zh.project0512.mapper.AdminMapper;
import com.zh.project0512.model.Video;
import com.zh.project0512.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
*
@ -16,5 +30,72 @@ import org.springframework.stereotype.Service;
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
@Autowired
private AdminMapper adminMapper;
public ArrayList videoData(StatisticsController.SSParam params){
int condition = params.getDateCondition();
QueryWrapper<Video> qw = new QueryWrapper<>();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM");
LocalDate now = LocalDateTime.now().toLocalDate();
LocalDate before = now.with(TemporalAdjusters.firstDayOfMonth());
switch (condition) {
case 1:
before = now;
break;
case 2:
before = now.with(DayOfWeek.MONDAY);
break;
case 3:
before = now.with(TemporalAdjusters.firstDayOfMonth());
break;
case 4:
before = now.with(TemporalAdjusters.firstDayOfYear());
break;
default:
before = params.getBeginTime() != null ? params.getBeginTime().toLocalDate() : before;
now = params.getEndTime() != null ? params.getEndTime().toLocalDate() : now;
}
if(params.getTaskId()>0){
qw.eq("taskId", params.getTaskId());
}
if(params.getUserId()>0){
qw.eq("userId", params.getUserId());
}
ArrayList<JSONObject> res = new ArrayList<>();
if (4 == condition) {
qw.between("date_format(creatAt, '%Y-%m')", before.format(df2), now.plusMonths(1).format(df2));
List<JSONObject> videoDataBydDay = adminMapper.statistics3(qw);
for (int i = 0; i <= ChronoUnit.MONTHS.between(before, now); i++) {
LocalDate today = before.plusMonths(i);
JSONObject obj = null;
// Stream<JSONObject> dat1 = videoDataBydDay.stream().filter(x -> LocalDate.parse((CharSequence) x.get("dat"), df) == today);
for (JSONObject o : videoDataBydDay) {
if (o.get("dat").equals(today.format(df2))) {
obj = o;
break;
}
}
res.add(obj == null ? new JSONObject().fluentPut("dat", today.format(df2)).fluentPut("playNumSum", 0).fluentPut("collectionNumSum", 0).fluentPut("reSendNumSum", 0)
.fluentPut("recommendNumSum", 0).fluentPut("commentNumSum", 0).fluentPut("commendNumSum", 0) : obj);
}
} else {
qw.between("date_format(creatAt, '%Y-%m-%d')", before, now.plusDays(1));
List<JSONObject> videoDataBydDay = adminMapper.statistics2(qw);
for (int i = 0; i <= ChronoUnit.DAYS.between(before, now); i++) {
LocalDate today = before.plusDays(i);
JSONObject obj = null;
// Stream<JSONObject> dat1 = videoDataBydDay.stream().filter(x -> LocalDate.parse((CharSequence) x.get("dat"), df) == today);
for (JSONObject o : videoDataBydDay) {
if (LocalDate.parse((CharSequence) o.get("dat"), df).equals(today)) {
obj = o;
break;
}
}
res.add(obj == null ? new JSONObject().fluentPut("dat", today).fluentPut("playNumSum", 0).fluentPut("collectionNumSum", 0).fluentPut("reSendNumSum", 0)
.fluentPut("recommendNumSum", 0).fluentPut("commentNumSum", 0).fluentPut("commendNumSum", 0) : obj);
}
}
return res;
}
}

Loading…
Cancel
Save