huliguo
2025-04-23 f2070facdb5715e7349df69cfe257289c680d292
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
package com.ruoyi.account.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.model.UserPoint;
import com.ruoyi.account.mapper.UserPointMapper;
import com.ruoyi.account.service.UserPointService;
import com.ruoyi.account.vo.*;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.order.feignClient.OrderClient;
import com.ruoyi.order.model.Order;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.Collection;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-21
 */
@RestController
@RequestMapping("/user-point")
@Api(tags = "个人积分")
public class UserPointController extends BaseController {
    @Resource
    private UserPointService userPointService;
    @Resource
    private OrderClient orderClient;
    @Resource
    private UserPointMapper userPointMapper;
 
 
    /**
     * 获取个人积分
     */
    @GetMapping("/getUserPoint")
    @ApiOperation("获取个人积分")
    public R<UserPointVO> getUserPoint() {
        return R.ok(userPointService.getUserPoint(SecurityUtils.getUserId()));
    }
 
    /**
     * 获取变更明细
     */
    @GetMapping("/getUserPointDetail")
    @ApiOperation("获取变更明细")
    public R<PageInfo<UserPointDetailVO>> getUserPointDetail(@ApiParam("指定日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
                                          @ApiParam("变动类型(4=兑换商品,12=他人赠送,13=赠与他人,16=取消订单,17=充值 )") Integer type, Integer pageCurr, Integer pageSize) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        if (date != null) {
            // 将 createTime 设置为当天的开始时间 (00:00)
            startTime = date.atStartOfDay();
 
            // 使用 YearMonth 来获取该月的最后一天
            YearMonth yearMonth = YearMonth.from(date);
            LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
 
            // 将最后一天转换为 LocalDateTime,并设置为当天的最后一秒 (23:59:59.999)
            endTime = lastDayOfMonth.atTime(LocalTime.MAX);
        }
 
        PageInfo<UserPointDetailVO> userPointDetail = userPointService.getUserPointDetail(SecurityUtils.getUserId(), startTime, endTime, type, pageCurr, pageSize);
        return R.ok(userPointDetail);
    }
 
    /**
     * 转赠积分
     */
    @PostMapping("/transferPoint")
    @ApiOperation("转赠积分")
    public R<Void> transferPoint(@RequestBody TransferPoint transferPoint) {
        return userPointService.transferPoint(transferPoint.getPoint(), transferPoint.getPhone());
    }
 
 
    /**
     * 保存积分流水记录
     *
     * @param userPoint
     * @return
     */
    @PostMapping("/saveUserPoint")
    public R saveUserPoint(@RequestBody UserPoint userPoint) {
        userPointService.save(userPoint);
        return R.ok();
    }
 
 
    /**
     * 删除用户积分流水
     */
 
    @DeleteMapping("/deleteUserPoint")
    public R deleteUserPoint(@RequestParam("orderId") Long orderId) {
        userPointService.remove(new LambdaQueryWrapper<UserPoint>().eq(UserPoint::getObjectId, orderId));
        return R.ok();
    }
 
 
    /**
     * 积分统计
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "积分统计", tags = "管理后台-财务统计-用户积分统计")
    public R<UserPointStatistics> statistics(UserPoint userPoint) {
        return R.ok(userPointService.getStatistics(userPoint));
    }
 
    /**
     * 变更记录
     */
    @GetMapping("/list")
    @ApiOperation(value = "积分变更记录", tags = "管理后台-财务统计-用户积分统计")
    public R<IPage<UserPoint>> list(UserPoint userPoint) {
        IPage<UserPoint> userPointPage = userPointService.getUserPointPage(Page.of(userPoint.getPageNum(), userPoint.getPageSize()), userPoint);
        return R.ok(userPointPage);
    }
 
    
    @GetMapping("/user/list")
    @ApiOperation(value = "积分管理-用户积分明细(必传用户id)", tags = "后台")
    public R<Page<UserPoint>> userlist(UserPoint userPoint) {
        Page<UserPoint> page = userPointService.lambdaQuery()
                .eq(userPoint.getType()!=null,UserPoint::getType, userPoint.getType())
                .eq(UserPoint::getAppUserId, userPoint.getAppUserId())
                .orderByDesc(UserPoint::getCreateTime)
                .page(Page.of(userPoint.getPageNum(), userPoint.getPageSize()));
        for (UserPoint record : page.getRecords()) {
            if (record.getType()==1 || record.getType()==11){
                Order data = orderClient.getOrderById(record.getObjectId()).getData();
                if (data!=null){
                    record.setExtention(data.getOrderNumber());
                }
            }
            int i = record.getHistoricalPoint() - record.getBalance();
            if (i>0){
                record.setVariableType(2);
            }else if (i<0){
                record.setVariableType(1);
            }else{
                record.setVariableType(0);
            }
        }
        return R.ok(page);
    }
 
 
 
