医院废弃物信息化管理平台
“mitao”
2026-01-14 ce8e86e7045a6eadc2f06d3cd1af90d22b04fdd0
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
package cn.besteam.modules.medical.controller;
 
import cn.besteam.common.api.vo.Result;
import cn.besteam.common.aspect.annotation.AutoLog;
import cn.besteam.common.util.oConvertUtils;
import cn.besteam.common.util.UserContextUtil;
import cn.besteam.modules.medical.dto.MwMaterialDTO;
import cn.besteam.modules.medical.dto.MwMaterialQueryDTO;
import cn.besteam.modules.medical.entity.MwMaterial;
import cn.besteam.modules.medical.entity.MwMaterialCategory;
import cn.besteam.modules.medical.service.IMwMaterialCategoryService;
import cn.besteam.modules.medical.service.IMwMaterialService;
import cn.besteam.modules.shiro.authc.util.EnvUtils;
import cn.besteam.modules.system.entity.SysDepartment;
import cn.besteam.modules.system.service.ISysDepartmentService;
import cn.besteam.modules.system.util.MybatisParameterUtils;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Description: 资料管理控制器
 * @author: besteam
 * @date: 2025-08-17
 */
@RestController
@RequestMapping("/medical/mwMaterial")
@Slf4j
@Api(tags = "【0729迭代】资料管理")
public class MwMaterialController {
 
    @Autowired
    private IMwMaterialService mwMaterialService;
 
    @Autowired
    private IMwMaterialCategoryService mwMaterialCategoryService;
 
    @Autowired
    private ISysDepartmentService sysDepartmentService;
 
    @Autowired
    private UserContextUtil userContextUtil;
 
    /**
     * 分页列表查询
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取资料列表", notes = "获取资料列表")
    public Result<IPage<MwMaterial>> queryPageList(MwMaterialQueryDTO queryDTO) {
        
        Page<MwMaterial> page = new Page<>(queryDTO.getPageNo(), queryDTO.getPageSize());
        IPage<MwMaterial> pageList = mwMaterialService.selectPageList(page, queryDTO);
        
        return Result.ok(pageList);
    }
 
    /**
     * 添加资料
     * @param materialDTO 资料信息
     * @return 操作结果
     */
    @PostMapping(value = "/add")
    @AutoLog(value = "添加资料")
    @ApiOperation(value = "添加资料", notes = "添加资料", produces = "application/json")
    public Result<String> add(@RequestBody MwMaterialDTO materialDTO) {
        // 验证资料名称是否重复
        if (checkMaterialNameExists(materialDTO.getMaterialName(), materialDTO.getHospitalId(), null)) {
            return Result.error("资料名称已存在!");
        }
        
        MwMaterial material = new MwMaterial();
        BeanUtils.copyProperties(materialDTO, material);
        
        boolean success = mwMaterialService.saveMaterial(material);
        if (success) {
            return Result.ok("添加成功!");
        } else {
            return Result.error("添加失败!");
        }
    }
 
    /**
     * 编辑资料
     * @param materialDTO 资料信息
     * @return 操作结果
     */
    @PostMapping(value = "/edit")
    @AutoLog(value = "编辑资料")
    @ApiOperation(value = "编辑资料", notes = "编辑资料", produces = "application/json")
    public Result<String> edit(@RequestBody MwMaterialDTO materialDTO) {
        if (materialDTO.getId() == null) {
            return Result.error("ID不能为空!");
        }
        
        // 验证资料名称是否重复
        if (checkMaterialNameExists(materialDTO.getMaterialName(), materialDTO.getHospitalId(), materialDTO.getId())) {
            return Result.error("资料名称已存在!");
        }
        
        MwMaterial material = new MwMaterial();
        BeanUtils.copyProperties(materialDTO, material);
        
        boolean success = mwMaterialService.updateMaterial(material);
        if (success) {
            return Result.ok("修改成功!");
        } else {
            return Result.error("修改失败!");
        }
    }
 
    /**
     * 通过id删除
     * @param id 资料ID
     * @return 操作结果
     */
    @DeleteMapping(value = "/delete")
    @AutoLog(value = "删除资料")
    @ApiOperation(value = "删除资料", notes = "删除资料", produces = "application/json")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        // 验证资料是否存在
        MwMaterial material = mwMaterialService.getById(Integer.parseInt(id));
        if (material == null) {
            return Result.error("资料不存在");
        }
        
