GatewayController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.huimv.env.admin.controller;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.huimv.env.admin.common.utils.Result;
  6. import com.huimv.env.admin.common.utils.ResultCode;
  7. import com.huimv.env.admin.entity.Terminal;
  8. import com.huimv.env.admin.entity.vo.*;
  9. import com.huimv.env.admin.entity.Gateway;
  10. import com.huimv.env.admin.service.IGatewayService;
  11. import com.huimv.env.admin.service.ITerminalService;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.util.List;
  15. /**
  16. * <p>
  17. * 采集网关 前端控制器
  18. * </p>
  19. *
  20. * @author newspaper
  21. * @since 2024-05-07
  22. */
  23. @RestController
  24. @CrossOrigin
  25. @RequestMapping("/gateway")
  26. public class GatewayController {
  27. @Autowired
  28. private IGatewayService gatewayService;
  29. @Autowired
  30. private ITerminalService terminalService;
  31. @PostMapping("/add")
  32. public Result add(@RequestBody GateWayAddParam gateWayAddParam){
  33. List<Gateway> gatewayList = gatewayService.list(new QueryWrapper<Gateway>().lambda().eq(Gateway::getDeviceCode, gateWayAddParam.getDeviceCode()));
  34. if (ObjectUtil.isNotEmpty(gatewayList)){
  35. return new Result(10001,"网关编号已存在",false);
  36. }
  37. Gateway gateway = new Gateway();
  38. gateway.setDeviceCode(gateWayAddParam.getDeviceCode());
  39. gateway.setDeviceName(gateWayAddParam.getDeviceName());
  40. gateway.setLocationId(gateWayAddParam.getLocationId());
  41. gateway.setType(gateWayAddParam.getType());
  42. gateway.setFarmId(gateWayAddParam.getFarmId());
  43. gatewayService.save(gateway);
  44. return Result.SUCCESS();
  45. }
  46. @PostMapping("/page")
  47. public Result page(@RequestBody GateWayPageParam gateWayPageParam){
  48. Page<Gateway> page = new Page<>(gateWayPageParam.getPageNum(),gateWayPageParam.getPageSize());
  49. Page<Gateway> gatewayPage = gatewayService.page(page,new QueryWrapper<Gateway>().lambda()
  50. .eq(Gateway::getFarmId,gateWayPageParam.getFarmId())
  51. .like(ObjectUtil.isNotEmpty(gateWayPageParam.getDeviceCode()),Gateway::getDeviceCode,gateWayPageParam.getDeviceCode()));
  52. return new Result(ResultCode.SUCCESS,gatewayPage);
  53. }
  54. @PostMapping("/list")
  55. public Result list(@RequestBody GateWayListParam gateWayListParam){
  56. List<Gateway> gatewayList = gatewayService.list(new QueryWrapper<Gateway>().lambda().eq(ObjectUtil.isNotEmpty(gateWayListParam.getFarmId()), Gateway::getFarmId, gateWayListParam.getFarmId()));
  57. return new Result(ResultCode.SUCCESS,gatewayList);
  58. }
  59. @PostMapping("/update")
  60. public Result update(@RequestBody GateWayUpdateParam gateWayUpdateParam){
  61. Gateway gateway = gatewayService.getById(gateWayUpdateParam.getId());
  62. List<Gateway> gatewayList = gatewayService.list(new QueryWrapper<Gateway>().lambda().eq(Gateway::getDeviceCode, gateWayUpdateParam.getDeviceCode()).ne(Gateway::getId,gateWayUpdateParam.getId()));
  63. if (ObjectUtil.isNotEmpty(gatewayList)){
  64. return new Result(10001,"网关编号已存在",false);
  65. }
  66. gateway.setDeviceCode(gateWayUpdateParam.getDeviceCode());
  67. gateway.setDeviceName(gateWayUpdateParam.getDeviceName());
  68. gateway.setLocationId(gateWayUpdateParam.getLocationId());
  69. gateway.setType(gateWayUpdateParam.getType());
  70. gateway.setFarmId(gateWayUpdateParam.getFarmId());
  71. gatewayService.updateById(gateway);
  72. return Result.SUCCESS();
  73. }
  74. @PostMapping("/delete")
  75. public Result delete(@RequestBody GateWayDeleteParam gateWayDeleteParam){
  76. List<Terminal> terminalList = terminalService.list(new QueryWrapper<Terminal>().lambda().eq(Terminal::getGatewayId, gateWayDeleteParam.getId()));
  77. if (ObjectUtil.isNotEmpty(terminalList)){
  78. return new Result(10001,"网关存在下属终端",false);
  79. }
  80. gatewayService.removeById(gateWayDeleteParam.getId());
  81. return Result.SUCCESS();
  82. }
  83. }