peng
昨天 672637fa4a4914ec9667cb15f70b67482b8a0e5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package cn.lili.modules.member.serviceimpl;
 
import cn.lili.base.Result;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.lmk.domain.query.FootPrintQuery;
import cn.lili.modules.lmk.domain.vo.*;
import cn.lili.modules.lmk.mapper.VideoMapper;
import cn.lili.modules.member.entity.dos.FootPrint;
import cn.lili.modules.member.entity.dto.FootPrintQueryParams;
import cn.lili.modules.member.mapper.FootprintMapper;
import cn.lili.modules.member.service.FootprintService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.statistics.entity.dto.StatisticsQueryParam;
import cn.lili.mybatis.util.PageUtil;
import cn.lili.utils.COSUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
/**
 * 会员浏览历史业务层实现
 *
 * @author Chopper
 * @since 2020/11/18 10:46 上午
 */
@Service
public class FootprintServiceImpl extends ServiceImpl<FootprintMapper, FootPrint> implements FootprintService {
 
 
    @Autowired
    private GoodsSkuService goodsSkuService;
 
    @Autowired
    private VideoMapper videoMapper;
 
    @Autowired
    private COSUtil cOSUtil;
 
    @Override
    public FootPrint saveFootprint(FootPrint footPrint) {
        this.save(footPrint);
        return footPrint;
    }
 
    @Override
    public boolean clean() {
        LambdaQueryWrapper<FootPrint> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(FootPrint::getMemberId, UserContext.getCurrentUser().getId());
        return this.remove(lambdaQueryWrapper);
    }
 
    @Override
    public boolean deleteByIds(List<String> ids, String viewType) {
        LambdaQueryWrapper<FootPrint> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(FootPrint::getMemberId, UserContext.getCurrentUser().getId());
        lambdaQueryWrapper.eq(FootPrint::getViewType, viewType);
        lambdaQueryWrapper.in(FootPrint::getRefId, ids);
        return this.remove(lambdaQueryWrapper);
    }
 
    @Override
    public IPage<EsGoodsIndex> footPrintPage(FootPrintQueryParams params) {
        params.setSort("createTime");
        Page<FootPrint> footPrintPages = this.page(PageUtil.initPage(params), params.queryWrapper());
        //定义结果
        Page<EsGoodsIndex> esGoodsIndexIPage = new Page<>();
        if (footPrintPages.getRecords() != null && !footPrintPages.getRecords().isEmpty()) {
            List<String> skuIds = footPrintPages.getRecords().stream().map(FootPrint::getSkuId).collect(Collectors.toList());
            List<GoodsSku> goodsSkuByIdFromCache = goodsSkuService.getGoodsSkuByIdFromCache(skuIds);
            List<EsGoodsIndex> collect = IntStream.range(0, goodsSkuByIdFromCache.size())
                    .mapToObj(i -> {
                        if (goodsSkuByIdFromCache.get(i) == null) {
                            return null;
                        }
                        Optional<FootPrint> first =
                                footPrintPages.getRecords().stream().filter(j -> goodsSkuByIdFromCache.get(i).getId().equals(j.getSkuId())).findFirst();
                        return first.map(footPrint -> new EsGoodsIndex(goodsSkuByIdFromCache.get(i), footPrint.getCreateTime())).orElseGet(() -> new EsGoodsIndex(goodsSkuByIdFromCache.get(i)));
                    })
                    .collect(Collectors.toList());
 
            collect.removeIf(Objects::isNull);
 
            esGoodsIndexIPage.setPages(footPrintPages.getPages());
            esGoodsIndexIPage.setRecords(collect);
            esGoodsIndexIPage.setTotal(footPrintPages.getTotal());
            esGoodsIndexIPage.setSize(footPrintPages.getSize());
            esGoodsIndexIPage.setCurrent(footPrintPages.getCurrent());
            return esGoodsIndexIPage;
        }
        return esGoodsIndexIPage;
    }
 
