地质所 沉降监测网建设项目
zmk
2024-05-17 5e697a70c7e49e18bc3bc2f75c2e9ad5bda17489
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
package com.javaweb.geo.service.impl;
 
import java.io.File;
import java.util.List;
 
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.javaweb.common.config.Global;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.IdGenerate;
import com.javaweb.geo.domain.Device;
import com.javaweb.geo.domain.Project;
import com.javaweb.geo.enums.RecordType;
import com.javaweb.geo.service.IDeviceService;
import com.javaweb.geo.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javaweb.geo.mapper.DeviceLogMapper;
import com.javaweb.geo.domain.DeviceLog;
import com.javaweb.geo.service.IDeviceLogService;
import com.javaweb.common.core.text.Convert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.genid.GenIdUtil;
 
/**
 * 设备出入库记录Service业务层处理
 *
 * @author cxy
 * @date 2024-05-16
 */
@Service
public class DeviceLogServiceImpl implements IDeviceLogService {
 
    @Autowired
    private DeviceLogMapper deviceLogMapper;
 
    @Autowired
    private IProjectService projectService;
 
    @Autowired
    private IDeviceService deviceService;
 
    private String fileSaveDir = Global.getProfile() + "\\template\\" ;
 
    /**
     * 查询设备出入库记录
     *
     * @param id 设备出入库记录ID
     * @return 设备出入库记录
     */
    @Override
    public DeviceLog selectDeviceLogById(String id) {
        return deviceLogMapper.selectDeviceLogById(id);
    }
 
    /**
     * 查询设备出入库记录列表
     *
     * @param deviceLog 设备出入库记录
     * @return 设备出入库记录
     */
    @Override
    public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
        return deviceLogMapper.selectDeviceLogList(deviceLog);
    }
 
    /**
     * 新增设备出入库记录
     *
     * @param deviceLog 设备出入库记录
     * @return 结果
     */
    @Override
    public int insertDeviceLog(DeviceLog deviceLog) {
        if(ObjectUtils.isEmpty(deviceLog.getId())){
            deviceLog.setId(IdGenerate.nextId());
        }
        deviceLog.setCreateDate(DateUtils.getNowDate());
        return deviceLogMapper.insertDeviceLog(deviceLog);
    }
 
    /**
     * 修改设备出入库记录
     *
     * @param deviceLog 设备出入库记录
     * @return 结果
     */
    @Override
    public int updateDeviceLog(DeviceLog deviceLog) {
        return deviceLogMapper.updateDeviceLog(deviceLog);
    }
 
    /**
     * 删除设备出入库记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDeviceLogByIds(String ids) {
        return deviceLogMapper.deleteDeviceLogByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除设备出入库记录信息
     *
     * @param id 设备出入库记录ID
     * @return 结果
     */
    @Override
    public int deleteDeviceLogById(String id) {
        return deviceLogMapper.deleteDeviceLogById(id);
    }
 
 
    /**
     * 导入设备出入库记录
     */
    @Override
    public String importDeviceLog(String projectId,MultipartFile file, boolean updateSupport) {
        // 关联项目信息
        Project project = projectService.selectProjectById(projectId);
 
        try {
            String dest = saveExcel(file);
            List<DeviceLog> list = readData(dest);
            for (DeviceLog item : list) {
                // 转换项目信息
                item.setProjectId(projectId);
                item.setProjectName(project.getFullName());
 
                // 转换出入库类型
                String proTypeCode = RecordType.getKeyByName(item.getType());
                item.setType(proTypeCode);
 
                // 转换设备编码
                String deviceCode = item.getDeviceCode();
                if (!ObjectUtils.isEmpty(deviceCode)){
                    Device param = new Device();
                    param.setCode(deviceCode);
                    List<Device> devices = deviceService.selectDeviceList(param);
                    if (!ObjectUtils.isEmpty(devices)){
                        Device device = devices.get(0);
                        item.setDeviceId(device.getId());
                        item.setDeviceCode(device.getCode());
                        item.setDeviceName(device.getName());
                    }
                }
 
                item.setCreateDate(DateUtils.getNowDate());
                insertDeviceLog(item);
            }
        } catch (Exception e) {
            return e.toString();
        }
        return "导入完毕" ;
    }
 
    private String saveExcel(MultipartFile file) {
        String filename = file.getOriginalFilename();
        File dir = new File(fileSaveDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        String addr = fileSaveDir + filename;
        try {
            File dest = new File(addr);
            file.transferTo(dest);
        } catch (Exception e) {
            return null;
        }
        return addr;
    }
 
    private List<DeviceLog> readData(String filepath) {
        ExcelReader reader = ExcelUtil.getReader(filepath, 0);
        reader.addHeaderAlias("出入库单子号", "code");
        reader.addHeaderAlias("设备编码", "deviceCode");
        reader.addHeaderAlias("出入库类型", "type");
        reader.addHeaderAlias("出入库时间", "transactionDate");
        reader.addHeaderAlias("经办人", "optUser");
        reader.addHeaderAlias("使用人", "applyUser");
        reader.addHeaderAlias("出入库数量", "number");
        reader.addHeaderAlias("备注", "remark");
 
        List<DeviceLog> list = reader.readAll(DeviceLog.class);
        return list;
    }
}