ColdWaterServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.studenthotel.services.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.mysql.cj.util.LogUtils;
  10. import com.studenthotel.model.pojo.ColdWater;
  11. import com.studenthotel.mapper.ColdWaterMapper;
  12. import com.studenthotel.model.pojo.Dorm;
  13. import com.studenthotel.services.ColdWaterService;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.studenthotel.services.DormService;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.http.HttpEntity;
  18. import org.springframework.http.HttpHeaders;
  19. import org.springframework.http.MediaType;
  20. import org.springframework.http.ResponseEntity;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.util.LinkedMultiValueMap;
  24. import org.springframework.util.MultiValueMap;
  25. import org.springframework.web.client.RestTemplate;
  26. import java.math.BigDecimal;
  27. import java.util.List;
  28. /**
  29. * <p>
  30. * 服务实现类
  31. * </p>
  32. *
  33. * @author liu
  34. * @since 2023-06-07
  35. */
  36. @Service
  37. public class ColdWaterServiceImpl extends ServiceImpl<ColdWaterMapper, ColdWater> implements ColdWaterService {
  38. @Autowired
  39. DormService dormService;
  40. private static String url = "https://jtishfw.ncjti.edu.cn/jxch-smartmp/HotWaters/cwaterMonthlist.action";
  41. private Integer page = 1;
  42. private Integer size = 8;
  43. // 每月1号每20分钟运行一次
  44. // @Scheduled(cron = "0/20 * * 1 * ? ")
  45. @Scheduled(cron = "0 0/1 * * * ? ")
  46. public void autoQueryOrder() {
  47. Page<Dorm> dPage = new Page<>(page, size);
  48. LambdaQueryWrapper<Dorm> wrapper=new LambdaQueryWrapper<>();
  49. IPage<Dorm> dormPage = dormService.page(dPage,wrapper);
  50. List<Dorm> records = dormPage.getRecords();
  51. long pages = dormPage.getPages();
  52. for (Dorm dorm : records) {
  53. if (dorm.getDom() != null) {
  54. try {
  55. this.getColdWater(dorm.getDom());
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. if (page <= pages) {
  62. //将当前计数器的页码信息赋值给page
  63. page += 1;
  64. } else {
  65. page -= 1;
  66. }
  67. }
  68. public void getColdWater(String dormNumber) {
  69. String token="AqwxcdAxs4212pomk231qsxssaz";
  70. MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
  71. // dormNumber="墨轩湖校区"+dormNumber;
  72. map.add("dom", dormNumber);
  73. map.add("page", 1);
  74. map.add("rows", 10);
  75. // map.add("token",token);
  76. RestTemplate restTemplate = new RestTemplate();
  77. HttpHeaders headers = new HttpHeaders();
  78. headers.set("token",token);
  79. headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  80. HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
  81. ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
  82. JSONObject jsonObj = JSON.parseObject(responseEntity.getBody());
  83. // rows为数组
  84. JSONArray jsonArray = jsonObj.getJSONArray("rows");
  85. if (ObjectUtils.isNotEmpty(jsonArray)) {
  86. // 0 2 4 6 7 8
  87. JSONObject jsonMap = jsonArray.getJSONObject(8);
  88. if (jsonMap != null) {
  89. ColdWater coldWater = new ColdWater();
  90. String dom = jsonMap.get("dom").toString();
  91. coldWater.setDom(dom);
  92. String build = jsonMap.getString("build");
  93. coldWater.setBuild(build);
  94. Double totalMoney = Double.valueOf(jsonMap.get("totalMoney").toString());
  95. Double totalPower = Double.valueOf(jsonMap.get("totalPower").toString());
  96. BigDecimal bg = new BigDecimal(totalMoney);
  97. BigDecimal bg2 = new BigDecimal(totalPower);
  98. /**
  99. * 参数:
  100. newScale - 要返回的 BigDecimal 值的标度。
  101. roundingMode - 要应用的舍入模式。
  102. 返回:
  103. 一个 BigDecimal,其标度为指定值,其非标度值可以通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定。
  104. */
  105. double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  106. double f2 = bg2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  107. coldWater.setTotalMoney(f1);
  108. coldWater.setTotalPower(f2);
  109. String dateTime = jsonMap.get("dataTime").toString();
  110. coldWater.setDataTime(dateTime);
  111. Boolean exists = chackColdWaterActive(dom, dateTime);
  112. if (!exists) {
  113. coldWater.setDeleted(0L);
  114. this.save(coldWater);
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * 通过宿舍号和时间判断该数据是否已添加
  121. *
  122. * @param dom
  123. * @param dateTime
  124. * @return
  125. */
  126. public Boolean chackColdWaterActive(String dom, String dateTime) {
  127. LambdaQueryWrapper<ColdWater> query = new LambdaQueryWrapper<>();
  128. query.eq(ColdWater::getDataTime, dateTime)
  129. .eq(ColdWater::getDom, dom);
  130. int count = this.count(query);
  131. if (count>0) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. }