| | |
| | | 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.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 |
| | | public class DeviceLogServiceImpl implements IDeviceLogService { |
| | | |
| | | @Autowired |
| | | private DeviceLogMapper deviceLogMapper; |
| | | |
| | | @Autowired |
| | | private IProjectService projectService; |
| | | |
| | | private String fileSaveDir = Global.getProfile() + "\\template\\" ; |
| | | |
| | | /** |
| | | * 查询设备出入库记录 |
| | |
| | | */ |
| | | @Override |
| | | public int insertDeviceLog(DeviceLog deviceLog) { |
| | | if(ObjectUtils.isEmpty(deviceLog.getId())){ |
| | | deviceLog.setId(IdGenerate.nextId()); |
| | | } |
| | | deviceLog.setCreateTime(DateUtils.getNowDate()); |
| | | deviceLog.setCreateDate(DateUtils.getNowDate()); |
| | | return deviceLogMapper.insertDeviceLog(deviceLog); |
| | | } |
| | | |
| | |
| | | public int deleteDeviceLogById(String id) { |
| | | return deviceLogMapper.deleteDeviceLogById(id); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 导入设备出入库记录 |
| | | */ |
| | | @Override |
| | | public String importDeviceLog(String projectId,MultipartFile file, boolean updateSupport) { |
| | | // 关联项目信息 |
| | | Project project = projectService.selectProjectById(projectId); |
| | | // 关联code |
| | | Integer number = deviceLogMapper.selectMaxDeviceLogId(projectId); |
| | | |
| | | try { |
| | | String dest = saveExcel(file); |
| | | List<DeviceLog> list = readData(dest); |
| | | for (DeviceLog item : list) { |
| | | // 转换项目信息 |
| | | item.setProjectId(projectId); |
| | | item.setProjectName(project.getFullName()); |
| | | |
| | | // 转换number信息 |
| | | if (ObjectUtils.isEmpty(number)){ |
| | | number = 1; |
| | | }else { |
| | | number++; |
| | | } |
| | | String code = "ZTCJ" + String.format("%06d", number); |
| | | item.setCode(code); |
| | | |
| | | item.setCreateDate(DateUtils.getNowDate()); |
| | | insertDeviceLog(item); |
| | | } |
| | | } catch (Exception e) { |
| | | return e.toString(); |
| | | } |
| | | return "导入完毕" ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 查询数据库中id最大值 |
| | | * @return |
| | | * |
| | | * TRUNCATE TABLE js_device_log; |
| | | * ALTER TABLE js_device_log AUTO_INCREMENT = 1; |
| | | */ |
| | | @Override |
| | | public Integer selectMaxDeviceLogId(String projectId) { |
| | | return deviceLogMapper.selectMaxDeviceLogId(projectId); |
| | | } |
| | | |
| | | 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("设备名称", "deviceName"); |
| | | reader.addHeaderAlias("类型", "type"); |
| | | reader.addHeaderAlias("入库时间", "inboundDate"); |
| | | reader.addHeaderAlias("出库时间", "outboundDate"); |
| | | reader.addHeaderAlias("经办人", "optUser"); |
| | | reader.addHeaderAlias("负责人", "applyUser"); |
| | | reader.addHeaderAlias("出入库数量", "number"); |
| | | reader.addHeaderAlias("备注", "remark"); |
| | | |
| | | List<DeviceLog> list = reader.readAll(DeviceLog.class); |
| | | return list; |
| | | } |
| | | } |