xuhy
2025-10-13 782cef1a49bb665fd88f3850a9193c08d63ef1d5
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
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.system.export.TCrmClinicAuditExport;
import com.ruoyi.system.export.TSysAppUserClinicExport;
import com.ruoyi.system.export.TSysAppUserExport;
import com.ruoyi.system.export.TSysAppUserListExport;
import com.ruoyi.system.model.TSysAppUser;
import com.ruoyi.system.model.TSysChronicDisease;
import com.ruoyi.system.model.TSysInspection;
import com.ruoyi.system.model.TSysMessageTemplate;
import com.ruoyi.system.query.TCrmClinicQuery;
import com.ruoyi.system.query.TSysAppUserQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TSysAppUserVO;
import com.ruoyi.system.vo.TSysOrderVO;
import com.ruoyi.web.controller.tool.MsgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
 
/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/t-sys-app-user")
public class TSysAppUserController {
 
    private final TSysAppUserService sysAppUserService;
    private final TSysInspectionService sysInspectionService;
    private final TSysOrderService sysOrderService;
    private final TSysChronicDiseaseService sysChronicDiseaseService;
    private final MsgUtils msgUtils;
    private final TSysMessageTemplateService sysMessageTemplateService;
    @Autowired
    public TSysAppUserController(TSysAppUserService sysAppUserService, TSysInspectionService sysInspectionService, TSysOrderService sysOrderService, TSysChronicDiseaseService sysChronicDiseaseService, MsgUtils msgUtils, TSysMessageTemplateService sysMessageTemplateService) {
        this.sysAppUserService = sysAppUserService;
        this.sysInspectionService = sysInspectionService;
        this.sysOrderService = sysOrderService;
        this.sysChronicDiseaseService = sysChronicDiseaseService;
        this.msgUtils = msgUtils;
        this.sysMessageTemplateService = sysMessageTemplateService;
    }
 
    /**
     * 获取用户管理管理列表
     */
    @ApiOperation(value = "获取用户管理分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TSysAppUserVO>> pageList(@RequestBody TSysAppUserQuery query) {
        return R.ok(sysAppUserService.pageList(query));
    }
 
    /**
     * 获取用户管理管理列表
     */
    @ApiOperation(value = "获取慢性病患者分页列表")
    @PostMapping(value = "/pageChronicDiseaseUserList")
    public R<PageInfo<TSysAppUserVO>> pageChronicDiseaseUserList(@RequestBody TSysAppUserQuery query) {
        return R.ok(sysAppUserService.pageChronicDiseaseUserList(query));
    }
 
 
    /**
     * 用药提醒
     */
    @ApiOperation(value = "慢性病患者-用药提醒", notes = "userId:患者用户id,templateId:短信模板id")
    @GetMapping(value = "/medicationReminder")
    public R<String> medicationReminder(@RequestParam(value = "userId") String userId,
                                                         @RequestParam(value = "templateId") String templateId) {
        TSysAppUser sysAppUser = sysAppUserService.getById(userId);
        if(StringUtils.isNotBlank(sysAppUser.getPhone())){
            sysAppUser.setLastReminderTime(LocalDateTime.now());
            sysAppUserService.updateById(sysAppUser);
            TSysMessageTemplate messageTemplate = sysMessageTemplateService.getById(templateId);
            // 发送短信
            try {
                Map<String,String> params = new HashMap<>();
                msgUtils.sendMsg(sysAppUser.getPhone(),params, messageTemplate.getTemplateCode());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return R.ok();
    }
 
    /**
     * 获取用户管理管理列表
     */
    @ApiOperation(value = "获取用户管理列表")
    @PostMapping(value = "/list")
    public R<List<TSysAppUser>> list() {
        return R.ok(sysAppUserService.list(Wrappers.lambdaQuery(TSysAppUser.class)
                .eq(TSysAppUser::getStatus,1)
                .orderByDesc(TSysAppUser::getCreateTime)));
    }
 
    /**
     * 添加用户管理管理
     */
    @Log(title = "用户管理信息-新增用户管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加用户管理")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TSysAppUser dto) {
        if (sysAppUserService.isExit(dto)) {
            return R.fail("用户管理信息已存在");
        }
        return R.ok(sysAppUserService.save(dto));
    }
 
    /**
     * 修改用户管理
     */
    @Log(title = "用户管理信息-修改用户管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户管理")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TSysAppUser dto) {
        if (sysAppUserService.isExit(dto)) {
            return R.fail("用户管理信息已存在");
        }
        return R.ok(sysAppUserService.updateById(dto));
    }
 
