医院废弃物信息化管理平台
“mitao”
2025-12-24 3763145f34d7706923d55edc32f5cfbc8bc08b92
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
package cn.besteam.modules.medical.service.impl;
 
import cn.besteam.common.constant.CommonConstant;
import cn.besteam.modules.medical.dto.MwDeviceQueryDTO;
import cn.besteam.modules.medical.entity.MwDevice;
import cn.besteam.modules.medical.mapper.MwDeviceMapper;
import cn.besteam.modules.medical.service.IMwDeviceService;
import cn.besteam.modules.system.entity.SysDepartment;
import cn.besteam.modules.system.service.ISysDepartmentService;
import cn.besteam.modules.system.service.ISysDictItemService;
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.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * @Description: 设备管理服务实现
 * @author: besteam
 * @date: 2025-08-17
 */
@Service
public class MwDeviceServiceImpl extends ServiceImpl<MwDeviceMapper, MwDevice> implements IMwDeviceService {
 
    @Autowired
    private ISysDepartmentService sysDepartmentService;
 
    @Autowired
    private ISysDictItemService sysDictItemService;
 
    @Override
    public IPage<MwDevice> selectPageList(Page<MwDevice> page, MwDeviceQueryDTO queryDTO) {
        List<Integer> hospitalIdList = sysDepartmentService.getSearchHospitalIdList(queryDTO.getHospitalId());
        if (CollectionUtils.isEmpty(hospitalIdList)) {
            return page;
        }
        LambdaQueryWrapper<MwDevice> queryWrapper = new LambdaQueryWrapper<>();
        try {
            MybatisParameterUtils.cutInParameter(queryWrapper,MwDevice::getHospitalId, hospitalIdList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        queryWrapper.eq(MwDevice::getDelFlag, CommonConstant.DEL_FLAG_0);
 
        if (queryDTO.getDeviceName() != null && !queryDTO.getDeviceName().isEmpty()) {
            queryWrapper.like(MwDevice::getDeviceName, queryDTO.getDeviceName());
        }
        if (queryDTO.getDeviceCode() != null && !queryDTO.getDeviceCode().isEmpty()) {
            queryWrapper.like(MwDevice::getDeviceCode, queryDTO.getDeviceCode());
        }
        if (queryDTO.getDeviceType() != null) {
            queryWrapper.eq(MwDevice::getDeviceType, queryDTO.getDeviceType());
        }
        if (queryDTO.getDeviceStatus() != null) {
            queryWrapper.eq(MwDevice::getDeviceStatus, queryDTO.getDeviceStatus());
        }
        
        queryWrapper.orderByDesc(MwDevice::getCreateTime);
        
        IPage<MwDevice> pageList = this.page(page, queryWrapper);
        
        // 填充医院名称和设备类型名称
        for (MwDevice device : pageList.getRecords()) {
            fillDeviceNames(device);
        }
        
        return pageList;
    }
 
    @Override
    public MwDevice selectById(Integer id) {
        MwDevice device = this.getById(id);
        if (device != null && device.getDelFlag() == CommonConstant.DEL_FLAG_0) {
            fillDeviceNames(device);
            return device;
        }
        return null;
    }
 
    @Override
    public boolean saveDevice(MwDevice mwDevice) {
        // 设置医院名称
        if (mwDevice.getHospitalId() != null) {
            SysDepartment hospital = sysDepartmentService.getById(mwDevice.getHospitalId());
            if (hospital != null) {
                mwDevice.setHospitalName(hospital.getDepartName());
            }
        }
        
        // 设置默认状态为启用
        if (mwDevice.getDeviceStatus() == null) {
            mwDevice.setDeviceStatus(1);
        }
        
        return this.save(mwDevice);
    }
 
    @Override
    public boolean updateDevice(MwDevice mwDevice) {
        MwDevice existingDevice = this.getById(mwDevice.getId());
        if (existingDevice == null) {
            return false;
        }
        
        // 如果医院ID发生变化,更新医院名称
        if (mwDevice.getHospitalId() != null && !mwDevice.getHospitalId().equals(existingDevice.getHospitalId())) {
            SysDepartment hospital = sysDepartmentService.getById(mwDevice.getHospitalId());
            if (hospital != null) {
                mwDevice.setHospitalName(hospital.getDepartName());
            }
        }
        
        return this.updateById(mwDevice);
    }
 
    @Override
    public boolean deleteDevice(Integer id) {
        return this.removeById(id);
    }
 
    /**
     * 填充设备名称信息
     * @param device 设备信息
     */
    private void fillDeviceNames(MwDevice device) {
        // 填充医院名称
        if (device.getHospitalId() != null && device.getHospitalName() == null) {
            SysDepartment hospital = sysDepartmentService.getById(device.getHospitalId());
            if (hospital != null) {
                device.setHospitalName(hospital.getDepartName());
            }
        }
        
        // 填充设备类型名称
        if (device.getDeviceType() != null) {
            try {
                String dictItemName = sysDictItemService.queryDictItemNameById(device.getDeviceType());
                device.setDeviceTypeName(dictItemName);
            } catch (Exception e) {
                // 如果查询失败,忽略异常
                device.setDeviceTypeName("");
            }
        }
    }
}