diff --git a/psdc-business/src/main/java/com/psdc/entity/PsdcDevice.java b/psdc-business/src/main/java/com/psdc/entity/PsdcDevice.java index 2b1e570..71fc46e 100644 --- a/psdc-business/src/main/java/com/psdc/entity/PsdcDevice.java +++ b/psdc-business/src/main/java/com/psdc/entity/PsdcDevice.java @@ -1,5 +1,6 @@ package com.psdc.entity; +import com.psdc.annotation.Excel; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -17,25 +18,43 @@ import java.util.Date; public class PsdcDevice { /** 设备id */ + @Excel(name = "设备Id", cellType = Excel.ColumnType.NUMERIC) private Integer deviceId ; /** 用户id */ private Integer userId ; + /** 用户名称 */ + @Excel(name = "用户名称") + private String userName ; + /** 设备类型:1-监测设备,2-运行设备 */ private Integer deviceType ; - /** 设备sn */ - private String deviceSn ; + /** 设备类型:1-监测设备,2-运行设备 */ + @Excel(name = "设备类型") + private String devType ; /** 设备名称 */ + @Excel(name = "设备名称") private String deviceName ; + /** 设备sn */ + @Excel(name = "设备Sn") + private String deviceSn ; /** 硬件版本 */ + @Excel(name = "硬件版本") private String hardVersion ; /** 软件版本 */ + @Excel(name = "软件版本") private String softVersion ; /** 安装地址 */ + @Excel(name = "安装地址") private String deviceAddress ; /** 启用时间 */ + @Excel(name = "启用时间") private String startTime ; + /** 设备状态:1-未激活,2-禁用,3-在线,4-离线 */ private Integer deviceStatus ; + /** 设备状态:1-未激活,2-禁用,3-在线,4-离线 */ + @Excel(name = "设备状态") + private String devStatus ; /** 图片地址 */ private String photoUrl ; /** 创建者 */ @@ -46,8 +65,12 @@ public class PsdcDevice { private String updateBy ; /** 修改时间 */ private Date updateTime ; + /** 设备运行状态:1-开启,2-关闭 */ private Integer deviceRunstatus ; + /** 设备运行状态:1-开启,2-关闭 */ + @Excel(name = "设备运行状态") + private String devRunstatus ; } diff --git a/psdc-business/src/main/java/com/psdc/mapper/PsdcDeviceMapper.java b/psdc-business/src/main/java/com/psdc/mapper/PsdcDeviceMapper.java index f52cab9..a1610ca 100644 --- a/psdc-business/src/main/java/com/psdc/mapper/PsdcDeviceMapper.java +++ b/psdc-business/src/main/java/com/psdc/mapper/PsdcDeviceMapper.java @@ -82,4 +82,6 @@ public interface PsdcDeviceMapper{ */ int updateDevRunStatusByDevId(@Param(value = "deviceId") Integer deviceId, @Param(value = "runStatus") Integer runStatus); + + void saveDevicesData(@Param(value = "devRecord") List devRecord); } \ No newline at end of file diff --git a/psdc-business/src/main/java/com/psdc/service/IPsdcDeviceService.java b/psdc-business/src/main/java/com/psdc/service/IPsdcDeviceService.java index 22e4d30..fee64de 100644 --- a/psdc-business/src/main/java/com/psdc/service/IPsdcDeviceService.java +++ b/psdc-business/src/main/java/com/psdc/service/IPsdcDeviceService.java @@ -2,6 +2,7 @@ package com.psdc.service; import com.psdc.entity.PsdcDevice; import com.psdc.entity.vo.DeviceStatusVo; +import org.springframework.web.multipart.MultipartFile; import java.util.HashMap; import java.util.List; @@ -63,6 +64,8 @@ public interface IPsdcDeviceService { */ boolean deleteById(Long[] deviceIds); + boolean daoruDevicesData(MultipartFile file); + /** * 更新设备状态 * @param deviceId diff --git a/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java b/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java index f5529da..264b392 100644 --- a/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java +++ b/psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java @@ -13,16 +13,21 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; +import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; + @Service @Slf4j -public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ +public class PsdcDeviceServiceImpl implements IPsdcDeviceService { @Autowired private PsdcDeviceMapper psdcDeviceMapper; @@ -36,21 +41,21 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ * @param deviceId 主键 * @return 实例对象 */ - public PsdcDevice queryById(Integer deviceId){ + public PsdcDevice queryById(Integer deviceId) { return psdcDeviceMapper.queryById(deviceId); } - /** * 根据用户id查询设备列表 + * * @return */ - public List queryByUserId(){ + public List queryByUserId() { return psdcDeviceMapper.queryByUserId(SecurityUtils.getUserId()); } - public List queryDeviceStatus(){ + public List queryDeviceStatus() { List psdcDevices = psdcDeviceMapper.queryByUserId(SecurityUtils.getUserId()); return psdcDevices.stream().map(psdcDevice -> { DeviceStatusVo deviceStatusVo = new DeviceStatusVo(); @@ -70,13 +75,20 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ * @param psdcDevice 实例对象 * @return 实例对象 */ - public Integer insert(PsdcDevice psdcDevice){ + public Integer insert(PsdcDevice psdcDevice) { return psdcDeviceMapper.insert(psdcDevice); } @Override public List queryAllByLimit(PsdcDevice psdcDevice) { - return psdcDeviceMapper.queryAllByLimit(psdcDevice); + List list = psdcDeviceMapper.queryAllByLimit(psdcDevice); + for (PsdcDevice dev : list){ + dev.setDevType(s("t", dev.getDeviceType())); + dev.setDevStatus(s("s", dev.getDeviceStatus())); + dev.setDevRunstatus(s("r", dev.getDeviceRunstatus())); + } + + return list; } /** @@ -85,7 +97,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ * @param psdcDevice 实例对象 * @return 实例对象 */ - public Integer update(PsdcDevice psdcDevice){ + public Integer update(PsdcDevice psdcDevice) { return psdcDeviceMapper.update(psdcDevice); } @@ -95,42 +107,59 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ * @param deviceIds 主键 * @return 是否成功 */ - public boolean deleteById(Long[] deviceIds){ + public boolean deleteById(Long[] deviceIds) { int total = psdcDeviceMapper.deleteById(deviceIds); return total > 0; } + @Override + @Transactional(propagation = REQUIRES_NEW) + public boolean daoruDevicesData(MultipartFile file) { + try { + //需要将上传文件转成Stream流 + InputStream in = file.getInputStream(); + // EasyExcel.read(in, PsdcDevice.class, new DevicesDataListener(psdcDeviceMapper)).sheet().sheetNo(0).doRead(); + return true; + } catch (Exception e) { + e.printStackTrace(); + log.info("Sorry,导入设备数据失败!+ 错误原因: {}", e.getMessage()); + return false; + } + } + /** * 更新设备状态 + * * @param deviceId * @param runStatus * @return */ @Override public int updateDeviceRunStatus(Integer deviceId, Integer runStatus) { - return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId,runStatus); + return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId, runStatus); } /** * 控制设备启动停止 + * * @param deviceId * @param runStatus * @return */ @Override - public int controlDeviceStartAndStop(Integer deviceId, Integer runStatus,String controlBy,Integer controlMethod) { + public int controlDeviceStartAndStop(Integer deviceId, Integer runStatus, String controlBy, Integer controlMethod) { String value = ""; - if(runStatus == 1){ + if (runStatus == 1) { value = "启动"; - } else if ( runStatus == 2 ) { + } else if (runStatus == 2) { value = "停止"; } - log.info("设备id:{}",deviceId); + log.info("设备id:{}", deviceId); PsdcDevice psdcDevice = psdcDeviceMapper.queryById(deviceId); - if(psdcDevice == null){ - psdcControlLogMapper.insert(new PsdcControlLog(deviceId,null,null,"设备启停",value,controlMethod,3,"未找到该设备",controlBy)); + if (psdcDevice == null) { + psdcControlLogMapper.insert(new PsdcControlLog(deviceId, null, null, "设备启停", value, controlMethod, 3, "未找到该设备", controlBy)); throw new ControlException("控制失败,未找到该设备"); } @@ -138,27 +167,27 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),"设备启停",value,controlMethod,3,"手动控制,等待终端响应超时",controlBy)); //发送成功 - psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),"设备启停",value,controlMethod,2,"手动控制,控制成功",controlBy)); - return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId,runStatus); + psdcControlLogMapper.insert(new PsdcControlLog(deviceId, psdcDevice.getDeviceName(), psdcDevice.getDeviceSn(), "设备启停", value, controlMethod, 2, "手动控制,控制成功", controlBy)); + return psdcDeviceMapper.updateDevRunStatusByDevId(deviceId, runStatus); } @Override - public int setTemperature(Integer deviceId, List data, String controlBy,Integer controlMethod) { + public int setTemperature(Integer deviceId, List data, String controlBy, Integer controlMethod) { - log.info("设备id:{}",deviceId); - log.info("控制指令:{}",data); + log.info("设备id:{}", deviceId); + log.info("控制指令:{}", data); PsdcDevice psdcDevice = psdcDeviceMapper.queryById(deviceId); - if(psdcDevice == null){ - psdcControlLogMapper.insert(new PsdcControlLog(deviceId,null,null,"设定温度",null,controlMethod,3,"未找到该设备",controlBy)); + if (psdcDevice == null) { + psdcControlLogMapper.insert(new PsdcControlLog(deviceId, null, null, "设定温度", null, controlMethod, 3, "未找到该设备", controlBy)); throw new ControlException("控制失败,未找到该设备"); } AtomicInteger atomicInteger = new AtomicInteger(0); - for (Map map: data) { + for (Map map : data) { String controlKey = map.get("controlKey").toString(); String controlValue = map.get("controlValue").toString(); @@ -168,7 +197,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,controlValue,controlMethod,3,"等待终端响应超时",controlBy)); //发送成功 - psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,controlValue,controlMethod,2,"控制成功",controlBy)); + psdcControlLogMapper.insert(new PsdcControlLog(deviceId, psdcDevice.getDeviceName(), psdcDevice.getDeviceSn(), controlContext, controlValue, controlMethod, 2, "控制成功", controlBy)); atomicInteger.incrementAndGet(); } return atomicInteger.get(); @@ -176,6 +205,7 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ /** * 单条控制指令 + * * @param deviceId * @param key * @param value @@ -184,17 +214,17 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ * @return */ @Override - public int setTemperature(Integer deviceId, String key,String value, String controlBy,Integer controlMethod) { + public int setTemperature(Integer deviceId, String key, String value, String controlBy, Integer controlMethod) { - log.info("设备id:{}",deviceId); - log.info("控制指令:{}",key); - log.info("控制内容:{}",value); + log.info("设备id:{}", deviceId); + log.info("控制指令:{}", key); + log.info("控制内容:{}", value); String controlContext = ControlKeyEnum.getControlContext(key); PsdcDevice psdcDevice = psdcDeviceMapper.queryById(deviceId); - if(psdcDevice == null){ - psdcControlLogMapper.insert(new PsdcControlLog(deviceId,null,null,controlContext,null,controlMethod,3,"未找到该设备",controlBy)); + if (psdcDevice == null) { + psdcControlLogMapper.insert(new PsdcControlLog(deviceId, null, null, controlContext, null, controlMethod, 3, "未找到该设备", controlBy)); throw new ControlException("控制失败,未找到该设备"); } @@ -204,7 +234,39 @@ public class PsdcDeviceServiceImpl implements IPsdcDeviceService{ // psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,value,controlMethod,3,"等待终端响应超时",controlBy)); //发送成功 - return psdcControlLogMapper.insert(new PsdcControlLog(deviceId,psdcDevice.getDeviceName(),psdcDevice.getDeviceSn(),controlContext,value,controlMethod,2,"控制成功",controlBy)); + return psdcControlLogMapper.insert(new PsdcControlLog(deviceId, psdcDevice.getDeviceName(), psdcDevice.getDeviceSn(), controlContext, value, controlMethod, 2, "控制成功", controlBy)); } + + private String s(String s, int i) { + switch (s) { + case "s": + switch (i) { + case 1: + return "未激活"; + case 2: + return "禁用"; + case 3: + return "在线"; + case 4: + return "离线"; + } + case "t": + switch (i) { + case 1: + return "监测设备"; + case 2: + return "运行设备"; + } + case "r": + switch (i) { + case 1: + return "开启"; + case 2: + return "关闭"; + } + default: + return s; + } + } } \ No newline at end of file diff --git a/psdc-business/src/main/resources/mapper/business/PsdcDeviceMapper.xml b/psdc-business/src/main/resources/mapper/business/PsdcDeviceMapper.xml index eea3970..a257fcc 100644 --- a/psdc-business/src/main/resources/mapper/business/PsdcDeviceMapper.xml +++ b/psdc-business/src/main/resources/mapper/business/PsdcDeviceMapper.xml @@ -213,6 +213,17 @@ #{updateTime}) + + Insert into psdc_device(user_id, device_type, device_sn, device_name, hard_version, soft_version, + device_address, start_time, device_status, device_runstatus, photo_url, create_by, + create_time, update_by, update_time) + Values + + (NULL, #{entity.deviceType}, #{entity.deviceSn}, #{entity.deviceName}, #{entity.hardVersion}, #{entity.softVersion}, #{entity.deviceAddress}, + #{entity.startTime}, #{entity.deviceStatus}, #{entity.deviceRunstatus}, #{entity.photoUrl}, #{entity.createBy}, #{entity.createTime}, NULL, NULL) + + + update psdc_device diff --git a/psdc-web/src/main/java/com/psdc/controller/manager/PsdcDeviceController.java b/psdc-web/src/main/java/com/psdc/controller/manager/PsdcDeviceController.java index 906acea..6fa3511 100644 --- a/psdc-web/src/main/java/com/psdc/controller/manager/PsdcDeviceController.java +++ b/psdc-web/src/main/java/com/psdc/controller/manager/PsdcDeviceController.java @@ -1,25 +1,34 @@ package com.psdc.controller.manager; -import com.alibaba.fastjson2.JSON; + import com.alibaba.fastjson2.JSONObject; -import com.github.pagehelper.page.PageMethod; import com.psdc.annotation.Log; import com.psdc.core.controller.BaseController; import com.psdc.core.domain.AjaxResult; import com.psdc.core.domain.entity.SysUser; import com.psdc.core.page.TableDataInfo; -import com.psdc.entity.PsdcControlLog; import com.psdc.entity.PsdcDevice; import com.psdc.enums.BusinessType; import com.psdc.service.IPsdcDeviceService; import com.psdc.service.ISysUserService; import com.psdc.utils.SecurityUtils; -import org.apache.commons.lang3.ArrayUtils; +import com.psdc.utils.poi.ExcelUtil; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.*; /** @@ -80,17 +89,83 @@ public class PsdcDeviceController extends BaseController { map.put("value", dd.getUserId()); maps.add(map); } - return AjaxResult.success("用户下来列表", maps); + return AjaxResult.success("用户下拉列表", maps); } @PreAuthorize("@ss.hasPermi('manager:device:list')") - @PostMapping("/devicesList") + @RequestMapping(value = "/devicesList", method = RequestMethod.POST) public TableDataInfo controlLogList(@RequestBody JSONObject jsonObject) { - // PageMethod.startPage(jsonObject.getInteger("pageNum"),jsonObject.getInteger("pageSize")); - // PsdcDevice deviceVo = JSON.parseObject(String.valueOf(jsonObject), PsdcDevice.class); + // PageMethod.startPage(jsonObject.getInteger("pageNum"),jsonObject.getInteger("pageSize")); + // PsdcDevice deviceVo = JSON.parseObject(String.valueOf(jsonObject), PsdcDevice.class); PsdcDevice deviceVo = myPage(jsonObject, PsdcDevice.class); List list = psdcDeviceService.queryAllByLimit(deviceVo); return getDataTable(list); } + /** + excel文件的下载 + */ + @PreAuthorize("@ss.hasPermi('manager:device:list')") + @Log(title = "设备管理", businessType = BusinessType.EXPORT) + @RequestMapping(value = "/daochuDevs", method = RequestMethod.POST) + public void daochuDevDta(HttpServletResponse response, @RequestBody PsdcDevice pd) { + List list = psdcDeviceService.queryAllByLimit(pd); + for (PsdcDevice dev : list){ + dev.setUserName(sysUserService.selectUserById(Long.valueOf(dev.getUserId())).getUserName()); + } + ExcelUtil util = new ExcelUtil<>(PsdcDevice.class); + util.exportExcel(response, list, "角色数据"); + } + + /** + * 下载设备信息模板 + * @param response + */ + @PreAuthorize("@ss.hasPermi('manager:device:list')") + @Log(title = "设备管理", businessType = BusinessType.EXPORT) + @RequestMapping(value = "/downloadDevTemplate", method = RequestMethod.GET) + public void downloadTemplate(HttpServletResponse response) { + // 获取要下载的模板名称 + String fileName = "devTemplate.xlsx"; + // 设置要下载的文件的名称 + response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); + // 通知客服文件的MIME类型 + response.setContentType("application/vnd.ms-template;charset=UTF-8"); + // 获取文件的路径 + try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/AllDevicesData.xlsx")) { + //读取excel模板 + XSSFWorkbook wb = new XSSFWorkbook(inputStream); + wb.setSelectedTab(1); + XSSFSheet sheet = wb.createSheet("设备类型表"); + XSSFRow row0 = sheet.createRow(0); + XSSFRow row1 = sheet.createRow(1); + XSSFRow row2 = sheet.createRow(2); + row0.createCell(0).setCellValue("设备类型Id"); + row0.createCell(1).setCellValue("设备类型名称"); + row1.createCell(0).setCellValue("1"); + row1.createCell(1).setCellValue("监测设备"); + row2.createCell(0).setCellValue("2"); + row2.createCell(1).setCellValue("运行设备"); + + + OutputStream os = new BufferedOutputStream(response.getOutputStream()); + wb.write(os); + os.flush(); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("下载设备模板出错"); + } + } + + /** + excel文件的读取 + */ + @RequestMapping("/daoruDevData") + @PreAuthorize("hasAuthority('addevice')") + @Transactional(rollbackFor = Exception.class) + public AjaxResult daoruDevData(@RequestPart("file") MultipartFile file) { + return toAjax(psdcDeviceService.daoruDevicesData(file)); + } + } diff --git a/psdc-web/src/main/resources/template/AllDevicesData.xlsx b/psdc-web/src/main/resources/template/AllDevicesData.xlsx new file mode 100644 index 0000000..71c6db9 Binary files /dev/null and b/psdc-web/src/main/resources/template/AllDevicesData.xlsx differ