EnvDeviceServiceImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.huimv.admin.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.huimv.admin.common.utils.Result;
  7. import com.huimv.admin.common.utils.ResultCode;
  8. import com.huimv.admin.entity.EnvDevice;
  9. import com.huimv.admin.entity.dto.DeviceDto;
  10. import com.huimv.admin.mapper.EnvDeviceMaintainMapper;
  11. import com.huimv.admin.mapper.EnvDeviceMapper;
  12. import com.huimv.admin.service.IEnvDeviceService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.text.NumberFormat;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * <p>
  22. * 服务实现类
  23. * </p>
  24. *
  25. * @author author
  26. * @since 2023-02-13
  27. */
  28. @Service
  29. public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice> implements IEnvDeviceService {
  30. @Autowired
  31. private EnvDeviceMapper envDeviceMapper;
  32. @Autowired
  33. private EnvDeviceMaintainMapper envDeviceMaintainMapper;
  34. @Override
  35. public Result count(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  36. String farmId = paramsMap.get("farmId");
  37. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  38. queryWrapper.eq("farm_id", farmId);
  39. Integer count = envDeviceMapper.selectCount(queryWrapper);
  40. QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
  41. queryWrapper1.eq("device_status", 1);
  42. Integer count1 = envDeviceMapper.selectCount(queryWrapper1);
  43. Integer OffDeviceCount = count- count1;
  44. //创建一个数值格式化对象
  45. NumberFormat numberFormat = NumberFormat.getInstance();
  46. //设置精确到小数点后两位
  47. numberFormat.setMaximumFractionDigits(2);
  48. String onDeviceRate = numberFormat.format((float)count1 / (float) count* 100) + "%";
  49. JSONObject jsonObject = new JSONObject();
  50. jsonObject.put("DeviceCount", count);
  51. jsonObject.put("OnDeviceCount", count1);
  52. jsonObject.put("OffDeviceCount", OffDeviceCount);
  53. jsonObject.put("OnDeviceRate", onDeviceRate);
  54. return new Result(ResultCode.SUCCESS, jsonObject);
  55. }
  56. @Override
  57. public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  58. String farmId = paramsMap.get("farmId");
  59. String pageSize = paramsMap.get("pageSize");
  60. String pageNo = paramsMap.get("pageNo");
  61. if (pageSize==null||pageSize=="") {
  62. pageSize = "10";
  63. }
  64. if (pageNo==null||pageNo=="") {
  65. pageNo = "1";
  66. }
  67. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  68. queryWrapper.eq("farm_id", farmId);
  69. Page<EnvDevice> page = new Page(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
  70. return new Result(ResultCode.SUCCESS,envDeviceMapper.selectPage(page, queryWrapper));
  71. }
  72. @Override
  73. public Result add(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  74. String farmId = paramsMap.get("farmId");
  75. String DeviceName = paramsMap.get("DeviceName");
  76. String BuildLocation = paramsMap.get("BuildLocation");
  77. String DeviceBrand = paramsMap.get("DeviceBrand");
  78. String remark = paramsMap.get("remark");
  79. if (remark == null || remark == "") {
  80. remark = null;
  81. }
  82. EnvDevice envDevice = new EnvDevice();
  83. envDevice.setFarmId(Integer.parseInt(farmId));
  84. envDevice.setDeviceName(DeviceName);
  85. envDevice.setBuildLocation(BuildLocation);
  86. envDevice.setDeviceBrand(DeviceBrand);
  87. envDevice.setRemark(remark);
  88. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  89. queryWrapper.eq("device_name", DeviceName).eq("farm_id",farmId);
  90. EnvDevice device = envDeviceMapper.selectOne(queryWrapper);
  91. if (ObjectUtil.isEmpty(device)) {
  92. envDeviceMapper.insert(envDevice);
  93. } else {
  94. return new Result(ResultCode.FAIL, "设备名称已存在");
  95. }
  96. return new Result(ResultCode.SUCCESS,"添加成功");
  97. }
  98. @Override
  99. public Result edit(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  100. String farmId = paramsMap.get("farmId");
  101. String id = paramsMap.get("id");
  102. String DeviceName = paramsMap.get("DeviceName");
  103. String BuildLocation = paramsMap.get("BuildLocation");
  104. String DeviceBrand = paramsMap.get("DeviceBrand");
  105. String remark = paramsMap.get("remark");
  106. if (remark == null || remark == "") {
  107. remark = null;
  108. }
  109. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  110. queryWrapper.eq("id", id);
  111. EnvDevice envDevice = envDeviceMapper.selectOne(queryWrapper);
  112. envDevice.setFarmId(Integer.parseInt(farmId));
  113. envDevice.setDeviceName(DeviceName);
  114. envDevice.setDeviceBrand(DeviceBrand);
  115. envDevice.setBuildLocation(BuildLocation);
  116. envDevice.setRemark(remark);
  117. QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
  118. queryWrapper1.eq("device_name", DeviceName);
  119. if (ObjectUtil.isEmpty(envDeviceMapper.selectOne(queryWrapper1))) {
  120. envDeviceMapper.updateById(envDevice);
  121. } else {
  122. return new Result(ResultCode.FAIL, "设备名称已存在");
  123. }
  124. return new Result(ResultCode.SUCCESS);
  125. }
  126. @Override
  127. public Result delete(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  128. String farmId = paramsMap.get("farmId");
  129. String id = paramsMap.get("id");
  130. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  131. queryWrapper.eq("id", id).eq("farm_id", farmId);
  132. envDeviceMapper.delete(queryWrapper);
  133. return new Result(ResultCode.SUCCESS,"删除成功");
  134. }
  135. }