    /**
     * 导出
     */
    @GetMapping("/export")
    @ApiOperation(value = "积分变更记录导出", tags = "管理后台-财务统计-用户积分统计")
    public void export(HttpServletResponse response, UserPoint userPoint) {
        IPage<UserPoint> userPointPage = userPointService.getUserPointPage(Page.of(1, Integer.MAX_VALUE), userPoint);
        List<UserPoint> userPointList = userPointPage.getRecords();
        ExcelUtil<UserPoint> util = new ExcelUtil<>(UserPoint.class);
        util.exportExcel(response, userPointList, "用户积分统计");
    }
 
 
    /**
     * 获取积分变动记录
     * @param userPoint
     * @return
     */
    @PostMapping("/getUserPointList")
    public R<List<UserPoint>> getUserPointList(@RequestBody UserPoint userPoint){
        LambdaQueryWrapper<UserPoint> queryWrapper = new LambdaQueryWrapper<>();
        if(null != userPoint.getType()){
            queryWrapper.eq(UserPoint::getType, userPoint.getType());
        }
        if(null != userPoint.getObjectId()){
            queryWrapper.eq(UserPoint::getObjectId, userPoint.getObjectId());
        }
        if(null != userPoint.getAppUserId()){
            queryWrapper.eq(UserPoint::getAppUserId, userPoint.getAppUserId());
        }
        List<UserPoint> list = userPointService.list(queryWrapper);
        return R.ok(list);
    }
 
 
    /**
     * 获取用户积分变更详情
     */
    @GetMapping("/getUserPontDetailPageList")
    @ApiOperation(value = "用户积分详情", tags = "管理后台-财务统计-用户积分统计")
    public R<PageInfo<UserPointDetailVO>> getUserPontDetailPageList(@RequestParam(value = "types",required = false) Collection<Integer> types,
                                                                               @RequestParam(value = "id") Long id,
                                                                               @ApiParam("当前页")@RequestParam("pageCurr") Integer pageCurr,
                                                                               @ApiParam("分页大小")@RequestParam("pageSize") Integer pageSize) {
        PageInfo<UserPointDetailVO> pageInfo=userPointService.getUserPontDetailPageList(types,id,pageCurr,pageSize);
 
        return R.ok(pageInfo);
    }
 
    /**
     * 用户积分统计
     */
    @GetMapping("/getUserPointStatisticsPageList")
    @ApiOperation(value = "用户积分统计", tags = "管理后台-财务统计-用户积分统计")
    public R<PageInfo<UserPointStatisticsPageVO>> getUserPointStatisticsPageList(@RequestParam(value = "types",required = false) Collection<Integer> types,
                                                                    @RequestParam(value = "name",required = false) String name,
                                                                    @RequestParam(value = "phone",required = false) String phone,
                                                                    @RequestParam(value = "beginTime",required = false) LocalDateTime beginTime,
                                                                    @RequestParam(value = "endTime",required = false) LocalDateTime endTime,
                                                                    @ApiParam("当前页")@RequestParam("pageCurr") Integer pageCurr,
                                                                    @ApiParam("分页大小")@RequestParam("pageSize") Integer pageSize) {
        //查找记录
        PageInfo<UserPointStatisticsPageVO> pageInfo=userPointService.getUserPointStatisticsPageList(types,name,phone,beginTime,endTime,pageCurr,pageSize);
        return R.ok(pageInfo);
    }
 
    /**
     * 用户积分统计
     */
    @GetMapping("/getUserPointStatisticsTop")
    @ApiOperation(value = "用户积分统计-顶部", tags = "管理后台-财务统计-用户积分统计")
    public R<UserPointStatisticsTopVO> getUserPointStatisticsTop(@RequestParam(value = "types",required = false) Collection<Integer> types,
                                                                      @RequestParam(value = "name",required = false) String name,
                                                                      @RequestParam(value = "phone",required = false) String phone,
                                                                      @RequestParam(value = "beginTime",required = false) LocalDateTime beginTime,
                                                                      @RequestParam(value = "endTime",required = false) LocalDateTime endTime
                                                                     ) {
        UserPointStatisticsTopVO userPointStatisticsOutVO = new UserPointStatisticsTopVO();
        //充值绿电分
 
        Integer chargePoint=userPointService.selectRechargeAndUse(name,phone,beginTime,endTime,17);
        // 消费绿电分
        Integer exchangePoint =userPointService.selectRechargeAndUse(name,phone,beginTime,endTime,4);
        Integer cancelPoint = userPointService.selectRechargeAndUse(name,phone,beginTime,endTime,16);
        Integer usePoint=(exchangePoint==null?0:exchangePoint )-( cancelPoint==null?0:cancelPoint);
        userPointStatisticsOutVO.setChargeTotalPoint(chargePoint==null?0:chargePoint);
        userPointStatisticsOutVO.setUseTotalPoint(usePoint);
        return R.ok(userPointStatisticsOutVO);
    }
 
    /**
     * 导出店铺余额列表
     */
 
    @GetMapping("/userPointExcel")
    @ApiOperation(value = "导出", tags = {"管理后台-财务统计-用户积分统计"})
    void userPointExcel(HttpServletResponse response,
                        @RequestParam(value = "types",required = false) Collection<Integer> types,
                        @RequestParam(value = "name",required = false) String name,
                        @RequestParam(value = "phone",required = false) String phone,
                        @RequestParam(value = "beginTime",required = false) LocalDateTime beginTime,
                        @RequestParam(value = "endTime",required = false) LocalDateTime endTime){
        List<UserPointExcel> exportList =userPointMapper.userPointExcel(types,name,phone,beginTime,endTime);
        exportList.forEach(x->{
            if (x.getType()==12||x.getType()==17){
                x.setVariablePointStr("+"+x.getVariablePoint()+"绿电分");
            }else {
                x.setVariablePointStr("-"+x.getVariablePoint()+"绿电分");
            }
        });
        ExcelUtil<UserPointExcel> util = new ExcelUtil<UserPointExcel>(UserPointExcel.class);
        util.exportExcel(response, exportList, "店铺余额列表数据");
    }
 
    @PostMapping("/save")
    R save(@RequestBody UserPoint userPoint){
        userPointService.save(userPoint);
        return R.ok();
    }
 
}