huliguo
昨天 d15bb55822001421572bfee603b3d503cd63e07d
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
package com.ruoyi.web.controller.errand;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.errand.domain.Order;
import com.ruoyi.errand.object.dto.app.AppletLogin;
import com.ruoyi.errand.object.dto.app.BirthDayDTO;
import com.ruoyi.errand.object.dto.app.MobileLoginDTO;
import com.ruoyi.errand.object.dto.app.RegisterDTO;
import com.ruoyi.errand.object.dto.sys.AppUserPageListDTO;
import com.ruoyi.errand.object.dto.sys.OrderPageListDTO;
import com.ruoyi.errand.object.dto.sys.UserStatsDTO;
import com.ruoyi.errand.object.vo.app.AppUserInfoVO;
import com.ruoyi.errand.object.vo.app.OrderPageVO;
import com.ruoyi.errand.object.vo.app.UserTopInfoVO;
import com.ruoyi.errand.object.vo.login.LoginVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.errand.object.vo.sys.*;
import com.ruoyi.errand.service.AppUserService;
import com.ruoyi.errand.utils.RefundCallbackResult;
import com.ruoyi.errand.utils.TokenBlacklistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.*;
 
 
@RestController
@RequestMapping(value = "/app/user")
@Api(value = "app用户",tags = "app用户操作控制器")
@Slf4j
public class AppUserController {
 
    @Autowired
    private AppUserService appUserService;
 
    @Autowired
    private TokenBlacklistService blacklistService;
    @GetMapping("/test")
    @ApiOperation(value = "测试接口" ,tags = "测试")
    public R<Void> test(@RequestHeader("Authorization") String token) {
        throw new ServiceException("测试");
 
    }
    /**
     * 登出
     */
    @GetMapping("/logout")
    @ApiOperation(value = "登出" ,tags = "app用户端")
    public R<Void> logout(@RequestHeader("Authorization") String token) {
        // 1. 将令牌加入黑名单
        blacklistService.addToBlacklist(token);
        return R.ok();
    }
 
    /**
     * 获取短信验证码
     */
    @GetMapping("/getSMSCode")
    @ApiOperation(value = "获取短信验证码",tags = "app用户端")
    public R<Void> getSMSCode(@RequestParam("phone") String phone) {
        appUserService.getSMSCode(phone);
        return R.ok();
    }
 
    /**
     * 手机号验证码登录
     */
    @PostMapping("/mobileLogin")
    @ApiOperation(value = "手机号登录",tags = "app用户端")
    public R<LoginVO> mobileLogin(@RequestBody @Valid MobileLoginDTO mobileLogin) {
        return appUserService.mobileLogin(mobileLogin);
 
    }
 
    /**
     * 小程序一键登录
     */
    @PostMapping("/appletLogin")
    @ApiOperation(value = "小程序一键登录",tags = "app用户端")
    public R<LoginVO> appletLogin(@RequestBody @Valid AppletLogin appletLogin) {
        return appUserService.appletLogin(appletLogin);
    }
 
    /**
     * 注册
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册",tags = "app用户端")
    public R<Void> register(@RequestBody @Valid RegisterDTO registerDTO) {
        appUserService.register(registerDTO);
        return R.ok();
    }
    /**
     * 根据小区id(可选择)
     * 获取小区名字 以及对应的收货地址 小区价格
     */
    @GetMapping("/getOrderPage")
    @ApiOperation(value = "获取相关信息",tags = "app用户端-下单页面")
    public R<OrderPageVO> getOrderPage(@RequestParam(value = "communityId",required = false) Integer communityId) {
        return R.ok(appUserService.getOrderPage(communityId));
    }
 
    /**
     * 个人中心
     */
    @GetMapping("/getMyInfo")
    @ApiOperation(value = "获取个人信息",tags = "app用户端-个人信息")
    public R<AppUserInfoVO> getMyInfo() {
        return R.ok(appUserService.getMyInfo());
    }
 
    /**
     * 选择性别 1=男,2=女,3=未知
     */
    @PutMapping("/setSex")
    @ApiOperation(value = "修改性别",tags = "app用户端-个人信息")
    public R<Void> setSex(@RequestParam Integer sex) {
        appUserService.setSex(sex);
        return R.ok();
    }
    /**
     * 修改生日
     */
    @PutMapping("/setBirthDay")
    @ApiOperation(value = "修改生日",tags = "app用户端-个人信息")
    public R<Void> setBirthDay(@RequestBody BirthDayDTO birth) {
        appUserService.setBirthDay(birth);
        return R.ok();
    }
 
    /**
     * 注销账号 需要修改delFlag?
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "注销账号",tags = "app用户端-个人信息")
    public R<Void> delete(@RequestHeader("Authorization") String token) {
        appUserService.delete(token);
        return R.ok();
    }
 
    /**
     * 用户统计
     */
    @GetMapping("/userTopInfo")
    @PreAuthorize("@ss.hasPermi('system:index:statistics')")
    @ApiOperation(value = "用户统计-顶部数据", tags = "系统后台-首页")
    public R<UserTopInfoVO> userTopInfo() {
 
        LocalDateTime[] dateRange;//日期范围
        dateRange = new LocalDateTime[]{
                LocalDateTime.now().with(LocalTime.MIN),
                LocalDateTime.now().with(LocalTime.MAX)
        };
        return R.ok(appUserService.userTopInfo(dateRange[0], dateRange[1]));
    }
 