    /**
     * 查看用户管理详情
     */
    @ApiOperation(value = "查看用户管理详情")
    @GetMapping(value = "/getDetailById")
    public R<TSysAppUserVO> getDetailById(@RequestParam String id) {
        TSysAppUser sysAppUser = sysAppUserService.getById(id);
        TSysAppUserVO sysAppUserVO = new TSysAppUserVO();
        BeanUtils.copyProperties(sysAppUser, sysAppUserVO);
        // 查询慢性病
        String chronicDiseaseId = sysAppUserVO.getChronicDiseaseId();
        if(StringUtils.isNotBlank(chronicDiseaseId)){
            String[] split = chronicDiseaseId.split(",");
            List<TSysChronicDisease> sysChronicDiseases = sysChronicDiseaseService.list(Wrappers.lambdaQuery(TSysChronicDisease.class).in(TSysChronicDisease::getId, Arrays.asList(split)));
            sysAppUserVO.setSysChronicDiseases(sysChronicDiseases);
        }
        // 查询检测信息
        List<TSysInspection> sysInspections = sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).eq(TSysInspection::getAppUserId, id));
        sysAppUserVO.setSysInspections(sysInspections);
        // 订单信息
        List<TSysOrderVO> sysOrders = sysOrderService.queryListByAppUserId(id);
        sysAppUserVO.setSysOrders(sysOrders);
        if(sysAppUserVO.getBirthTime() != null){
            long age = ChronoUnit.YEARS.between(sysAppUserVO.getBirthTime(), LocalDateTime.now());
            sysAppUserVO.setAge(age);
        }
        return R.ok(sysAppUserVO);
    }
 
    /**
     * 根据用户id查询检测信息
     */
    @ApiOperation(value = "根据用户id查询检测信息")
    @GetMapping(value = "/getInspectionByAppUserId")
    public R<List<TSysInspection>> getInspectionByAppUserId(@RequestParam String appUserId) {
        // 订单信息
        List<TSysInspection> sysInspections = sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).eq(TSysInspection::getAppUserId, appUserId));
        return R.ok(sysInspections);
    }
 
    /**
     * 查看用户管理详情
     */
    @ApiOperation(value = "查看用户购药详情")
    @GetMapping(value = "/getOrderByAppUserId")
    public R<List<TSysOrderVO>> getOrderByAppUserId(@RequestParam String appUserId) {
        List<TSysOrderVO> sysOrders = sysOrderService.queryListByAppUserId(appUserId);
        return R.ok(sysOrders);
    }
 
    /**
     * 删除用户管理
     */
    @Log(title = "用户管理信息-删除用户管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户管理")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(sysAppUserService.removeById(id));
    }
 
    /**
     * 批量删除用户管理
     */
    @Log(title = "用户管理信息-删除用户管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除用户管理")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        return R.ok(sysAppUserService.removeByIds(ids));
    }
 
    /**
     * 分公司管理解冻冻结
     */
    @Log(title = "用户管理信息-用户管理解冻冻结", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "用户管理解冻冻结",notes = "状态 1=使用中 2=冻结")
    @PutMapping(value = "/thawOrFreeze")
    public R<Boolean> thawOrFreeze(@RequestParam(value = "id")String id,
                                   @RequestParam(value = "status")Integer status) {
        TSysAppUser sysAppUser = sysAppUserService.getById(id);
        sysAppUser.setStatus(status);
        sysAppUserService.updateById(sysAppUser);
        if(status == 2){
            // TODO 退出账号 冻结
 
        }
        return R.ok();
    }
 
    /**
     * 修改用户慢性病
     */
    @Log(title = "用户管理信息-修改用户慢性病", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户慢性病")
    @PostMapping(value = "/updateDisease")
    public R<Boolean> updateDisease(@RequestParam(value = "id") String id,
                                    @RequestParam(value = "chronicDiseaseId") String chronicDiseaseId) {
        TSysAppUser sysAppUser = new TSysAppUser();
        sysAppUser.setId(id);
        sysAppUser.setChronicDiseaseId(chronicDiseaseId);
        return R.ok(sysAppUserService.updateById(sysAppUser));
    }
 
 
    @Log(title = "用户管理信息-慢性病患者导出-平台", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "慢性病患者导出-平台")
    @PostMapping("/exportList")
    public void exportList(@RequestBody TSysAppUserQuery query){
        List<TSysAppUserListExport> sysAppUserListExports = sysAppUserService.exportList(query);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysAppUserListExport.class, sysAppUserListExports);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        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();
            }
        }
    }
    @Log(title = "用户管理信息-慢性病患者导出-诊所", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "慢性病患者导出-诊所")
    @PostMapping("/exportListClinic")
    public void exportListClinic(@RequestBody TSysAppUserQuery query){
        List<TSysAppUserClinicExport> sysAppUserClinicExports = sysAppUserService.exportListClinic(query);
        for (TSysAppUserClinicExport sysAppUserClinicExport : sysAppUserClinicExports) {
            String lastReminderTime = sysAppUserClinicExport.getLastReminderTime();
            lastReminderTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(lastReminderTime);
            sysAppUserClinicExport.setLastReminderTime(lastReminderTime);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysAppUserClinicExport.class, sysAppUserClinicExports);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        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();
            }
        }
    }
 
    @Log(title = "用户管理信息-用户管理信息导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "用户管理信息导出")
    @PostMapping("/exportListAppUser")
    public void exportListAppUser(@RequestBody TSysAppUserQuery query){
        List<TSysAppUserExport> sysAppUserExports = sysAppUserService.exportListAppUser(query);
        for (TSysAppUserExport sysAppUserExport : sysAppUserExports) {
            String lastInspectionTime = sysAppUserExport.getLastInspectionTime();
            if(StringUtils.isNotEmpty(lastInspectionTime)){
                lastInspectionTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(lastInspectionTime);
                sysAppUserExport.setLastInspectionTime(lastInspectionTime);
            }
            sysAppUserExport.setCreateTimeStr(sysAppUserExport.getCreateTime().toLocalDate().toString());
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysAppUserExport.class, sysAppUserExports);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        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();
            }
        }
    }
 
}