| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package com.studenthotel.services.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.mysql.cj.util.LogUtils;
- import com.studenthotel.model.pojo.ColdWater;
- import com.studenthotel.mapper.ColdWaterMapper;
- import com.studenthotel.model.pojo.Dorm;
- import com.studenthotel.services.ColdWaterService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.studenthotel.services.DormService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import org.springframework.util.LinkedMultiValueMap;
- import org.springframework.util.MultiValueMap;
- import org.springframework.web.client.RestTemplate;
- import java.math.BigDecimal;
- import java.util.List;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author liu
- * @since 2023-06-07
- */
- @Service
- public class ColdWaterServiceImpl extends ServiceImpl<ColdWaterMapper, ColdWater> implements ColdWaterService {
- @Autowired
- DormService dormService;
- private static String url = "https://jtishfw.ncjti.edu.cn/jxch-smartmp/HotWaters/cwaterMonthlist.action";
- private Integer page = 1;
- private Integer size = 8;
- // 每月1号每20分钟运行一次
- // @Scheduled(cron = "0/20 * * 1 * ? ")
- @Scheduled(cron = "0 0/1 * * * ? ")
- public void autoQueryOrder() {
- Page<Dorm> dPage = new Page<>(page, size);
- LambdaQueryWrapper<Dorm> wrapper=new LambdaQueryWrapper<>();
- IPage<Dorm> dormPage = dormService.page(dPage,wrapper);
- List<Dorm> records = dormPage.getRecords();
- long pages = dormPage.getPages();
- for (Dorm dorm : records) {
- if (dorm.getDom() != null) {
- try {
- this.getColdWater(dorm.getDom());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- if (page <= pages) {
- //将当前计数器的页码信息赋值给page
- page += 1;
- } else {
- page -= 1;
- }
- }
- public void getColdWater(String dormNumber) {
- String token="AqwxcdAxs4212pomk231qsxssaz";
- MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
- // dormNumber="墨轩湖校区"+dormNumber;
- map.add("dom", dormNumber);
- map.add("page", 1);
- map.add("rows", 10);
- // map.add("token",token);
- RestTemplate restTemplate = new RestTemplate();
- HttpHeaders headers = new HttpHeaders();
- headers.set("token",token);
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
- ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
- JSONObject jsonObj = JSON.parseObject(responseEntity.getBody());
- // rows为数组
- JSONArray jsonArray = jsonObj.getJSONArray("rows");
- if (ObjectUtils.isNotEmpty(jsonArray)) {
- // 0 2 4 6 7 8
- JSONObject jsonMap = jsonArray.getJSONObject(8);
- if (jsonMap != null) {
- ColdWater coldWater = new ColdWater();
- String dom = jsonMap.get("dom").toString();
- coldWater.setDom(dom);
- String build = jsonMap.getString("build");
- coldWater.setBuild(build);
- Double totalMoney = Double.valueOf(jsonMap.get("totalMoney").toString());
- Double totalPower = Double.valueOf(jsonMap.get("totalPower").toString());
- BigDecimal bg = new BigDecimal(totalMoney);
- BigDecimal bg2 = new BigDecimal(totalPower);
- /**
- * 参数:
- newScale - 要返回的 BigDecimal 值的标度。
- roundingMode - 要应用的舍入模式。
- 返回:
- 一个 BigDecimal,其标度为指定值,其非标度值可以通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定。
- */
- double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- double f2 = bg2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- coldWater.setTotalMoney(f1);
- coldWater.setTotalPower(f2);
- String dateTime = jsonMap.get("dataTime").toString();
- coldWater.setDataTime(dateTime);
- Boolean exists = chackColdWaterActive(dom, dateTime);
- if (!exists) {
- coldWater.setDeleted(0L);
- this.save(coldWater);
- }
- }
- }
- }
- /**
- * 通过宿舍号和时间判断该数据是否已添加
- *
- * @param dom
- * @param dateTime
- * @return
- */
- public Boolean chackColdWaterActive(String dom, String dateTime) {
- LambdaQueryWrapper<ColdWater> query = new LambdaQueryWrapper<>();
- query.eq(ColdWater::getDataTime, dateTime)
- .eq(ColdWater::getDom, dom);
- int count = this.count(query);
- if (count>0) {
- return true;
- }
- return false;
- }
- }
|