    /**
     * 用户统计
     */
    @PostMapping("/statistics")
    @PreAuthorize("@ss.hasPermi('system:index:statistics')")
    @ApiOperation(value = "用户统计-折线图", tags = "系统后台-首页")
    public R<UserStatsVO> statistics(@RequestBody @Valid UserStatsDTO userStatsDTO) {
        LocalDateTime[] dateRange;//日期范围
        String datePattern;//日期格式
 
        switch (userStatsDTO.getType()) {
            case 1: // 今日 当天数据,按小时划分
                dateRange = new LocalDateTime[]{
                        LocalDateTime.now().with(LocalTime.MIN),
                        LocalDateTime.now().with(LocalTime.MAX)
                };
                datePattern = "HH时";
                break;
            case 2: // 本周 按星期一、二...划分
                LocalDate now = LocalDate.now();
                dateRange = new LocalDateTime[]{
                        now.with(DayOfWeek.MONDAY).atStartOfDay(), // 本周一
                        now.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX) // 本周日
                };
                datePattern = "EEEE";
                break;
            case 3: // 本月 按1日至月末划分
                YearMonth currentMonth = YearMonth.now();
                dateRange = new LocalDateTime[]{
                        currentMonth.atDay(1).atStartOfDay(), // 本月1日
                        currentMonth.atEndOfMonth().atTime(LocalTime.MAX) // 本月最后一天
                };
                datePattern = "dd日";
                break;
            case 4: // 本季度 按当前季度的月份划分
                YearMonth thisMonth = YearMonth.now();
                YearMonth firstMonthOfQuarter = thisMonth.with(
                        Month.of(((thisMonth.getMonthValue() - 1) / 3) * 3 + 1));
                YearMonth lastMonthOfQuarter = firstMonthOfQuarter.plusMonths(2);
                dateRange = new LocalDateTime[]{
                        firstMonthOfQuarter.atDay(1).atStartOfDay(), // 季度第一个月1日
                        lastMonthOfQuarter.atEndOfMonth().atTime(LocalTime.MAX) // 季度最后一个月最后一天
                };
                datePattern = "MM月";
                break;
            case 5: // 半年 上半年或下半年完整月份
                YearMonth current = YearMonth.now();
                YearMonth halfYearStart = current.getMonthValue() <= 6 ?
                        YearMonth.of(current.getYear(), Month.JANUARY) :
                        YearMonth.of(current.getYear(), Month.JULY);
                YearMonth halfYearEnd = halfYearStart.plusMonths(5);
                dateRange = new LocalDateTime[]{
                        halfYearStart.atDay(1).atStartOfDay(),
                        halfYearEnd.atEndOfMonth().atTime(LocalTime.MAX)
                };
                datePattern = "MM月";
                break;
            case 6: // 本年 按1月至12月完整年份
                int year = Year.now().getValue();
                dateRange = new LocalDateTime[]{
                        LocalDateTime.of(year, 1, 1, 0, 0), // 1月1日
                        LocalDateTime.of(year, 12, 31, 23, 59, 59) // 12月31日
                };
                datePattern = "MM月";
                break;
            case 7: // 自定义 按起始时间到终止时间之间的日期划分
                if (userStatsDTO.getStartDate() == null || userStatsDTO.getEndDate() == null) {
                    throw new ServiceException("自定义时间范围必须指定开始和结束日期");
                }
                if (userStatsDTO.getStartDate().isAfter(userStatsDTO.getEndDate())) {
                    throw new ServiceException("开始日期不能晚于结束日期");
                }
                dateRange = new LocalDateTime[]{
                        userStatsDTO.getStartDate().atStartOfDay(),
                        userStatsDTO.getEndDate().atTime(LocalTime.MAX)
                };
                datePattern = "yyyy-MM-dd";
                break;
            default:
                throw new ServiceException("无效的筛选类型: " + userStatsDTO.getType());
        }
 
        return R.ok(appUserService.getUserStats(dateRange[0], dateRange[1], datePattern));
    }
 
    /**
     * 用户管理列表
     */
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('system:appuser:list')")
    @ApiOperation(value = "用户管理-分页列表", tags = "系统后台-用户管理")
    public R<IPage<AppUserPageListVO>> getAppUserPageList(@RequestBody @Valid AppUserPageListDTO appUserPageListDTO) {
        return R.ok(appUserService.getAppUserPageList(appUserPageListDTO));
    }
    /**
     * 查看详情
     */
    @GetMapping("/detail")
    @PreAuthorize("@ss.hasPermi('system:appuser:list')")
    @ApiOperation(value = "用户管理-用户详情", tags = "系统后台-用户管理")
    public R<AppUserSysDetailVO> detail(@RequestParam("id") Integer id) {
        return R.ok(appUserService.detail(id));
    }
    /**
     * 冻结/解冻
     */
    @PutMapping("/froze")
    @PreAuthorize("@ss.hasPermi('system:appuser:list')")
    @ApiOperation(value = "用户管理-冻结/解冻", tags = "系统后台-用户管理")
    public R froze(@RequestParam("id") Integer id) {
        appUserService.froze(id);
        return R.ok();
    }
    /**
     * 会员退费
     */
    @GetMapping("/refund")
    @PreAuthorize("@ss.hasPermi('system:appuser:list')")
    @ApiOperation(value = "用户管理-会员退费", tags = "系统后台-用户管理")
    public R<Void> refund(@RequestParam("id") Integer id) {
        appUserService.refund(id);
        return R.ok();
    }
 
 
    /**
     * 订单取消支付回退
     *
     * @param refundCallbackResult
     * @param response
     * @return
     */
    @ResponseBody
    @GetMapping("/refundPayMoneyCallback")
    public void refundPayMoneyCallback(RefundCallbackResult refundCallbackResult, HttpServletResponse response) {
        R callback = appUserService.refundPayMoneyCallback(refundCallbackResult);
        if (callback.getCode() == 200) {
            response.setStatus(200);
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            out.println("success");
            out.flush();
            out.close();
        }
    }
 
}