123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package com.jjt.calc.service.impl;
- import cn.hutool.json.JSONArray;
- import cn.hutool.json.JSONObject;
- import com.jjt.calc.domain.TwinCalcHourEnergy;
- import com.jjt.calc.mapper.TwinCalcHourEnergyMapper;
- import com.jjt.calc.service.ITwinCalcHourEnergyService;
- import com.jjt.common.utils.DateUtils;
- import com.jjt.utils.IotService;
- import com.jjt.utils.Tools;
- import com.jjt.ws.domain.TwinWorkshop;
- import com.jjt.ws.domain.TwinWorkshopEnergy;
- import com.jjt.ws.service.ITwinWorkshopService;
- import javafx.util.Pair;
- import org.apache.ibatis.session.ExecutorType;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.ZoneOffset;
- import java.util.*;
- /**
- * 能源1小时统计数据Service业务层处理
- *
- * @author wukai
- * @date 2025-01-18
- */
- @Service
- public class TwinCalcHourEnergyServiceImpl implements ITwinCalcHourEnergyService {
- @Resource
- private TwinCalcHourEnergyMapper twinCalcHourEnergyMapper;
- @Resource
- private IotService iotService;
- @Resource
- private SqlSessionFactory factory;
- @Resource
- private ITwinWorkshopService workshopService;
- /**
- * 查询能源1小时统计数据
- *
- * @param autoId 能源1小时统计数据主键
- * @return 能源1小时统计数据
- */
- @Override
- public TwinCalcHourEnergy selectTwinCalcHourEnergyByAutoId(Long autoId) {
- return twinCalcHourEnergyMapper.selectTwinCalcHourEnergyByAutoId(autoId);
- }
- /**
- * 查询能源1小时统计数据列表
- *
- * @param twinCalcHourEnergy 能源1小时统计数据
- * @return 能源1小时统计数据
- */
- @Override
- public List<TwinCalcHourEnergy> selectTwinCalcHourEnergyList(TwinCalcHourEnergy twinCalcHourEnergy) {
- return twinCalcHourEnergyMapper.selectTwinCalcHourEnergyList(twinCalcHourEnergy);
- }
- /**
- * 新增能源1小时统计数据
- *
- * @param twinCalcHourEnergy 能源1小时统计数据
- * @return 结果
- */
- @Override
- public int insertTwinCalcHourEnergy(TwinCalcHourEnergy twinCalcHourEnergy) {
- return twinCalcHourEnergyMapper.insertTwinCalcHourEnergy(twinCalcHourEnergy);
- }
- /**
- * 修改能源1小时统计数据
- *
- * @param twinCalcHourEnergy 能源1小时统计数据
- * @return 结果
- */
- @Override
- public int updateTwinCalcHourEnergy(TwinCalcHourEnergy twinCalcHourEnergy) {
- return twinCalcHourEnergyMapper.updateTwinCalcHourEnergy(twinCalcHourEnergy);
- }
- /**
- * 批量删除能源1小时统计数据
- *
- * @param autoIds 需要删除的能源1小时统计数据主键
- * @return 结果
- */
- @Override
- public int deleteTwinCalcHourEnergyByAutoIds(Long[] autoIds) {
- return twinCalcHourEnergyMapper.deleteTwinCalcHourEnergyByAutoIds(autoIds);
- }
- /**
- * 删除能源1小时统计数据信息
- *
- * @param autoId 能源1小时统计数据主键
- * @return 结果
- */
- @Override
- public int deleteTwinCalcHourEnergyByAutoId(Long autoId) {
- return twinCalcHourEnergyMapper.deleteTwinCalcHourEnergyByAutoId(autoId);
- }
- /**
- * 统计指定日期指定时段能耗数据
- *
- * @param start 开始时间戳
- * @param end 结束时间戳
- */
- @Override
- public void calcEnergy(LocalDateTime start, LocalDateTime end) {
- Date date = Date.from(start.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
- int period = start.getHour();
- Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
- Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
- String baseSql = "select %s from %s where time>%s and time <=%s";
- TwinWorkshop ws = workshopService.selectTwinWorkshopByWsId(3L);
- List<TwinWorkshopEnergy> energyList = ws.getTwinWorkshopEnergyList();
- List<TwinCalcHourEnergy> list = new ArrayList<>();
- for (TwinWorkshopEnergy energy : energyList) {
- String sql = String.format(baseSql, energy.getEnergyCode(), energy.getEnergyPath(), startTime, endTime);
- JSONObject jsonObject = iotService.query(sql);
- JSONObject data = jsonObject.getJSONObject("data");
- JSONArray values = data.getJSONArray("values");
- JSONArray first = values.getJSONArray(0);
- JSONArray last = values.getJSONArray(values.size() - 1);
- int res = last.getInt(0) - first.getInt(0);
- TwinCalcHourEnergy calcEnergy = new TwinCalcHourEnergy();
- calcEnergy.setEnergyId(energy.getEnergyId());
- BigDecimal dataV = BigDecimal.valueOf(res).multiply(energy.getCoefficient());
- calcEnergy.setDataValue(dataV);
- calcEnergy.setDataDate(date);
- calcEnergy.setHour(period);
- list.add(calcEnergy);
- }
- if (list.size() > 0) {
- try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
- TwinCalcHourEnergyMapper mapper = sqlSession.getMapper(TwinCalcHourEnergyMapper.class);
- list.forEach(mapper::insertTwinCalcHourEnergy);
- sqlSession.commit();
- }
- }
- }
- /**
- * 统计上一个时段的能源
- */
- @Override
- public void calcLastEnergy() {
- LocalDateTime ldt = Tools.currWholeTime();
- //上一个小时
- ldt = ldt.minusHours(1);
- //这里不需要向前取一秒了
- LocalDateTime start = ldt;
- LocalDateTime end = ldt.plusHours(1);
- calcEnergy(start, end);
- }
- /**
- * 按时间查询
- *
- * @param date 时间
- * @return 结果
- */
- @Override
- public List<TwinCalcHourEnergy> selectTwinEmpCalcListByDate(Date date) {
- LocalDate localDate = DateUtils.toLocalDate(date);
- //计算统计时间
- Pair<Date, Date> pair = Tools.calcDay(localDate);
- Date sTime = pair.getKey();
- Date eTime = pair.getValue();
- TwinCalcHourEnergy hour = new TwinCalcHourEnergy();
- Map<String, Object> params = new HashMap<>(16);
- params.put("sTime", sTime);
- params.put("eTime", eTime);
- hour.setParams(params);
- return selectTwinCalcHourEnergyList(hour);
- }
- }
|