EnvDeviceServiceImpl.java 19 KB

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