zhangjinli 3 years ago
parent fabe90aa21
commit 20a700377e

@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.UserEffectRecords;
import com.zh.project0512.model.Video;
import com.zh.project0512.model.dto.RankListDTO;
import com.zh.project0512.service.IRankService;
@ -23,6 +25,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
@ -38,8 +41,8 @@ import java.util.stream.Stream;
public class StatisticsController {
@Autowired
private AdminMapper adminMapper;
@Resource
IRankService rankService;
@Autowired
private UserEffectRecordsMapper userEffectRecordsMapper;
@Operation(summary = "总数据")
@PostMapping("/data1")
@ -67,9 +70,10 @@ public class StatisticsController {
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.minusMonths(1);
if (null != param.getDateCondition()) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime before = null;
switch (param.getDateCondition()) {
case 1:
before = now;
@ -85,39 +89,85 @@ public class StatisticsController {
break;
default:
}
qw.between("creatAt", before, LocalDateTime.now());
} else {
qw.ge(param.getBeginTime() != null, "date_format(creatAt, '%Y-%m-%d')", param.getBeginTime() != null?param.getBeginTime().format(df):null);
qw.le(param.getEndTime() != null, "date_format(creatAt, '%Y-%m-%d')", param.getEndTime() != null?param.getEndTime().format(df):null);
before = param.getBeginTime() != null ? param.getBeginTime().toLocalDate() : before;
now = param.getEndTime() != null ? param.getEndTime().toLocalDate() : now;
}
List<JSONObject> videoDataBydDay = adminMapper.statistics2(qw);
ArrayList<JSONObject> res = new ArrayList<>();
if (videoDataBydDay == null || videoDataBydDay.size() == 0) {
return Result.success(null, "请求成功");
}
JSONObject first = videoDataBydDay.get(0);
JSONObject last = videoDataBydDay.get(videoDataBydDay.size() - 1);
LocalDate dat = param.getBeginTime() == null ? LocalDate.parse((CharSequence) first.get("dat"), df) : param.getBeginTime().toLocalDate();
LocalDate dat2 = param.getEndTime() == null ? LocalDate.parse((CharSequence) last.get("dat"), df) : param.getEndTime().toLocalDate();
for (int i = 0; i <= ChronoUnit.DAYS.between(dat, dat2); i++) {
LocalDate today = dat.plusDays(i);
JSONObject obj = null;
if(param.getDateCondition() == 4){
qw.between("date_format(creatAt, '%Y-%m')", before.format(df2), now.plusMonths(1).format(df2));
List<JSONObject> videoDataBydDay =adminMapper.statistics3(qw);
if (videoDataBydDay == null || videoDataBydDay.size() == 0) {
return Result.success(null, "请求成功");
}
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 (LocalDate.parse((CharSequence) o.get("dat"), df).equals(today)) {
obj = o;
break;
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)) : obj);
}
}else {
qw.between("date_format(creatAt, '%Y-%m-%d')", before, now.plusDays(1));
List<JSONObject> videoDataBydDay = adminMapper.statistics2(qw);
if (videoDataBydDay == null || videoDataBydDay.size() == 0) {
return Result.success(null, "请求成功");
}
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) : obj);
}
res.add(obj == null ? new JSONObject().fluentPut("dat", today) : obj);
}
return Result.success(res, "请求成功");
}
@Data
static class RankParam {
@Schema(title = "时间条件", description = "1今日;2本周;3本月;4全年")
private Integer dateCondition;
@Schema(title = "1经销商2员工", description = "默认1")
private Integer type;
}
@Operation(summary = "排行列表")
@PostMapping("/data3")
@AdminTokenValid
public Result rankList(@Validated @RequestBody RankListDTO rankListDTO) {
return Result.success(rankService.rankList(rankListDTO), "请求成功");
public Result data3(@Validated @RequestBody RankParam param) {
QueryWrapper<UserEffectRecords> qw = new QueryWrapper<>();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime now = LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIN);
LocalDateTime before = now.minusMonths(1);
if (param.getDateCondition() != null) {
switch (param.getDateCondition()) {
case 1:
before = now;
break;
case 2:
before = now.minusWeeks(1);
break;
case 3:
before = now.minusMonths(1);
break;
case 4:
before = now.minusYears(1);
break;
default:
}
}
qw.between("t1.createAt", before, now.plusDays(1));
return Result.success((param.getType() != null && param.getType() == 1) ? userEffectRecordsMapper.rankByDepartment(qw) : userEffectRecordsMapper.rankByUser(qw), "请求成功");
}
}

@ -52,4 +52,23 @@ public interface AdminMapper extends BaseMapper<Admin> {
"GROUP BY dat ORDER BY dat "+
"</script>")
List<JSONObject> statistics2(@Param("ew") Wrapper<Video> queryWrapper);
@Select("<script>" +
"SELECT date_format(creatAt, '%Y-%m') as dat" +
",sum(IFNULL(playNum,0)) as playNumSum " +
",sum(IFNULL(commendNum,0)) as commendNumSum " +
",sum(IFNULL(collectionNum,0)) as collectionNumSum " +
",sum(IFNULL(commentNum,0)) as commentNumSum " +
",sum(IFNULL(reSendNum,0)) as reSendNumSum " +
",sum(IFNULL(recommendNum,0)) as recommendNumSum " +
"from video \n" +
"where date_format(creatAt, '%Y-%m')" +
"<if test='ew != null'>" +
"<if test='ew.nonEmptyOfWhere'>" +
"AND " +
"</if> " +
"${ew.SqlSegment}" +
"</if> " +
"GROUP BY dat ORDER BY dat "+
"</script>")
List<JSONObject> statistics3(@Param("ew") Wrapper<Video> queryWrapper);
}

@ -1,9 +1,15 @@
package com.zh.project0512.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zh.project0512.model.User;
import com.zh.project0512.model.UserEffectRecords;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
/**
* <p>
* Mapper
@ -18,4 +24,16 @@ public interface UserEffectRecordsMapper extends BaseMapper<UserEffectRecords> {
" VALUES " +
"(#{userId},(SELECT main_department from user WHERE id = #{userId}),#{effectResultTran},#{createAt})")
void saveEffectResult(UserEffectRecords userEffectRecords);
@Select("SELECT t1.departmentId,t3.name as title,IFNULL(sum(t1.effectResultTran),0) as effectValue from userEffectRecords as t1\n" +
"INNER JOIN qywxDepartment as t3 on t3.departmentId = t1.departmentId\n" +
"${ew.customSqlSegment} " +
"GROUP BY departmentId\n" +
"ORDER BY effectValue DESC")
List<Map> rankByDepartment(@Param("ew") Wrapper<UserEffectRecords> queryWrapper);
@Select("SELECT t1.userId,t2.name as title,t2.departmentId,t2.departmentName as subtitle,sum(t1.effectResultTran) as effectValue from userEffectRecords as t1\n" +
"INNER JOIN (SELECT t1.id,t1.name,t2.departmentId,t2.name as departmentName FROM user as t1 left join qywxDepartment as t2 on t1.main_department = t2.departmentId) as t2 on t1.userId = t2.id\n" +
"${ew.customSqlSegment} " +
"GROUP BY userId\n" +
"ORDER BY effectValue DESC")
List<Map> rankByUser(@Param("ew") Wrapper<UserEffectRecords> queryWrapper);
}

Loading…
Cancel
Save