2023-05-12 11:23:36 完成设备管理的导出、模板下载接口!

master
魔神煜修罗皇 2 years ago
parent b3720b33b5
commit 73fab0ad90
  1. 27
      psdc-business/src/main/java/com/psdc/entity/PsdcDevice.java
  2. 2
      psdc-business/src/main/java/com/psdc/mapper/PsdcDeviceMapper.java
  3. 3
      psdc-business/src/main/java/com/psdc/service/IPsdcDeviceService.java
  4. 126
      psdc-business/src/main/java/com/psdc/service/impl/PsdcDeviceServiceImpl.java
  5. 11
      psdc-business/src/main/resources/mapper/business/PsdcDeviceMapper.xml
  6. 87
      psdc-web/src/main/java/com/psdc/controller/manager/PsdcDeviceController.java
  7. BIN
      psdc-web/src/main/resources/template/AllDevicesData.xlsx

@ -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 ;
}

@ -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<PsdcDevice> devRecord);
}

@ -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

@ -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<PsdcDevice> queryByUserId(){
public List<PsdcDevice> queryByUserId() {
return psdcDeviceMapper.queryByUserId(SecurityUtils.getUserId());
}
public List<DeviceStatusVo> queryDeviceStatus(){
public List<DeviceStatusVo> queryDeviceStatus() {
List<PsdcDevice> 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<PsdcDevice> queryAllByLimit(PsdcDevice psdcDevice) {
return psdcDeviceMapper.queryAllByLimit(psdcDevice);
List<PsdcDevice> 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<HashMap> data, String controlBy,Integer controlMethod) {
public int setTemperature(Integer deviceId, List<HashMap> 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;
}
}
}

@ -213,6 +213,17 @@
#{updateTime})
</insert>
<insert id="saveDevicesData" parameterType="com.psdc.entity.PsdcDevice" useGeneratedKeys="true">
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
<foreach collection="devRecord" item="entity" separator=",">
(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)
</foreach>
</insert>
<!-- 更新数据 -->
<update id="update">
update psdc_device

@ -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,11 +89,11 @@ 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);
@ -93,4 +102,70 @@ public class PsdcDeviceController extends BaseController {
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<PsdcDevice> list = psdcDeviceService.queryAllByLimit(pd);
for (PsdcDevice dev : list){
dev.setUserName(sysUserService.selectUserById(Long.valueOf(dev.getUserId())).getUserName());
}
ExcelUtil<PsdcDevice> 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));
}
}

Loading…
Cancel
Save