        boolean success = mwMaterialService.deleteMaterial(Integer.parseInt(id));
        if (success) {
            return Result.ok("删除成功!");
        } else {
            return Result.error("删除失败!");
        }
    }
 
    /**
     * 批量删除
     * @param ids 资料ID数组
     * @return 操作结果
     */
    @DeleteMapping(value = "/deleteBatch")
    @AutoLog(value = "批量删除资料")
    @ApiOperation(value = "批量删除资料", notes = "批量删除资料", produces = "application/json")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        if (oConvertUtils.isEmpty(ids)) {
            return Result.error("参数不识别!");
        }
        
        String[] idArray = ids.split(",");
        
        for (String id : idArray) {
            mwMaterialService.deleteMaterial(Integer.parseInt(id));
        }
        
        return Result.ok("批量删除成功!");
    }
 
    /**
     * 通过id查询
     * @param id 资料ID
     * @return 资料详情
     */
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "通过ID查询资料", notes = "通过ID查询资料", produces = "application/json")
    public Result<MwMaterial> queryById(@RequestParam(name = "id", required = true) String id) {
        MwMaterial material = mwMaterialService.selectById(Integer.parseInt(id));
        if (material != null) {
            return Result.ok(material);
        } else {
            return Result.error("未找到对应实体");
        }
    }
 
    /**
     * 更新资料状态
     * @param id 资料ID
     * @param status 状态
     * @return 操作结果
     */
    @PostMapping(value = "/updateStatus")
    @AutoLog(value = "更新资料状态")
    @ApiOperation(value = "更新资料状态", notes = "更新资料状态", produces = "application/json")
    public Result<String> updateStatus(
            @RequestParam(name = "id", required = true) Integer id,
            @RequestParam(name = "status", required = true) Integer status) {
        
        MwMaterial material = mwMaterialService.getById(id);
        if (material == null) {
            return Result.error("未找到对应实体");
        }
        
        material.setStatus(status);
        material.setUpdateBy(EnvUtils.getUser().getName());
        boolean success = mwMaterialService.updateById(material);
        if (success) {
            return Result.ok("状态更新成功!");
        } else {
            return Result.error("状态更新失败!");
        }
    }
 
    /**
     * 获取医院列表
     * @return 医院列表
     */
    @GetMapping(value = "/getHospitals")
    @ApiOperation(value = "获取医院列表", notes = "获取医院列表", produces = "application/json")
    public Result<List<SysDepartment>> getHospitals() {
        QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_type", 2); // 2表示单位(医院)
        queryWrapper.eq("del_flag", 0);
        queryWrapper.orderByAsc("depart_order");
        
        List<SysDepartment> hospitals = sysDepartmentService.list(queryWrapper);
        return Result.ok(hospitals);
    }
 
    /**
     * 获取资料分类列表
     * @return 资料分类列表
     */
    @GetMapping(value = "/getMaterialCategories")
    @ApiOperation(value = "获取资料分类列表", notes = "获取资料分类列表", produces = "application/json")
    public Result<List<MwMaterialCategory>> getMaterialCategories() {
        LambdaQueryWrapper<MwMaterialCategory> queryWrapper = new LambdaQueryWrapper<>();
 
        SysDepartment currentUserDepartment = userContextUtil.getCurrentUserDepartment();
        if (currentUserDepartment.getOrgType().equals(1)) {
            List<SysDepartment> sysDepartments = sysDepartmentService.listMyHospital(userContextUtil.getCurrentUser().getId());
            List<Integer> hospitalIds = sysDepartments.stream().map(SysDepartment::getId).collect(Collectors.toList());
            try {
                MybatisParameterUtils.cutInParameter(queryWrapper, MwMaterialCategory::getHospitalId, hospitalIds);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            Integer currentUserHospitalId = userContextUtil.getCurrentUserHospitalId();
            queryWrapper.eq(MwMaterialCategory::getHospitalId, currentUserHospitalId);
        }
        queryWrapper.eq(MwMaterialCategory::getDelFlag, 0);
        queryWrapper.orderByAsc(MwMaterialCategory::getCreateTime);
        
        List<MwMaterialCategory> categories = mwMaterialCategoryService.list(queryWrapper);
        return Result.ok(categories);
    }
 
    /**
     * 检查资料名称是否重复
     * @param materialName 资料名称
     * @param hospitalId 医院ID
     * @param id 资料ID(编辑时传入)
     * @return 是否重复
     */
    @GetMapping(value = "/checkMaterialName")
    @ApiOperation(value = "检查资料名称是否重复", notes = "检查资料名称是否重复", produces = "application/json")
    public Result<Boolean> checkMaterialName(
            @RequestParam(name = "materialName", required = true) String materialName,
            @RequestParam(name = "hospitalId", required = true) Integer hospitalId,
            @RequestParam(name = "id", required = false) Integer id) {
        
        boolean exists = checkMaterialNameExists(materialName, hospitalId, id);
        return Result.ok(!exists);
    }
 
    /**
     * 检查资料名称是否存在
     * @param materialName 资料名称
     * @param hospitalId 医院ID
     * @param id 资料ID(编辑时传入)
     * @return 是否存在
     */
    private boolean checkMaterialNameExists(String materialName, Integer hospitalId, Integer id) {
        QueryWrapper<MwMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_name", materialName);
        queryWrapper.eq("hospital_id", hospitalId);
        queryWrapper.eq("del_flag", 0);
        
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        
        int count = mwMaterialService.count(queryWrapper);
        return count > 0;
    }
}