EnvDeviceServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package com.huimv.admin.service.impl;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.collection.ListUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import cn.hutool.json.JSONUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  11. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  12. import com.huimv.admin.common.utils.DataUill;
  13. import com.huimv.admin.common.utils.Result;
  14. import com.huimv.admin.common.utils.ResultCode;
  15. import com.huimv.admin.entity.BasePigpen;
  16. import com.huimv.admin.entity.EnvData;
  17. import com.huimv.admin.entity.EnvDevice;
  18. import com.huimv.admin.mapper.BasePigpenMapper;
  19. import com.huimv.admin.mapper.EnvDataMapper;
  20. import com.huimv.admin.mapper.EnvDeviceMapper;
  21. import com.huimv.admin.service.IEnvDeviceService;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.http.HttpEntity;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.http.HttpMethod;
  26. import org.springframework.http.ResponseEntity;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import org.springframework.web.client.RestTemplate;
  30. import javax.servlet.http.HttpServletRequest;
  31. import java.text.NumberFormat;
  32. import java.util.*;
  33. import java.util.concurrent.CopyOnWriteArrayList;
  34. import java.util.stream.Collectors;
  35. /**
  36. * <p>
  37. * 服务实现类
  38. * </p>
  39. *
  40. * @author author
  41. * @since 2023-02-13
  42. */
  43. @Service
  44. public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice> implements IEnvDeviceService {
  45. @Autowired
  46. private EnvDeviceMapper envDeviceMapper;
  47. @Autowired
  48. private BasePigpenMapper basePigpenMapper;
  49. @Autowired
  50. private EnvDataMapper dataMapper;
  51. // @Autowired
  52. // private RestTemplate restTemplate;
  53. @Override
  54. public Result count(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  55. String farmId = paramsMap.get("farmId");
  56. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  57. queryWrapper.eq("farm_id", farmId);
  58. Integer count = envDeviceMapper.selectCount(queryWrapper);
  59. JSONObject jsonObject = new JSONObject();
  60. if (count == 0) {
  61. jsonObject.put("DeviceCount", 0);
  62. jsonObject.put("OnDeviceCount", 0);
  63. jsonObject.put("OffDeviceCount", 0);
  64. jsonObject.put("OnDeviceRate", 0);
  65. } else {
  66. QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
  67. queryWrapper1.eq("device_status", 1);
  68. Integer count1 = envDeviceMapper.selectCount(queryWrapper1);
  69. Integer OffDeviceCount = count- count1;
  70. //创建一个数值格式化对象
  71. NumberFormat numberFormat = NumberFormat.getInstance();
  72. //设置精确到小数点后两位
  73. numberFormat.setMaximumFractionDigits(2);
  74. String onDeviceRate = numberFormat.format((float)count1 / (float) count* 100) + "%";
  75. jsonObject.put("DeviceCount", count);
  76. jsonObject.put("OnDeviceCount", count1);
  77. jsonObject.put("OffDeviceCount", OffDeviceCount);
  78. jsonObject.put("OnDeviceRate", onDeviceRate);
  79. }
  80. return new Result(ResultCode.SUCCESS, jsonObject);
  81. }
  82. @Override
  83. public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  84. String farmId = paramsMap.get("farmId");
  85. String pageSize = paramsMap.get("pageSize");
  86. String pageNo = paramsMap.get("pageNo");
  87. if (pageSize==null||pageSize=="") {
  88. pageSize = "10";
  89. }
  90. if (pageNo==null||pageNo=="") {
  91. pageNo = "1";
  92. }
  93. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  94. queryWrapper.eq("farm_id", farmId);
  95. Page<EnvDevice> page = new Page(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
  96. return new Result(ResultCode.SUCCESS,envDeviceMapper.selectPage(page, queryWrapper));
  97. }
  98. @Override
  99. public Result add(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  100. String farmId = paramsMap.get("farmId");
  101. String DeviceName = paramsMap.get("DeviceName");
  102. String BuildLocation = paramsMap.get("BuildLocation");
  103. String DeviceBrand = paramsMap.get("DeviceBrand");
  104. String remark = paramsMap.get("remark");
  105. if (remark == null || remark == "") {
  106. remark = null;
  107. }
  108. EnvDevice envDevice = new EnvDevice();
  109. envDevice.setFarmId(Integer.parseInt(farmId));
  110. envDevice.setDeviceName(DeviceName);
  111. envDevice.setBuildLocation(BuildLocation);
  112. envDevice.setDeviceBrand(DeviceBrand);
  113. envDevice.setRemark(remark);
  114. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  115. queryWrapper.eq("device_name", DeviceName).eq("farm_id",farmId);
  116. EnvDevice device = envDeviceMapper.selectOne(queryWrapper);
  117. if (ObjectUtil.isEmpty(device)) {
  118. envDeviceMapper.insert(envDevice);
  119. } else {
  120. return new Result(ResultCode.FAIL, "设备名称已存在");
  121. }
  122. return new Result(ResultCode.SUCCESS,"添加成功");
  123. }
  124. @Override
  125. public Result edit(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  126. String farmId = paramsMap.get("farmId");
  127. String id = paramsMap.get("id");
  128. String DeviceName = paramsMap.get("DeviceName");
  129. String BuildLocation = paramsMap.get("BuildLocation");
  130. String DeviceBrand = paramsMap.get("DeviceBrand");
  131. String remark = paramsMap.get("remark");
  132. if (remark == null || remark == "") {
  133. remark = null;
  134. }
  135. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  136. queryWrapper.eq("id", id);
  137. EnvDevice envDevice = envDeviceMapper.selectOne(queryWrapper);
  138. envDevice.setFarmId(Integer.parseInt(farmId));
  139. envDevice.setDeviceName(DeviceName);
  140. envDevice.setDeviceBrand(DeviceBrand);
  141. envDevice.setBuildLocation(BuildLocation);
  142. envDevice.setRemark(remark);
  143. QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
  144. queryWrapper1.eq("device_name", DeviceName);
  145. if (ObjectUtil.isEmpty(envDeviceMapper.selectOne(queryWrapper1))) {
  146. envDeviceMapper.updateById(envDevice);
  147. } else {
  148. return new Result(ResultCode.FAIL, "设备名称已存在");
  149. }
  150. return new Result(ResultCode.SUCCESS);
  151. }
  152. @Override
  153. public Result delete(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  154. String farmId = paramsMap.get("farmId");
  155. String id = paramsMap.get("id");
  156. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  157. queryWrapper.eq("id", id).eq("farm_id", farmId);
  158. envDeviceMapper.delete(queryWrapper);
  159. return new Result(ResultCode.SUCCESS,"删除成功");
  160. }
  161. @Override
  162. public Result listPigpen(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  163. String farmId = paramsMap.get("farmId");
  164. String id = paramsMap.get("id");//楼层id
  165. JSONArray jsonArray = new JSONArray();
  166. QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
  167. queryWrapper.eq("farm_id", farmId);
  168. if (id == null || id == "") {
  169. queryWrapper.eq("parent_id", 3);
  170. } else {
  171. queryWrapper.like("other2", id);
  172. }
  173. List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);//得到栋舍单元
  174. for (int i = 0; i < basePigpens.size(); i++) {
  175. /* QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
  176. queryWrapper1.eq("unit_name",basePigpens.get(i).getId());
  177. EnvDevice envDevice = envDeviceMapper.selectOne(queryWrapper1);//找到栋舍绑定的设备,利用单元id*/
  178. QueryWrapper<EnvData> queryWrapper2 = new QueryWrapper<>();
  179. queryWrapper2.eq("unit_id",basePigpens.get(i).getId()).orderByDesc("create_time").last(" limit 1");//通过设备id来拿取数据
  180. EnvData envData = dataMapper.selectOne(queryWrapper2);
  181. JSONObject jsonObject = new JSONObject();
  182. if (ObjectUtil.isEmpty(envData)) {
  183. jsonObject.put("temp", 0);//温度
  184. jsonObject.put("hum", 0);//湿度
  185. jsonObject.put("location", basePigpens.get(i).getBuildName());
  186. jsonObject.put("unit_id", 0);//单元id
  187. } else {
  188. jsonObject.put("temp", envData.getEnvTemp());//温度
  189. jsonObject.put("hum", envData.getEnvHum());//湿度
  190. jsonObject.put("location", basePigpens.get(i).getBuildName());
  191. jsonObject.put("unit_id", envData.getUnitId());//单元id
  192. }
  193. jsonArray.add(jsonObject);
  194. }
  195. return new Result(ResultCode.SUCCESS,jsonArray);
  196. }
  197. @Override
  198. public Result listEnv(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  199. String farmId = paramsMap.get("farmId");
  200. String id = paramsMap.get("id");//单元id
  201. String type = paramsMap.get("type");//查询类型
  202. String startTime = paramsMap.get("startTime");
  203. String endTime = paramsMap.get("endTime");
  204. if (type == null || type == "") {
  205. type = "1";
  206. }
  207. QueryWrapper<BasePigpen> basePigpenQueryWrapper = new QueryWrapper<>();
  208. basePigpenQueryWrapper.eq("farm_id", farmId).eq("id", id);
  209. BasePigpen basePigpen = basePigpenMapper.selectOne(basePigpenQueryWrapper);
  210. QueryWrapper<EnvData> queryWrapper = new QueryWrapper<>();
  211. queryWrapper.eq("unit_id", id).eq("farm_id", farmId);
  212. Map map = new HashMap<>();
  213. //自定义查询
  214. if ("4".equals(type)) {
  215. startTime = startTime + " 00:00:00";
  216. endTime = endTime + " 23:59:59";
  217. queryWrapper.between("create_time", startTime, endTime);
  218. List<EnvData> envData = dataMapper.listDay(queryWrapper);
  219. map.put("location", basePigpen.getBuildName());
  220. map.put("data", envData);
  221. }
  222. //本月
  223. else if ("3".equals(type)) {
  224. Date timesMonthmorning = DataUill.getTimesMonthmorning();
  225. queryWrapper.ge("create_time", timesMonthmorning);
  226. List<EnvData> envData = dataMapper.listDay(queryWrapper);
  227. map.put("location", basePigpen.getBuildName());
  228. map.put("data", envData);
  229. }
  230. //本周
  231. else if ("2".equals(type)) {
  232. Calendar calendar = Calendar.getInstance();
  233. calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
  234. queryWrapper.ge("create_time",calendar.getTime());
  235. List<EnvData> envData = dataMapper.listDay(queryWrapper);
  236. map.put("location", basePigpen.getBuildName());
  237. map.put("data", envData);
  238. }
  239. //今日
  240. else if ("1".equals(type)) {
  241. Date timesmorning = DataUill.getTimesmorning();
  242. queryWrapper.ge("create_time", timesmorning);
  243. List<EnvData> envData = dataMapper.listDay(queryWrapper);
  244. map.put("location", basePigpen.getBuildName());
  245. map.put("data", envData);
  246. }
  247. return new Result(ResultCode.SUCCESS,map);
  248. }
  249. @Override
  250. public Result listDeviceCount(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  251. String farmId = paramsMap.get("farmId");
  252. Integer offCount = 0;
  253. Integer onCount = 0;
  254. QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
  255. queryWrapper.eq("farm_id", farmId).eq("parent_id", 0);
  256. List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);//得到所有的栋
  257. JSONArray jsonArray = new JSONArray();
  258. for (int i = 0; i < basePigpens.size(); i++) {
  259. QueryWrapper<BasePigpen> queryWrapper1 = new QueryWrapper<>();
  260. queryWrapper1.like("other2", basePigpens.get(i).getId());
  261. List<BasePigpen> basePigpens1 = basePigpenMapper.selectList(queryWrapper1);//得到所有的楼层
  262. for (int j = 0; j < basePigpens1.size(); j++) {
  263. QueryWrapper<EnvDevice> deviceQueryWrapper = new QueryWrapper<>();
  264. deviceQueryWrapper.eq("unit_id",basePigpens1.get(j).getId());
  265. EnvDevice envDevice = envDeviceMapper.selectOne(deviceQueryWrapper);
  266. if (ObjectUtil.isNotEmpty(envDevice)) {
  267. if (envDevice.getDeviceStatus() == 0) {
  268. offCount++;
  269. } else {
  270. onCount++;
  271. }
  272. }
  273. }
  274. JSONObject jsonObject = new JSONObject();
  275. jsonObject.put("location",basePigpens.get(i).getBuildName());
  276. jsonObject.put("onDevice", onCount);
  277. jsonObject.put("offDevice", offCount);
  278. jsonArray.add(jsonObject);
  279. offCount = 0;
  280. onCount = 0;
  281. }
  282. return new Result(ResultCode.SUCCESS,jsonArray);
  283. }
  284. // @Override
  285. // @Transactional
  286. // public Result sync(Map<String, String> params) throws Exception {
  287. // String farmId = params.get("farmId");
  288. // //获取所有栏舍
  289. // Map<String, Object> map = new HashMap<String, Object>();
  290. // String s = HttpClientSSLUtils.doPost("https://yzwlw.loongk.com/mobile/login?username=江西增鑫&password=21218cca77804d2ba1922c33e0151105", JSON.toJSONString(map));
  291. // System.out.println("登录带栏舍:"+s);
  292. // LoginDto loginDto = JSONUtil.toBean(s, LoginDto.class);
  293. // DataToken token = loginDto.getData().getToken();
  294. // String encode = Base64.encode(token.getUserId() + "_" + token.getToken());
  295. // HttpHeaders headers = new HttpHeaders();
  296. // headers.add("Authorization",encode);
  297. // HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
  298. // List<DataShacks> shacks = loginDto.getData().getShacks();
  299. // // 不能删除,需做比较
  300. // List<String> zengXinDeviceId = shacks.stream().map(DataShacks::getId).collect(Collectors.toList());
  301. // List<String> huatongDeviceId = envDeviceMapper.selectDeviceCodeByfarmId(Integer.parseInt(farmId));
  302. //
  303. // CopyOnWriteArrayList<String> zengXinDeviceIdCopy = ListUtil.toCopyOnWriteArrayList(zengXinDeviceId);
  304. // CopyOnWriteArrayList<String> huatongDeviceIdCopy = ListUtil.toCopyOnWriteArrayList(huatongDeviceId);
  305. // //新增的设备
  306. // zengXinDeviceIdCopy.removeAll(huatongDeviceId);
  307. // //新增
  308. // if (zengXinDeviceIdCopy.size() >0){
  309. // for (String deviceId : zengXinDeviceIdCopy) {
  310. // syncConfig(deviceId,requestEntity,farmId);
  311. // }
  312. // }
  313. // //需要删除的设备
  314. // huatongDeviceIdCopy.removeAll(zengXinDeviceId);
  315. // if (huatongDeviceIdCopy.size() >0){
  316. // this.remove(new QueryWrapper<EnvDevice>().in("device_code",huatongDeviceIdCopy));
  317. // }
  318. // System.out.println("zengxin:"+zengXinDeviceIdCopy);
  319. // System.out.println(huatongDeviceIdCopy);
  320. // return new Result(10000,"同步成功",false);
  321. // }
  322. @Override
  323. public Result bandingUnitId(HttpServletRequest httpServletRequest, EnvDevice envDevice) {
  324. Integer unitId = envDevice.getUnitId();
  325. int count = this.count(new QueryWrapper<EnvDevice>().eq("unit_id", unitId));
  326. if (count>0){
  327. return new Result(10001,"绑定失败,该栋舍已有设备",false);
  328. }
  329. this.updateById(envDevice);
  330. return new Result(10000,"绑定成功",true);
  331. }
  332. @Override
  333. public Result listPigpenAll(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  334. String farmId = paramsMap.get("farmId");
  335. List objects = new ArrayList<>();
  336. List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().eq("farm_id", farmId).eq("f_type",3));
  337. for (BasePigpen basePigpen : basePigpens) {
  338. Integer id = basePigpen.getId();
  339. EnvData envData = dataMapper.selectOne(new QueryWrapper<EnvData>().eq("unit_id", id).orderByDesc("id").last("limit 1"));
  340. JSONObject jsonObject = new JSONObject();
  341. if (ObjectUtil.isNotEmpty(envData)){
  342. jsonObject.put("temp", envData.getEnvTemp());//温度
  343. jsonObject.put("hum", envData.getEnvHum());//湿度
  344. jsonObject.put("location", basePigpen.getBuildName());
  345. jsonObject.put("unit_id", basePigpen.getId());//单元id
  346. }else {
  347. jsonObject.put("temp", 0);//温度
  348. jsonObject.put("hum", 0);//湿度
  349. jsonObject.put("location", basePigpen.getBuildName());
  350. jsonObject.put("unit_id", basePigpen.getId());//单元id
  351. }
  352. objects.add(jsonObject);
  353. }
  354. return new Result(ResultCode.SUCCESS,objects);
  355. }
  356. // //添加新的设备
  357. // private void syncConfig(String shackId,HttpEntity httpEntity,String farmId) {
  358. // try {
  359. // ResponseEntity<String> exchangePeizhi = restTemplate.exchange("https://yzwlw.loongk.com/mobile/loadShackConfig/"+shackId, HttpMethod.GET, httpEntity, String.class);
  360. // String peizhibody = exchangePeizhi.getBody();
  361. // ShackConfigDto shackConfigDto = JSONUtil.toBean(peizhibody, ShackConfigDto.class);
  362. // ShackConfigData data = shackConfigDto.getData();
  363. // List<ShackConfigDataSensors> sensors = data.getSensors();
  364. // if (ObjectUtil.isNotEmpty(sensors)){
  365. // EnvDevice envDevice =new EnvDevice();
  366. // envDevice.setFarmId(Integer.parseInt(farmId));
  367. // envDevice.setDeviceBrand("增鑫");
  368. // envDevice.setDeviceCode(data.getId());
  369. // envDevice.setDeviceName(data.getName());
  370. // for (ShackConfigDataSensors sensor : sensors) {
  371. // if ("TEMPERATURE".equals(sensor.getType())){
  372. // envDevice.setOhter1(sensor.getId());
  373. // }
  374. // if ("HUMIDITY".equals(sensor.getType())){
  375. // envDevice.setOhter2(sensor.getId());
  376. // }
  377. // }
  378. // envDeviceMapper.insert(envDevice);
  379. // }
  380. // }catch (Exception e){
  381. // System.out.println("设备同步异常:" + e +" deviceId"+shackId);
  382. // }
  383. //
  384. // }
  385. }