    @Override
    public Result videoFootPrintPage(FootPrintQuery query) {
        IPage<VideoFootVO> page = cn.lili.utils.PageUtil.getPage(query,VideoFootVO.class);
        videoMapper.videoFootPage(page,query);
 
        VideoFootInfoVo videoFootInfoVo = videoMapper.getVideoFootInfo(query.getMemberId());
 
        for (VideoFootVO videoFootVO : page.getRecords()) {
            videoFootVO.setCoverCOSUrl(cOSUtil.getPreviewUrl(videoFootVO.getCoverUrl()));
            //计算播放进度
            if(videoFootVO.getPlayAt() == null || videoFootVO.getPlayAt().isEmpty()){
                videoFootVO.setPlayProgress(0.0);
                continue;
            }
 
            BigDecimal value = BigDecimal.valueOf(Double.parseDouble(videoFootVO.getPlayAt()) / videoFootVO.getVideoDuration())
                    .setScale(2, RoundingMode.HALF_UP);
 
            if (value.compareTo(BigDecimal.ONE) > 0) {
                // 这里可以添加处理逻辑,比如:自动修正为1.0
                value = BigDecimal.ONE;
            }
 
            videoFootVO.setPlayProgress(value.doubleValue());
        }
 
        HashMap<String,Object> map = new HashMap<>();
        map.put("data", page.getRecords());
        map.put("total", page.getTotal());
        if (videoFootInfoVo == null) {
            map.put("avgProgress",0);
            map.put("totalDuration",0);
        }else {
            map.put("avgProgress",videoFootInfoVo.getAvgCompletionRate());
            map.put("totalDuration",videoFootInfoVo.getTotalDuration());
        }
        return Result.ok().data(map);
    }
 
    @Override
    public Result memberActionAnalyse(String id){
        //视频分类
        List<VideoFootVO> list = videoMapper.videoFoot(id);
        Map<String, Long> tagCountMap = list.stream()
                .flatMap(video -> video.getTagList().stream()) // 展开所有 tag
                .filter(Objects::nonNull)                      // 过滤 null
                .collect(Collectors.groupingBy(
                        SimpleVideoTagVO::getTagName,          // 按 tagName 分组
                        Collectors.counting()                  // 计算每个 tagName 出现的次数
                ));
 
        return Result.ok().data(tagCountMap);
    }
 
 
    @Override
    public long getFootprintNum() {
        LambdaQueryWrapper<FootPrint> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(FootPrint::getMemberId, Objects.requireNonNull(UserContext.getCurrentUser()).getId());
        lambdaQueryWrapper.eq(FootPrint::getDeleteFlag, false);
        return this.count(lambdaQueryWrapper);
    }
 
    @Override
    public Result getViewAndCompletionRateCount(StatisticsQueryParam queryParam) {
        Date startTime = null;
        Date endTime = new Date(); // 结束时间默认是当前时间
        List<String> dateList = new ArrayList<>();
 
        Calendar calendar = Calendar.getInstance(); // 用于日期计算的日历实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        switch (queryParam.getSearchType()) {
            case "TODAY":
                // 今天:从今天0点到现在
                calendar.setTime(new Date()); // 重置为当前时间
                calendar.set(Calendar.HOUR_OF_DAY, 0); // 小时设为0(24小时制)
                calendar.set(Calendar.MINUTE, 0);      // 分钟设为0
                calendar.set(Calendar.SECOND, 0);      // 秒设为0
                calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0
                startTime = calendar.getTime();        // 得到今天0点的Date对象
                dateList.add(sdf.format(startTime));
                break;
            case "YESTERDAY":
                // 昨天:从昨天0点到昨天23:59:59.999
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, -1); // 日期减1天(变为昨天)
 
                // 设置昨天0点
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                startTime = calendar.getTime();
 
                // 设置昨天23:59:59.999
                calendar.set(Calendar.HOUR_OF_DAY, 23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                calendar.set(Calendar.MILLISECOND, 999);
                endTime = calendar.getTime();
 
                dateList.add(sdf.format(startTime));
                break;
            case "LAST_SEVEN":
                // 过去七天:从7天前0点到现在(含今天共7天)
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, -6); // 日期减6天(7天前的今天)
 
