地质所 沉降监测网建设项目
zmk
2024-05-17 5e697a70c7e49e18bc3bc2f75c2e9ad5bda17489
javaweb-plus/javaweb-cms/src/main/java/com/javaweb/geo/service/impl/DeviceLogServiceImpl.java
@@ -1,9 +1,18 @@
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;
@@ -11,6 +20,7 @@
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;
/**
@@ -21,8 +31,17 @@
 */
@Service
public class DeviceLogServiceImpl implements IDeviceLogService {
    @Autowired
    private DeviceLogMapper deviceLogMapper;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IDeviceService deviceService;
    private String fileSaveDir = Global.getProfile() + "\\template\\" ;
    /**
     * 查询设备出入库记录
@@ -57,7 +76,7 @@
        if(ObjectUtils.isEmpty(deviceLog.getId())){
            deviceLog.setId(IdGenerate.nextId());
        }
        deviceLog.setCreateTime(DateUtils.getNowDate());
        deviceLog.setCreateDate(DateUtils.getNowDate());
        return deviceLogMapper.insertDeviceLog(deviceLog);
    }
@@ -93,4 +112,79 @@
    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;
    }
}