xuhy
2026-01-19 cb6975effb2165769b92a3204533f30ff4a6cd97
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
package com.ruoyi.web.controller.api;
 
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.model.TCrmClinic;
import com.ruoyi.system.model.TSysAppUser;
import com.ruoyi.system.model.TSysInspection;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TCrmClinicService;
import com.ruoyi.system.service.TSysAppUserService;
import com.ruoyi.system.service.TSysInspectionService;
import com.ruoyi.web.controller.tool.WordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>
 * 用户检测信息 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Api(tags = "用户检测信息管理")
@RestController
@RequestMapping("/t-sys-inspection")
public class TSysInspectionController {
 
    private final TSysInspectionService sysInspectionService;
    private final TokenService tokenService;
    private final TCrmClinicService crmClinicService;
    private final ISysUserService sysUserService;
    private final TSysAppUserService sysAppUserService;
    private final WordUtil wordUtil;
    @Autowired
    public TSysInspectionController(TSysInspectionService sysInspectionService, TokenService tokenService, TCrmClinicService crmClinicService, ISysUserService sysUserService, TSysAppUserService sysAppUserService, WordUtil wordUtil) {
        this.sysInspectionService = sysInspectionService;
        this.tokenService = tokenService;
        this.crmClinicService = crmClinicService;
        this.sysUserService = sysUserService;
        this.sysAppUserService = sysAppUserService;
        this.wordUtil = wordUtil;
    }
 
//    /**
//     * 获取用户检测信息管理管理列表
//     */
//    @ApiOperation(value = "获取用户检测信息管理分页列表")
//    @PostMapping(value = "/pageList")
//    public R<PageInfo<TSysInspection>> pageList(@RequestBody TSysInspectionQuery query) {
//        return R.ok(sysInspectionService.pageList(query));
//    }
 