                // 设置7天前0点
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                startTime = calendar.getTime();
 
                days = 7;
                // 循环生成7天的日期字符串
                Calendar tempCalendar = Calendar.getInstance();
                tempCalendar.setTime(startTime);
                for (int i = 0; i < days; i++) {
                    dateList.add(sdf.format(tempCalendar.getTime()));
                    tempCalendar.add(Calendar.DATE, 1); // 每天累加1天
                }
                break;
            case "LAST_THIRTY":
                // 过去30天:从30天前0点到现在(含今天共30天)
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, -29); // 日期减29天(30天前的今天)
 
                // 设置30天前0点
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                startTime = calendar.getTime();
 
                days = 30;
                // 循环生成30天的日期字符串
                Calendar tempCalendar30 = Calendar.getInstance();
                tempCalendar30.setTime(startTime);
                for (int i = 0; i < days; i++) {
                    dateList.add(sdf.format(tempCalendar30.getTime()));
                    tempCalendar30.add(Calendar.DATE, 1); // 每天累加1天
                }
                break;
            default:
                return Result.error("不支持的时间范围类型");
        }
        if ("goods".equals(queryParam.getCurrentType())){
            List<Map<String, Object>> maps = baseMapper.selectViewAndCompletionRateCountByDay(startTime, endTime,queryParam.getCurrentLimit());
            Map<String,Long> viewPrintMap = maps.stream()
                    .collect(Collectors.toMap(
                            map -> {
                                String goodsId = Objects.toString(map.get("GoodsId"), ""); // 空值处理
                                String goodsName = Objects.toString(map.get("GoodsName"), ""); // 空值处理
                                return goodsName + " | " + goodsId; // 用|分隔,方便后续拆分
                            },
                            map-> Long.valueOf(map.get("view_count").toString()),
                            (existing, replacement) -> existing,
                            LinkedHashMap::new  // 指定使用 LinkedHashMap,保留插入顺序
                    ));
            List<String> yData = new ArrayList<>();
            List<Long> xData = new ArrayList<>();
 
            for (Map.Entry<String, Long> entry : viewPrintMap.entrySet()){
                yData.add(entry.getKey());
                xData.add(entry.getValue());
            }
            Map<String,Object> data = new HashMap<>();
            data.put("yData", yData);
            data.put("xData", xData);
            return Result.ok().data(data);
        }else{
            List<Map<String, Object>> maps = baseMapper.selectEachVideoStats(startTime, endTime,queryParam.getCurrentLimit());
            Map<String, VideoViewCompletionRateVO> viewPrintMap = maps.stream()
                    .collect(Collectors.toMap(
                            map -> {
                                String videoId = Objects.toString(map.get("video_id"), ""); // 空值处理
                                String videoName = Objects.toString(map.get("title"), ""); // 空值处理
                                return videoName + " | " + videoId; // 用|分隔,方便后续拆分
                            },
                            map-> {
                                VideoViewCompletionRateVO vo = new VideoViewCompletionRateVO();
                                vo.setTotal_views(Long.valueOf(map.get("total_views").toString()));
                                vo.setComplete_rate(map.get("complete_rate").toString());
                                return vo;
                            },
                            (existing, replacement) -> existing,
                            LinkedHashMap::new  // 指定使用 LinkedHashMap,保留插入顺序
                    ));
            List<String> yData = new ArrayList<>();
            List<Long> xData = new ArrayList<>();
            List<String> rateData = new ArrayList<>();
 
            for (Map.Entry<String, VideoViewCompletionRateVO> entry : viewPrintMap.entrySet()){
                yData.add(entry.getKey());
                xData.add(entry.getValue().getTotal_views());
                rateData.add(entry.getValue().getComplete_rate());
            }
            Map<String,Object> data = new HashMap<>();
            data.put("yData", yData);
            data.put("xData", xData);
            data.put("rateData", rateData);
            return Result.ok().data(data);
        }
 
    }
 
 
}