package com.xy.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.collections.list.JArrayList;
import com.xy.collections.list.JList;
import com.xy.config.DeviceThreadPoolConfig;
import com.xy.consts.DictConsts;
import com.xy.dto.*;
import com.xy.entity.*;
import com.xy.enums.FileExportType;
import com.xy.error.CommRuntimeException;
import com.xy.mapper.DeviceInfoMapper;
import com.xy.mapper.entity.DeviceInfoQueryPage;
import com.xy.util.ExcelUtils;
import com.xy.util.FileUtils;
import com.xy.utils.*;
import com.xy.utils.enums.DictEnum;
import com.xy.utils.enums.DictSonEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.introspector.MapperFunction;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static com.xy.utils.PlusBeans.*;
/**
*
* 设备-信息 服务实现类
*
*
* @author lijin
* @since 2022-12-23
*/
@Service
@RequiredArgsConstructor
@Api(tags = "设备-信息")
public class DeviceInfoServiceImpl extends ServiceImpl implements DeviceInfoService {
private final DeviceSysinfoServiceImpl deviceSysinfoService;
private final DeviceStatusServiceImpl deviceStatusService;
private final DeviceRegisterServiceImpl deviceRegisterService;
private final DeviceErrorsRecordServiceImpl deviceErrorsRecordService;
private final DeviceDataServiceImpl deviceDataService;
private final DeviceTempRecordsServiceImpl deviceTempRecordsService;
private final DeviceNetRecordServiceImpl deviceNetRecordService;
private final GoodsDeviceService goodsDeviceService;
private final FileExportService fileExportService;
private final RedisService redisService;
private final String keyPrefix = "device:history:";
private final FileUtils fileUtils;
@Override
@ApiOperation("对象查询")
public R obj(DeviceInfoDto.Obj obj) {
//设备信息
LambdaQueryWrapper lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(obj, DeviceInfo.class).build();
List list = list(lambdaQueryWrapper);
if (!Emptys.check(list)) {
return R.ok();
}
DeviceInfoDto.Vo deviceInfo = copy(DeviceInfoDto.Vo.class, list.get(0));
int num = 0;
if (obj.getIsSysinfo()) {
num++;
}
if (obj.getIsStatus()) {
num++;
}
if (obj.getIsRegister()) {
num++;
}
if (num > 0) {
ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, num);
if (obj.getIsSysinfo()) {
execute.execute(() -> {
//系统信息
DeviceSysinfoDto.Vo deviceSysinfo = deviceSysinfoService.get(new DeviceSysinfoDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
deviceInfo.setDeviceSysinfo(deviceSysinfo);
});
}
if (obj.getIsStatus()) {
execute.execute(() -> {
//状态信息
DeviceStatusDto.Vo deviceStatus = deviceStatusService.obj(new DeviceStatusDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
deviceInfo.setDeviceStatus(deviceStatus);
});
}
if (obj.getIsRegister()) {
execute.execute(() -> {
//注册信息
DeviceRegisterDto.Vo deviceRegister = deviceRegisterService.obj(new DeviceRegisterDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
deviceInfo.setDeviceRegister(deviceRegister);
});
}
execute.end();
}
return R.ok(deviceInfo);
}
@Override
@ApiOperation("设备访问历史添加")
public R history(DeviceInfoDto.Obj obj) {
//获取字典
SysDictRedis sysDictRedis = SysDictUtils.get(DictEnum.DATA_CLEAR_SIZE.getKey(), "device_history_twig");
Integer value = Integer.valueOf(sysDictRedis.getValue());
//获取redis
String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
List list = redisService.getList(key);
list.add(0, String.valueOf(obj.getDeviceId()));
//去重
List redisList = new ArrayList<>();
JList comparing = new JArrayList<>(list).comparing();
if (comparing.size() > value) {
for (int i = 0; i < value; i++) {
redisList.add(comparing.get(i));
}
} else {
redisList = comparing;
}
redisService.removeList(key);
redisService.setList(key, redisList);
return R.ok();
}
@Override
@ApiOperation("开门检测")
public R checkOpenDoor(DeviceInfoDto.Obj obj) {
DeviceInfoDto.Vo deviceInfo = obj(new DeviceInfoDto.Obj()
.setDeviceId(obj.getDeviceId())
.setIsStatus(true)
).getData();
if (deviceInfo == null) {
return R.fail("设备不存在");
}
DeviceStatusDto.Vo deviceStatus = deviceInfo.getDeviceStatus();
check(deviceInfo.getFreezeStatus(), 2, "设备已冻结");
SysDictRedis sysDictRedis = SysDictUtils.get(DictEnum.DEVICE_FAULT_LEVEL_PAY_THRESHOLD.getKey(), DictSonEnum.DEVICE_FAULT_LEVEL_PAY_THRESHOLD_NOT_PAY.getKey());
if (deviceInfo.getFaultLevel() >= Integer.valueOf(sysDictRedis.getValue())) {
return R.fail("设备故障");
}
check(deviceStatus.getNetState(), 2, "设备已离线");
check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getDoorStateR() : deviceStatus.getDoorStateL(), 2, "设备正在使用中,请稍后");
check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getDeviceStateR() : deviceStatus.getDeviceStateL(), 2, "设备已锁机");
return R.ok();
}
@PostMapping("historyList")
@ApiOperation("设备访问历史查询")
public R> historyList() {
//获取redis
String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
List deviceIds = redisService.getList(key);
if (!Emptys.check(deviceIds)) {
return R.ok();
}
//查询数据库
List list = list(new LambdaQueryWrapper().in(DeviceInfo::getDeviceId, deviceIds));
return R.ok(copy(DeviceInfoDto.Vo.class, list));
}
@ApiOperation("修改")
@PostMapping("update")
public R update(@RequestBody @Validated DeviceInfoDto.Update update) {
DeviceInfo deviceInfo = copy(DeviceInfo.class, update);
updateById(deviceInfo);
return R.ok();
}
@Override
@ApiOperation("更新商户线路")
public R updateLine(@RequestBody @Validated DeviceInfoDto.UpdateLine updateLine) {
LambdaUpdateWrapper luw = new LambdaUpdateWrapper().eq(DeviceInfo::getMercId, updateLine.getMercId());
//绑定线路,更换线路
if (DeviceInfoDto.UPDATE.equals(updateLine.getType())) {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setPlaceLineId(updateLine.getPlaceLineId());
luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
baseMapper.update(deviceInfo, luw);
}
//删除线路
if (DeviceInfoDto.DEL.equals(updateLine.getType())) {
luw.eq(DeviceInfo::getPlaceLineId, updateLine.getWherePlaceLineId());
luw.set(DeviceInfo::getPlaceLineId, null);
baseMapper.update(null, luw);
}
//解绑线路 设置线路ID为null
if (DeviceInfoDto.CLEAR.equals(updateLine.getType())) {
luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
luw.set(DeviceInfo::getPlaceLineId, null);
baseMapper.update(null, luw);
}
return R.ok();
}
@Override
@ApiOperation("更新商户点位")
public R updatePlace(@RequestBody @Validated DeviceInfoDto.UpdatePlace updatePlace) {
LambdaUpdateWrapper luw = new LambdaUpdateWrapper().eq(DeviceInfo::getMercId, updatePlace.getMercId());
//绑定点位,更换点位
if (DeviceInfoDto.UPDATE.equals(updatePlace.getType())) {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setPlaceId(updatePlace.getPlaceId());
luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
baseMapper.update(deviceInfo, luw);
}
//删除点位
if (DeviceInfoDto.DEL.equals(updatePlace.getType())) {
luw.eq(DeviceInfo::getPlaceId, updatePlace.getWherePlaceId());
luw.set(DeviceInfo::getPlaceId, null);
baseMapper.update(null, luw);
}
//解绑点位 设置点位ID为null
if (DeviceInfoDto.CLEAR.equals(updatePlace.getType())) {
luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
luw.set(DeviceInfo::getPlaceId, null);
baseMapper.update(null, luw);
}
return R.ok();
}
@PostMapping("page")
@ApiOperation("分页查询")
public R> page(@RequestBody DeviceInfoDto.Page page) {
return R.ok(queryPage(page));
}
@ApiOperation("导出设备列表")
@PostMapping("exportDevices")
public void exportDevices(HttpServletResponse response, @RequestBody @Valid DeviceInfoDto.Page page) throws IOException {
PageBean pageBean = queryPage(page);
List records = pageBean.getRecords();
List deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
// 输出
ExcelUtils.write(response, "设备列表.xls", "设备列表", DeviceInfoDto.DeviceExcelVO.class, deviceExcelVOS);
}
@ApiOperation("导出设备(异步)")
@PostMapping("exportDevices/async")
public void exportDevicesAsync(@RequestBody @Validated DeviceInfoDto.Page page) {
PageBean pageBean = queryPage(page);
List records = pageBean.getRecords();
List deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
//异步导出参数封装
ExcelDTO excelDTO = new ExcelDTO<>();
excelDTO.setData(deviceExcelVOS);
excelDTO.setHead(DeviceInfoDto.DeviceExcelVO.class);
excelDTO.setSheetName(FileExportType.DEVICE_INFO.getDescription());
excelDTO.setFileExportType(FileExportType.DEVICE_INFO);
//执行导出
fileExportService.exportExcelAsync(excelDTO);
}
@PostMapping("nearbyPage")
@ApiOperation("附近设备分页查询")
public R> nearbyPage(@RequestBody DeviceInfoDto.Page page) {
if (!Emptys.check(page.getLon()) || !Emptys.check(page.getLat())) {
throw new CommRuntimeException("经纬度不能为空");
}
return R.ok(queryPage(page));
}
@ApiOperation(value = "商户设备授权", hidden = true)
@Override
@Transactional(rollbackFor = Exception.class)
public R mercDeviceAuth(DeviceInfoDto.MercDeviceAuthDto auth) {
Long mercId = auth.getMercId();
String mercCode = auth.getMercCode();
Long algorithmId = auth.getAlgorithmId();
String mercName = auth.getMercName();
//商户最终设备列表
List deviceIds = auth.getDeviceIds();
List devices = getDevicesByMercId(mercId);
//取消商户设备授权
if (CollUtil.isEmpty(deviceIds)) {
if (CollUtil.isEmpty(devices)) {
return R.ok(Boolean.TRUE);
}
}
//更新商户设备授权
List deviceInfos = this.listByIds(deviceIds);
for (DeviceInfo deviceInfo : deviceInfos) {
int refMercId = deviceInfo.getMercId().intValue();
// 只有解绑后,才能给顶级商户授权
if (refMercId != -1 && refMercId != mercId.intValue()) {
StrBuilder sb = StrBuilder.create();
String errMsg = sb.append("设备[")
.append(deviceInfo.getDeviceId())
.append("]")
.append("已被商户[")
.append(deviceInfo.getMercName())
.append("]绑定,请先进行解绑!")
.toString();
//已关联别商户
return R.fail(errMsg, Boolean.FALSE);
}
//绑定关系
deviceInfo.setMercId(mercId).setMercCode(mercCode).setAlgorithmId(algorithmId).setMercName(mercName);
}
saveOrUpdateBatch(deviceInfos);
// //原来存在的设备关系,不在最终设备列表中的移除
// if (CollUtil.isNotEmpty(devices)) {
// List oldIds = new ArrayList<>();
// List removeIds = new ArrayList<>();
// devices.forEach(device -> oldIds.add(device.getDeviceId()));
// oldIds.forEach(deviceId -> {
// //不在最终设备列表中的待移除
// if (!deviceIds.contains(deviceId)) {
// removeIds.add(deviceId);
// }
// });
// if (CollUtil.isNotEmpty(removeIds)) {
// List removeList = this.listByIds(removeIds);
// removeMerRefDevices(removeList, parentId);
// }
// }
return R.ok(Boolean.TRUE);
}
/**
* 解绑机器 回收
*
* @param dto
* @return
*/
@Override
public R unBindMercDevice(DeviceInfoDto.MercDeviceUnBindDto dto) {
List deviceInfos = this.listByIds(dto.getDeviceIds());
return R.ok(removeMerRefDevicesToTopMerc(deviceInfos));
}
/**
* 回收机器
*
* @param deviceInfos
* @return
*/
private Boolean removeMerRefDevicesToTopMerc(List deviceInfos) {
if (CollUtil.isNotEmpty(deviceInfos)) {
deviceInfos.forEach(deviceInfo -> {
//回收
deviceInfo.setMercId(-1L);
deviceInfo.setMercDeviceCode(StrUtil.EMPTY);
deviceInfo.setMercName(StrUtil.EMPTY);
deviceInfo.setMercCode(StrUtil.EMPTY);
});
//批量更新
return updateBatchById(deviceInfos);
}
return Boolean.FALSE;
}
/**
* 批量移除商户设备绑定关系
*
* @param deviceInfos
* @return
*/
private Boolean removeMerRefDevices(List deviceInfos, Long parentId) {
if (CollUtil.isNotEmpty(deviceInfos) && parentId != null) {
deviceInfos.forEach(deviceInfo -> {
//非顶级兴元商户,解绑后,机器归父商户
if (parentId != 1) {
deviceInfo.setMercId(parentId);
}
//一级商户,解绑后,直接释放
if (parentId == 0) {
//兴元等级 0 释放关系
deviceInfo.setMercId(-1L);
}
deviceInfo.setMercName(StrUtil.EMPTY);
deviceInfo.setMercCode(StrUtil.EMPTY);
});
//批量更新
return updateBatchById(deviceInfos);
}
return Boolean.FALSE;
}
@ApiOperation("集合查询")
@Override
public R> list(DeviceInfoDto.ListDto dto) {
List list = list(new LambdaQueryWrapper().in(CollUtil.isNotEmpty(dto.getDeviceIds()), DeviceInfo::getDeviceId, dto.getDeviceIds()));
return R.ok(copy(DeviceInfoDto.Vo.class, list));
}
@Override
@ApiOperation("通用集合查询")
public R> listCommon(DeviceInfoDto.ListCommon dto) {
String deviceSearch = dto.getDeviceSearch();
QueryWrapper queryWrapper = new MybatisPlusQuery().eqWrapper(dto.getVo(), DeviceInfo.class).buildQW();
List placeLineIds = dto.getPlaceLineIds();
List deviceIds = dto.getDeviceIds();
List columnList = dto.getColumnList();
if (CollUtil.isNotEmpty(placeLineIds)) {
queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getPlaceLineId), placeLineIds);
}
if (StrUtil.isNotEmpty(deviceSearch)) {
queryWrapper.and(wrapper -> wrapper.likeRight(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceName), deviceSearch).or()
.eq(LambdaUtils.getUnderlineCaseName(DeviceInfo::getMercDeviceCode), deviceSearch));
}
if (CollUtil.isNotEmpty(columnList)) {
queryWrapper.select(columnList.stream().toArray(String[]::new));
}
if (CollUtil.isNotEmpty(deviceIds)) {
queryWrapper.eq(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceIds);
}
return R.ok(copy(DeviceInfoDto.Vo.class, list(queryWrapper)));
}
@Override
@ApiOperation("商户设备算法列表")
public R> mercAlgorithmIdList(DeviceInfoDto.MercAlgorithmIdListDto dto) {
String algorithmIdStr = LambdaUtils.getProperty(DeviceInfo::getAlgorithmId);
String mercStr = StringTools.humpToLine(LambdaUtils.getProperty(DeviceInfo::getMercId));
QueryWrapper lqw = new QueryWrapper()
.isNotNull(StringTools.humpToLine(algorithmIdStr))
.eq(mercStr, dto.getMercId())
.select(String.format("DISTINCT (%s) as %s", StringTools.humpToLine(algorithmIdStr), algorithmIdStr));
List list = listObjs(lqw, (MapperFunction