无关风月
2024-11-04 64f7ccb9ef8b5a0618e65cddc14b981c1f108ba3
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package com.xinquan.system.controller;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.utils.WebUtils;
import com.xinquan.common.core.utils.page.BeanUtils;
import com.xinquan.common.core.utils.page.PageDTO;
import com.xinquan.course.api.domain.Course;
import com.xinquan.course.api.domain.CourseCategory;
import com.xinquan.course.api.domain.CourseDTO;
import com.xinquan.course.api.feign.RemoteCourseService;
import com.xinquan.meditation.api.feign.RemoteMeditationService;
import com.xinquan.order.api.domain.Order;
import com.xinquan.order.api.domain.vo.HomeDto;
import com.xinquan.order.api.feign.RemoteOrderService;
import com.xinquan.system.api.domain.vo.HomeVO;
import com.xinquan.system.domain.Banner;
import com.xinquan.system.domain.export.HomeExport;
import com.xinquan.system.service.BannerService;
import com.xinquan.user.api.feign.RemoteAppUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/system/home")
public class HomeController {
 
    @Resource
    private BannerService bannerService;
    @Resource
    private RemoteCourseService remoteCourseService;
    @Resource
    private RemoteAppUserService remoteAppUserService;
    @Resource
    private RemoteMeditationService remoteMeditationService;
    @Resource
    private RemoteOrderService remoteOrderService;
    @PostMapping("/homeStatistics")
    @ApiOperation(value = "首页统计",tags = "管理后台-首页统计")
    public R<HomeVO> bannerList(@RequestBody HomeDto homeDto) {
        HomeVO homeVO = new HomeVO();
//        homeVO.setOrderMoney();
//        homeVO.setOrderCount();
//        homeVO.setOrderList();
//        homeVO.setOrderTimeList();
        // 查询用户总数、非会员数、月卡会员数、季卡会员数、年卡会员数
        String data = remoteAppUserService.getUserCount().getData();
        if (data!=null){
            String[] split = data.split(",");
            homeVO.setUserCount(split[0]);
            homeVO.setNoVip(split[1]);
            homeVO.setVipMonth(split[2]);
            homeVO.setVipQuarter(split[3]);
            homeVO.setVipYear(split[4]);
        }
        // 查询课程总数 免费、会员免费、单独收费、线下课程数
        String data1 = remoteCourseService.getCourseCount().getData();
        if (data1!=null){
            String[] split = data1.split(",");
            homeVO.setCourseCount(split[0]);
            homeVO.setCourseFree(split[1]);
            homeVO.setCourseVip(split[2]);
            homeVO.setCoursePay(split[3]);
            homeVO.setCourseOffOnline(split[4]);
        }
        // 查询疗愈总数 免费、会员免费、单独收费
        String data2 = remoteMeditationService.getMeditationCount().getData();
        if (data2!=null){
            String[] split = data2.split(",");
            homeVO.setMeditationCount(split[0]);
            homeVO.setMeditationFree(split[1]);
            homeVO.setMeditationVip(split[2]);
            homeVO.setMeditationPay(split[3]);
        }
        String data3 = remoteMeditationService.getQuestionCount().getData();
        if (data3!=null){
            homeVO.setQuestionRecord(data3);
        }
        String data4 = remoteAppUserService.getPrizeRecordCount().getData();
        if (data4!=null){
            homeVO.setPrizeRecord(data4);
        }
        // 查询用户总数列表变化趋势
        Map<String, Object> data5 = remoteAppUserService.getUserListCount().getData();
        homeVO.setUserList(data5);
        // 查询时间范围内的订单列表 课程 疗愈 会员
        if (!StringUtils.hasLength(homeDto.getTime())){
            homeDto.setTime("1");
        }
        if(homeDto.getTimeType() == null){
            homeDto.setTimeType(1);
        }
        List<Order> data6 = remoteOrderService.getOrderList(homeDto.getStatus(), homeDto.getTimeType(), homeDto.getTime()).getData();
        homeVO.setOrderCount(data6.size()+"");
        BigDecimal bigDecimal = new BigDecimal("0");
        for (Order order : data6) {
            if (order.getRealPayAmount()!=null){
                bigDecimal= bigDecimal.add(order.getRealPayAmount());
            }
        }
        homeVO.setOrderMoney(bigDecimal+"");
        Map<String, Object> x = new HashMap<>();
        Map<String, Object> x1 = new HashMap<>();
        Map<String, Object> x2 = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        switch (homeDto.getTimeType()){
            case 1:
                // 年
                x.put("1",0);
                x.put("2",0);
                x.put("3",0);
                x.put("4",0);
                x.put("5",0);
                x.put("6",0);
                x.put("7",0);
                x.put("8",0);
                x.put("9",0);
                x.put("10",0);
                x.put("11",0);
                x.put("12",0);
 
                x1.put("1",0);
                x1.put("2",0);
                x1.put("3",0);
                x1.put("4",0);
                x1.put("5",0);
                x1.put("6",0);
                x1.put("7",0);
                x1.put("8",0);
                x1.put("9",0);
                x1.put("10",0);
                x1.put("11",0);
                x1.put("12",0);
 
                x2.put("1",0);
                x2.put("2",0);
                x2.put("3",0);
                x2.put("4",0);
                x2.put("5",0);
                x2.put("6",0);
                x2.put("7",0);
                x2.put("8",0);
                x2.put("9",0);
                x2.put("10",0);
                x2.put("11",0);
                x2.put("12",0);
                break;
            case 2:
                // 获取当前日期格式为 yyyy-MM-dd
                Date date = new Date();
                String format = simpleDateFormat.format(date);
                x.put(format,0);
                x1.put(format,0);
                x2.put(format,0);
                break;
            case 3:
                // 周
                // 获取本周7天 格式为dd
 
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                for (int i = 1; i <= 7; i++) {
                    Date time = calendar.getTime();
                    String format1 = simpleDateFormat.format(time);
                    x.put(format1,0);
                    x1.put(format1,0);
                    x2.put(format1,0);
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                }
                break;
            case 4:
                // 月
                // 获取本月31天 格式为dd
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                for (int i = 1; i <= 31; i++) {
                    Date time = calendar.getTime();
                    String format1 = simpleDateFormat.format(time);
                    x.put(format1,0);
                    x1.put(format1,0);
                    x2.put(format1,0);
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                }
                break;
        }
 
        for (Order order : data6) {
            int value = order.getCreateTime().getMonth().getValue();
            int value1 = order.getCreateTime().getDayOfMonth();
            switch (order.getOrderFrom()){
                case 1:
                    switch (homeDto.getTimeType()){
                        case 1:
                            // 获取订单月份
                            x.put(value+"",(int)x.get(value+"")+1);
                            break;
                        case 2:
                            Date date = new Date();
                            String format = simpleDateFormat.format(date);
                            x.put(format,(int)x.get(format)+1);
                            break;
                        case 3:
                            x.put(value1+"",(int)x.get(value1+"")+1);
                            break;
                        case 4:
                            x.put(value1+"",(int)x.get(value1+"")+1);
                            break;
                    }
                    break;
                case 2:
                    switch (homeDto.getTimeType()){
                        case 1:
                            // 获取订单月份
                            x1.put(value+"",(int)x1.get(value+"")+1);
                            break;
                        case 2:
                            Date date = new Date();
                            String format = simpleDateFormat.format(date);
                            x1.put(format,(int)x1.get(format)+1);
                            break;
                        case 3:
                            x1.put(value1+"",(int)x1.get(value1+"")+1);
                            break;
                        case 4:
                            x1.put(value1+"",(int)x1.get(value1+"")+1);
                            break;
                    }
                    break;
                case 3:
                    switch (homeDto.getTimeType()){
                        case 1:
                            // 获取订单月份
                            x2.put(value+"",(int)x2.get(value+"")+1);
                            break;
                        case 2:
                            Date date = new Date();
                            String format = simpleDateFormat.format(date);
                            x2.put(format,(int)x2.get(format)+1);
                            break;
                        case 3:
                            x2.put(value1+"",(int)x2.get(value1+"")+1);
                            break;
                        case 4:
                            x2.put(value1+"",(int)x2.get(value1+"")+1);
                            break;
                    }
                    break;
            }
        }
        switch (homeDto.getTimeType()){
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
        }
        // 对x的key值进行从小到大排序
        Map<String, Object> sortedMap = new TreeMap<>(Comparator.reverseOrder());
        sortedMap.putAll(x);
        Map<String, Object> sortedMap1 = new TreeMap<>(Comparator.reverseOrder());
        sortedMap1.putAll(x1);
        Map<String, Object> sortedMap2 = new TreeMap<>(Comparator.reverseOrder());
        sortedMap2.putAll(x2);
        homeVO.setOrderMeditationList(sortedMap);
        homeVO.setOrderCourseList(sortedMap1);
        homeVO.setOrderVipList(sortedMap2);
        return R.ok(homeVO);
    }
    @PutMapping("/homeStatisticsExport")
    @ApiOperation(value = "首页统计-导出",tags = "管理后台-首页统计")
    public void homeStatisticsExport() {
        HomeVO homeVO = new HomeVO();
//        homeVO.setOrderMoney();
//        homeVO.setOrderCount();
//        homeVO.setOrderList();
//        homeVO.setOrderTimeList();
        // 查询用户总数、非会员数、月卡会员数、季卡会员数、年卡会员数
        String data = remoteAppUserService.getUserCount().getData();
        if (data!=null){
            String[] split = data.split(",");
            homeVO.setUserCount(split[0]);
            homeVO.setNoVip(split[1]);
            homeVO.setVipMonth(split[2]);
            homeVO.setVipQuarter(split[3]);
            homeVO.setVipYear(split[4]);
        }
        // 查询课程总数 免费、会员免费、单独收费、线下课程数
        String data1 = remoteCourseService.getCourseCount().getData();
        if (data1!=null){
            String[] split = data1.split(",");
            homeVO.setCourseCount(split[0]);
            homeVO.setCourseFree(split[1]);
            homeVO.setCourseVip(split[2]);
            homeVO.setCoursePay(split[3]);
            homeVO.setCourseOffOnline(split[4]);
        }
        // 查询疗愈总数 免费、会员免费、单独收费
        String data2 = remoteMeditationService.getMeditationCount().getData();
        if (data2!=null){
            String[] split = data2.split(",");
            homeVO.setMeditationCount(split[0]);
            homeVO.setMeditationFree(split[1]);
            homeVO.setMeditationVip(split[2]);
            homeVO.setMeditationPay(split[3]);
        }
        String data3 = remoteMeditationService.getQuestionCount().getData();
        if (data3!=null){
            homeVO.setQuestionRecord(data3);
        }
        String data4 = remoteAppUserService.getPrizeRecordCount().getData();
        if (data4!=null){
            homeVO.setPrizeRecord(data4);
        }
 
 
        HomeExport homeExport = new HomeExport();
        BeanUtils.copyProperties(homeVO, homeExport);
        List<HomeExport> homeExports = new ArrayList<>();
        homeExports.add(homeExport);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), HomeExport.class, homeExports);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("首页统计导出.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @PostMapping("/cateList")
    @ApiOperation(value = "课程分类下拉选择列表-不分页",tags = "管理后台banner")
 
    public R<List<CourseCategory>> cateList() {
        List<CourseCategory> data = remoteCourseService.cateList().getData();
        return R.ok(data);
    }
    @GetMapping("/courseList")
    @ApiOperation(value = "选择课程-分页",tags = "管理后台banner")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分页参数,当前页码", name = "pageCurr", required = false, dataType = "Integer"),
            @ApiImplicitParam(value = "分页参数,每页数量", name = "pageSize", required = false, dataType = "Integer"),
            @ApiImplicitParam(value = "课程类型1线上2线下", name = "courseType", required = false, dataType = "Integer"),
            @ApiImplicitParam(value = "课程分类id", name = "cateId", required = false, dataType = "Long"),
            @ApiImplicitParam(value = "课程标题", name = "courseTitle", required = false, dataType = "String"),
            @ApiImplicitParam(value = "导师名称", name = "tutor", required = false, dataType = "String"),
    })
    public R<PageDTO<Course>> courseList(
            Integer pageCurr,
            Integer pageSize,
         Integer courseType,
     Long cateId,
       String courseTitle,
 
            String tutor
            ) {
        CourseDTO courseDTO = new CourseDTO();
        courseDTO.setPageCurr(pageCurr);
        courseDTO.setPageSize(pageSize);
        courseDTO.setCourseType(courseType);
        courseDTO.setCateId(cateId);
        courseDTO.setCourseTitle(courseTitle);
        courseDTO.setTutor(tutor);
        PageDTO<Course> data = remoteCourseService.courseList(courseDTO).getData();
        return R.ok(data);
    }
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除banner",tags = "管理后台banner")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "ids", name = "ids", required = true, dataType = "String"),
    })
    public R deleteBatch(
            @RequestParam(value = "ids") String ids)
    {
        List<Long> list = Arrays.asList(ids.split(",")).stream().map(Long::valueOf).collect(Collectors.toList());
        bannerService.removeBatchByIds(list);
        return R.ok();
    }
    @PostMapping("/addBanner")
    @ApiOperation(value = "添加banner",tags = "管理后台banner")
 
    public R addBanner(@RequestBody Banner banner)
    {
        bannerService.save(banner);
        return R.ok();
    }
    @PostMapping("/updateBanner")
    @ApiOperation(value = "修改banner",tags = "管理后台banner")
 
    public R updateBanner(@RequestBody Banner banner)
    {
        bannerService.updateById(banner);
        return R.ok();
    }
    @PostMapping("/bannerDetail")
    @ApiOperation(value = "查看详情",tags = "管理后台banner")
 
    public R<Banner> bannerDetail(String uid)
    {
        Banner byId = bannerService.getById(uid);
        if (byId.getCourseId()!=null){
            Course data = remoteCourseService.getCourseById(byId.getCourseId()).getData();
            if (data!=null){
                byId.setCourseTitle(data.getCourseTitle());
                byId.setCoverUrl(data.getCoverUrl());
                byId.setTutor(data.getTutor());
                byId.setPrice(data.getGeneralPrice());
                if (data.getCateId()!=null){
                    CourseCategory data1 = remoteCourseService.getCategoryById(data.getCateId().toString()).getData();
                    byId.setCate(data1.getName());
                }
            }
        }
        return R.ok(byId);
    }
 
}