142 lines
5.6 KiB
Java
142 lines
5.6 KiB
Java
package com.gxwebsoft.apps.service.impl;
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.gxwebsoft.apps.mapper.EquipmentMapper;
|
|
import com.gxwebsoft.apps.service.EquipmentService;
|
|
import com.gxwebsoft.apps.entity.Equipment;
|
|
import com.gxwebsoft.apps.param.EquipmentParam;
|
|
import com.gxwebsoft.common.core.web.PageParam;
|
|
import com.gxwebsoft.common.core.web.PageResult;
|
|
import com.gxwebsoft.common.system.entity.User;
|
|
import com.gxwebsoft.common.system.service.UserService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 设备管理Service实现
|
|
*
|
|
* @author 科技小王子
|
|
* @since 2022-11-30 02:11:16
|
|
*/
|
|
@Slf4j
|
|
@Service
|
|
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {
|
|
@Resource
|
|
private UserService userService;
|
|
|
|
@Resource
|
|
private RestTemplate restTemplate;
|
|
|
|
@Override
|
|
public PageResult<Equipment> pageRel(EquipmentParam param) {
|
|
PageParam<Equipment, EquipmentParam> page = new PageParam<>(param);
|
|
page.setDefaultOrder("create_time desc");
|
|
List<Equipment> list = baseMapper.selectPageRel(page, param);
|
|
|
|
Set<Integer> touziUserIds = list.stream().map(Equipment::getTouziUserId).collect(Collectors.toSet());
|
|
// List<User> touziUserList = userService.lambdaQuery().in(User::getUserId, touziUserIds).list();
|
|
Map<Integer, User> touziUserCollect = null;
|
|
if(CollectionUtil.isNotEmpty(touziUserIds)){
|
|
List<User> touziUserList = userService.listByIds(touziUserIds);
|
|
if(CollectionUtil.isNotEmpty(touziUserList)){
|
|
touziUserCollect = touziUserList.stream().collect(Collectors.toMap(User::getUserId, e->e));
|
|
}
|
|
}
|
|
|
|
// 查询绑定电池的用户
|
|
for (Equipment equipment : list) {
|
|
// 查询状态
|
|
try {
|
|
ResponseEntity<JSONObject> entity = restTemplate.getForEntity("https://battery.zfdliot.com/api/battery/status?battery_sn=" + equipment.getEquipmentCode(), JSONObject.class);
|
|
JSONObject body = entity.getBody();
|
|
Integer code = body.getInteger("code");
|
|
JSONObject data = body.getJSONObject("data");
|
|
if(null != data){
|
|
equipment.setBms(data.getString("bms_zt"));
|
|
equipment.setBatteryPower(data.getString("sydl"));
|
|
equipment.setTotalVoltage(data.getString("sbdy"));
|
|
equipment.setGps(data.getString("gps_xh"));
|
|
equipment.setGsm(data.getString("gsm_xh"));
|
|
equipment.setWorkingStatus(data.getString("sbzt"));
|
|
}
|
|
else {
|
|
log.warn("获取电池信息返回失败!{}", JSON.toJSONString(body));
|
|
}
|
|
|
|
// System.out.println(body);
|
|
} catch (Exception e) {
|
|
// e.printStackTrace();
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
if (!equipment.getUserId().equals(0)) {
|
|
equipment.setUser(userService.getById(equipment.getUserId()));
|
|
}
|
|
|
|
if(CollectionUtil.isNotEmpty(touziUserCollect) &&
|
|
equipment.getTouziUserId()!= null && !equipment.getTouziUserId().equals(0)) {
|
|
User tUser = touziUserCollect.get(equipment.getTouziUserId());
|
|
if(null != tUser){
|
|
equipment.setTouziUser(tUser);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
return new PageResult<>(list, page.getTotal());
|
|
}
|
|
|
|
@Override
|
|
public List<Equipment> listRel(EquipmentParam param) {
|
|
List<Equipment> list = baseMapper.selectListRel(param);
|
|
// 排序
|
|
PageParam<Equipment, EquipmentParam> page = new PageParam<>();
|
|
page.setDefaultOrder("create_time desc");
|
|
return page.sortRecords(list);
|
|
}
|
|
|
|
@Override
|
|
public Equipment getByIdRel(Integer equipmentId) {
|
|
EquipmentParam param = new EquipmentParam();
|
|
param.setEquipmentId(equipmentId);
|
|
Equipment equipment = baseMapper.selectListRel(param).get(0);
|
|
try {
|
|
ResponseEntity<JSONObject> entity = restTemplate.getForEntity("https://battery.zfdliot.com/api/battery/status?battery_sn=" + equipment.getEquipmentCode(), JSONObject.class);
|
|
JSONObject body = entity.getBody();
|
|
Integer code = body.getInteger("code");
|
|
JSONObject data = body.getJSONObject("data");
|
|
if(null != data){
|
|
equipment.setBms(data.getString("bms_zt"));
|
|
equipment.setBatteryPower(data.getString("sydl"));
|
|
equipment.setTotalVoltage(data.getString("sbdy"));
|
|
equipment.setGps(data.getString("gps_xh"));
|
|
equipment.setGsm(data.getString("gsm_xh"));
|
|
equipment.setWorkingStatus(data.getString("sbzt"));
|
|
}
|
|
else {
|
|
log.warn("获取电池信息返回失败!{}", JSON.toJSONString(body));
|
|
}
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
return equipment;
|
|
}
|
|
|
|
@Override
|
|
public Equipment getByEquipmentCode(String equipmentCode) {
|
|
return query().eq("equipment_code", equipmentCode).one();
|
|
}
|
|
|
|
}
|