    /**
     * 获取用户检测信息管理管理列表
     */
    @ApiOperation(value = "获取用户检测信息管理列表")
    @PostMapping(value = "/list")
    public R<List<TSysInspection>> list() {
        return R.ok(sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).orderByDesc(TSysInspection::getCreateTime)));
    }
 
    /**
     * 添加用户检测信息管理管理
     */
    @Log(title = "用户检测信息管理信息-新增用户检测信息管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加用户检测信息管理")
    @PostMapping(value = "/add")
    public R<String> add(@Validated @RequestBody TSysInspection dto) {
        dto.setIsPay(1);
        String checkTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        dto.setCheckTime(checkTime);
        Long userId = tokenService.getLoginUser().getUserId();
        TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                .eq(TCrmClinic::getUserId, userId)
                .last("LIMIT 1"));
        if(Objects.isNull(crmClinic)){
            SysUser sysUser = sysUserService.selectUserById(userId);
            crmClinic = crmClinicService.getById(sysUser.getObjectId());
        }
        dto.setClinicId(crmClinic.getId());
        // 查询该诊所最新的一条检测记录
        TSysInspection sysInspection = sysInspectionService.getOne(Wrappers.lambdaQuery(TSysInspection.class)
                .eq(TSysInspection::getClinicId, crmClinic.getId())
                .orderByDesc(TSysInspection::getCreateTime)
                .last("LIMIT 1"));
        if(StringUtils.hasLength(sysInspection.getPrescriptionNumber())){
            // 生成处方编号 总共六位数字,不满6位前面补0
            String prescriptionNumber = sysInspection.getPrescriptionNumber();
            prescriptionNumber = prescriptionNumber.substring(prescriptionNumber.length() - 6);
            prescriptionNumber = String.format("%06d", Integer.parseInt(prescriptionNumber) + 1);
            dto.setPrescriptionNumber(prescriptionNumber);
            dto.setHospitalizationNumber(prescriptionNumber);
        }else{
            dto.setPrescriptionNumber("000001");
            dto.setHospitalizationNumber("000001");
        }
        dto.setInspectionType(1);
        sysInspectionService.save(dto);
        return R.ok(dto.getId());
    }
 
    /**
     * 修改用户检测信息管理
     */
    @Log(title = "用户检测信息管理信息-修改用户检测信息管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户检测信息管理")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TSysInspection dto) {
        return R.ok(sysInspectionService.updateById(dto));
    }
 
    /**
     * 查看用户检测信息管理详情
     */
    @ApiOperation(value = "查看用户检测信息管理详情")
    @GetMapping(value = "/getDetailById")
    public R<TSysInspection> getDetailById(@RequestParam String id) {
        return R.ok(sysInspectionService.getById(id));
    }
 
    /**
     * 删除用户检测信息管理
     */
    @Log(title = "用户检测信息管理信息-删除用户检测信息管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户检测信息管理")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(sysInspectionService.removeById(id));
    }
 
    /**
     * 批量删除用户检测信息管理
     */
    @Log(title = "用户检测信息管理信息-删除用户检测信息管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除用户检测信息管理")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        return R.ok(sysInspectionService.removeByIds(ids));
    }
 
    @Log(title = "用户检测信息管理信息-导出检测信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "导出检测信息")
    @GetMapping(value = "/exportInspection")
    public R<String> exportInspection(@RequestParam String id) {
        TSysInspection sysInspection = sysInspectionService.getById(id);
        if(StringUtils.hasLength(sysInspection.getDetectionPdf())){
            return R.ok(sysInspection.getDetectionPdf());
        }
        // 查询诊所信息
        TCrmClinic crmClinic = crmClinicService.getById(sysInspection.getClinicId());
        if(Objects.isNull(crmClinic)){
            return R.fail("诊所信息不存在");
        }
        Map<String, Object> templateParam = new HashMap<>();
        fill(templateParam, "clinicName", crmClinic.getClinicName());
        // 住院次数
        if(StringUtils.hasLength(sysInspection.getHospitalizationCount())){
            fill(templateParam, "hospitalizationCount", sysInspection.getHospitalizationCount());
        }
        // 住院编号
        if(StringUtils.hasLength(sysInspection.getHospitalizationNumber())){
            fill(templateParam, "hospitalizationNumber", sysInspection.getHospitalizationNumber());
        }
        // 姓名
        if(StringUtils.hasLength(sysInspection.getNickName())){
            fill(templateParam, "nickName", sysInspection.getNickName());
        }
        // 出生地
        if(StringUtils.hasLength(sysInspection.getBirthplace())){
            fill(templateParam, "birthplace", sysInspection.getBirthplace());
        }
        // 性别
        if(Objects.nonNull(sysInspection.getSex())){
            if(sysInspection.getSex() == 1){
                fill(templateParam, "sex", "男");
            }else{
                fill(templateParam, "sex", "女");
            }
        }
        // 民族
        if(StringUtils.hasLength(sysInspection.getNation())){
            fill(templateParam, "nation", sysInspection.getNation());
        }
        // 年龄
        if(Objects.nonNull(sysInspection.getAge())){
            fill(templateParam, "age", sysInspection.getAge());
        }
        // 入院日期
        if(StringUtils.hasLength(sysInspection.getAdmissionDate())){
            fill(templateParam, "admissionDate", sysInspection.getAdmissionDate());
        }
        // 婚姻状况
        if(Objects.nonNull(sysInspection.getMaritalStatus())){
            fill(templateParam, "maritalStatus", sysInspection.getMaritalStatus());
        }
        // 记录日期
        if(StringUtils.hasLength(sysInspection.getRecordDate())){
            fill(templateParam, "recordDate", sysInspection.getRecordDate());
        }
        // 职业
        if(StringUtils.hasLength(sysInspection.getOccupation())){
            fill(templateParam, "occupation", sysInspection.getOccupation());
        }
        // 病史陈述者
        if(StringUtils.hasLength(sysInspection.getHistoryNarrator())){
            fill(templateParam, "historyNarrator", sysInspection.getHistoryNarrator());
        }
        // 主诉
        if(StringUtils.hasLength(sysInspection.getChiefComplaint())){
            fill(templateParam, "chiefComplaint", sysInspection.getChiefComplaint());
        }
        // 现病史
        if(StringUtils.hasLength(sysInspection.getPresentHistory())){
            fill(templateParam, "presentHistory", sysInspection.getPresentHistory());
        }
        // 既往史
        if(StringUtils.hasLength(sysInspection.getPastMedicalHistory())){
            fill(templateParam, "pastMedicalHistory", sysInspection.getPastMedicalHistory());
        }
        // 个人史
        if(StringUtils.hasLength(sysInspection.getPersonalHistory())){
            fill(templateParam, "personalHistory", sysInspection.getPersonalHistory());
        }
        // 月经史
        if(StringUtils.hasLength(sysInspection.getMenstrualHistory())){
            fill(templateParam, "menstrualHistory", sysInspection.getMenstrualHistory());
        }
        // 婚育史
        if(StringUtils.hasLength(sysInspection.getObstetricHistory())){
            fill(templateParam, "obstetricHistory", sysInspection.getObstetricHistory());
        }
        // 家族史
        if(StringUtils.hasLength(sysInspection.getFamilyHistory())){
            fill(templateParam, "familyHistory", sysInspection.getFamilyHistory());
        }
        // 体温
        if(StringUtils.hasLength(sysInspection.getBodyTemperature())){
            fill(templateParam, "bodyTemperature", sysInspection.getBodyTemperature());
        }
        // 脉搏
        if(StringUtils.hasLength(sysInspection.getPulse())){
            fill(templateParam, "pulse", sysInspection.getPulse());
        }
        // 呼吸
        if(StringUtils.hasLength(sysInspection.getBreathe())){
            fill(templateParam, "breathe", sysInspection.getBreathe());
        }
        // 血压
        if(StringUtils.hasLength(sysInspection.getBloodPressure())){
            fill(templateParam, "bloodPressure", sysInspection.getBloodPressure());
        }
        // 一般情况
        if(StringUtils.hasLength(sysInspection.getGeneralSituation())){
            fill(templateParam, "generalSituation", sysInspection.getGeneralSituation());
        }
 
        String url = wordUtil.generatePdf(
                "/usr/local/project/template/",
                "病历-样例.docx",
                templateParam,
                "病历-模板",
                "/usr/local/project/template/");
        // windows
//        String url = wordUtil.generatePdf(
//                "E:\\WXWork\\1688857451467088\\Cache\\File\\2026-01\\",
//                "病历-样例.docx",
//                templateParam,
//                "病历-模板",
//                "E:\\WXWork\\1688857451467088\\Cache\\File\\2026-01\\");
        sysInspection.setDetectionPdf(url);
        sysInspectionService.updateById(sysInspection);
        return R.ok(url);
    }
 
    @Log(title = "用户检测信息管理信息-导出处方信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "导出处方信息")
    @GetMapping(value = "/exportPrescription")
    public R<String> exportPrescription(@RequestParam String id) {
        TSysInspection sysInspection = sysInspectionService.getById(id);
        if(StringUtils.hasLength(sysInspection.getPrescriptionPdf())){
            return R.ok(sysInspection.getPrescriptionPdf());
        }
        // 查询诊所信息
        TCrmClinic crmClinic = crmClinicService.getById(sysInspection.getClinicId());
        if(Objects.isNull(crmClinic)){
            return R.fail("诊所信息不存在");
        }
        Map<String, Object> templateParam = new HashMap<>();
        fill(templateParam, "clinicName", crmClinic.getClinicName());
        // 住院编号
        if(StringUtils.hasLength(sysInspection.getPrescriptionNumber())){
            fill(templateParam, "prescriptionNumber", sysInspection.getPrescriptionNumber());
        }
        // 科室
        if(StringUtils.hasLength(sysInspection.getDepartmentName())){
            fill(templateParam, "departmentName", sysInspection.getDepartmentName());
        }
        // 处方时间
        if(StringUtils.hasLength(sysInspection.getPrescriptionTime())){
            fill(templateParam, "prescriptionTime", sysInspection.getPrescriptionTime());
        }
        // 姓名
        if(StringUtils.hasLength(sysInspection.getNickName())){
            fill(templateParam, "nickName", sysInspection.getNickName());
        }
        // 性别
        if(Objects.nonNull(sysInspection.getSex())){
            if(sysInspection.getSex() == 1){
                fill(templateParam, "sex", "男");
            }else{
                fill(templateParam, "sex", "女");
            }
        }
        // 年龄
        if(Objects.nonNull(sysInspection.getAge())){
            fill(templateParam, "age", sysInspection.getAge());
        }
        // 门诊病历号
        if(StringUtils.hasLength(sysInspection.getId())){
            fill(templateParam, "id", sysInspection.getId());
        }
        // 单位或家庭住址
        if(Objects.nonNull(sysInspection.getHomeAddress())){
            fill(templateParam, "homeAddress", sysInspection.getHomeAddress());
        }
        // 临床诊断及证型
        if(StringUtils.hasLength(sysInspection.getClinicalDiagnosis())){
            fill(templateParam, "clinicalDiagnosis", sysInspection.getClinicalDiagnosis());
        }
        // 处方内容
        if(StringUtils.hasLength(sysInspection.getPrescriptionContent())){
            fill(templateParam, "prescriptionContent", sysInspection.getPrescriptionContent());
        }
        // 备注
        if(StringUtils.hasLength(sysInspection.getRemarks())){
            fill(templateParam, "remarks", sysInspection.getRemarks());
        }
 
        String url = wordUtil.generatePdf(
                "/usr/local/project/template/",
                "处方单-样例.docx",
                templateParam,
                "处方单-模板",
                "/usr/local/project/template/");
        // windows
//        String url = wordUtil.generatePdf(
//                "E:\\WXWork\\1688857451467088\\Cache\\File\\2026-01\\",
//                "处方单-样例.docx",
//                templateParam,
//                "处方单-模板",
//                "E:\\WXWork\\1688857451467088\\Cache\\File\\2026-01\\");
        sysInspection.setPrescriptionPdf(url);
        sysInspectionService.updateById(sysInspection);
        return R.ok(url);
    }
 
    private void fill(Map<String, Object> templateParam, String key, Object value) {
        if (com.ruoyi.common.utils.StringUtils.isEmpty(key)){
            throw new RuntimeException("key不能为空");
        }
        templateParam.put("${"+key+"}", value != null ? value : "");
    }
 
}