DeviceInfoServiceImpl.java 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. package com.xy.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.collection.ListUtil;
  5. import cn.hutool.core.date.DatePattern;
  6. import cn.hutool.core.date.DateTime;
  7. import cn.hutool.core.date.DateUtil;
  8. import cn.hutool.core.map.MapUtil;
  9. import cn.hutool.core.text.StrBuilder;
  10. import cn.hutool.core.util.BooleanUtil;
  11. import cn.hutool.core.util.NumberUtil;
  12. import cn.hutool.core.util.ObjectUtil;
  13. import cn.hutool.core.util.StrUtil;
  14. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  15. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  16. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  17. import com.baomidou.mybatisplus.core.metadata.IPage;
  18. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import com.xy.collections.list.JArrayList;
  21. import com.xy.collections.list.JList;
  22. import com.xy.config.DeviceThreadPoolConfig;
  23. import com.xy.consts.DictConsts;
  24. import com.xy.device.EnumDeviceDataType;
  25. import com.xy.device.EnumDeviceFaultLevelPayThreshold;
  26. import com.xy.device.EnumDeviceOnlineStatus;
  27. import com.xy.device.EnumQualityMercSets;
  28. import com.xy.dto.*;
  29. import com.xy.dto.api.biz.ContainerAddDTO;
  30. import com.xy.dto.be.MercDto;
  31. import com.xy.entity.*;
  32. import com.xy.enums.FileExportType;
  33. import com.xy.error.CommRuntimeException;
  34. import com.xy.mapper.DeviceInfoMapper;
  35. import com.xy.mapper.entity.DeviceInfoQueryPage;
  36. import com.xy.service.be.MercFeignService;
  37. import com.xy.sys.EnumDataClearSize;
  38. import com.xy.util.ExcelUtils;
  39. import com.xy.utils.*;
  40. import com.xy.utils.Enum.AlgorithmTypeEnum;
  41. import com.xy.utils.enums.DeviceActiveStateEnum;
  42. import com.xy.utils.enums.DeviceLockState;
  43. import com.xy.utils.enums.DeviceNetSateType;
  44. import com.xy.utils.enums.DeviceTypeEnum;
  45. import io.swagger.annotations.Api;
  46. import io.swagger.annotations.ApiOperation;
  47. import jodd.introspector.MapperFunction;
  48. import lombok.RequiredArgsConstructor;
  49. import org.springframework.stereotype.Service;
  50. import org.springframework.transaction.annotation.Transactional;
  51. import org.springframework.validation.annotation.Validated;
  52. import org.springframework.web.bind.annotation.PostMapping;
  53. import org.springframework.web.bind.annotation.RequestBody;
  54. import javax.servlet.http.HttpServletResponse;
  55. import javax.validation.Valid;
  56. import java.io.IOException;
  57. import java.math.BigDecimal;
  58. import java.time.LocalDateTime;
  59. import java.util.*;
  60. import java.util.stream.Collectors;
  61. import static com.xy.utils.PlusBeans.*;
  62. /**
  63. * <p>
  64. * 设备-信息 服务实现类
  65. * </p>
  66. *
  67. * @author lijin
  68. * @since 2022-12-23
  69. */
  70. @Service
  71. @RequiredArgsConstructor
  72. @Api(tags = "设备-信息")
  73. public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements DeviceInfoService {
  74. private final CloudWalkApiService cloudWalkApiService;
  75. /**
  76. * 质检商户code
  77. */
  78. public static final String QA_MERC_CODE = "10001";
  79. private final MercFeignService mercFeignService;
  80. private final DeviceSysinfoServiceImpl deviceSysinfoService;
  81. private final DeviceStatusServiceImpl deviceStatusService;
  82. private final DeviceRegisterServiceImpl deviceRegisterService;
  83. private final DeviceEventMsgServiceImpl deviceEventMsgService;
  84. private final DeviceDataServiceImpl deviceDataService;
  85. private final DeviceTempRecordsServiceImpl deviceTempRecordsService;
  86. private final DeviceNetRecordServiceImpl deviceNetRecordService;
  87. private final GoodsDeviceService goodsDeviceService;
  88. private final FileExportService fileExportService;
  89. private final RedisService<String> redisService;
  90. private final GoodsService goodsService;
  91. private final OrderRefundService refundService;
  92. private final AlipayDeviceService alipayDeviceService;
  93. private final String keyPrefix = "device:history:";
  94. @Override
  95. @ApiOperation("设备在线数查询")
  96. public R<Map<Long, DeviceInfoDto.NetStateCountVo>> netStateCount(DeviceInfoDto.NetStateCountDto dto) {
  97. List<DeviceInfoDto.NetStateCount> netStateCountList = baseMapper.netStateCount(dto.getMercIdList());
  98. List<Long> netStateCountMercList = netStateCountList.stream().map(DeviceInfoDto.NetStateCount::getMercId).distinct().collect(Collectors.toList());
  99. Map<Long, DeviceInfoDto.NetStateCountVo> voMap = new HashMap<>();
  100. List<Long> mercIdList = Emptys.check(dto.getMercIdList()) ? dto.getMercIdList() : netStateCountMercList;
  101. if (!Emptys.check(mercIdList)) {
  102. return R.ok();
  103. }
  104. for (Long mercId : mercIdList) {
  105. Integer count = 0;
  106. Integer offline = 0;
  107. DeviceInfoDto.NetStateCountVo vo = new DeviceInfoDto.NetStateCountVo().setMercId(mercId);
  108. for (DeviceInfoDto.NetStateCount i : netStateCountList) {
  109. if (Objects.equals(i.getMercId(), mercId)) {
  110. count += i.getCount();
  111. if ("1".equals(i.getNetState())) {
  112. vo.setOnLineCount(i.getCount());
  113. } else {
  114. offline += i.getCount();
  115. }
  116. }
  117. }
  118. vo.setCount(count).setOffLineCount(offline);
  119. voMap.put(mercId, vo);
  120. }
  121. return R.ok(voMap);
  122. }
  123. @Override
  124. @ApiOperation("分页")
  125. public R<PageBean<DeviceInfoDto.Vo>> pageSingle(DeviceInfoDto.PageSingle dto) {
  126. PageBean pageBean = dto.getPage();
  127. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  128. .build();
  129. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  130. return R.ok(toPageBean(DeviceInfoDto.Vo.class, iPage));
  131. }
  132. @Override
  133. @ApiOperation("对象查询")
  134. public R<DeviceInfoDto.Vo> obj(DeviceInfoDto.Obj obj) {
  135. //设备信息
  136. LambdaQueryWrapper<DeviceInfo> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(obj, DeviceInfo.class).build();
  137. List<DeviceInfo> list = list(lambdaQueryWrapper);
  138. if (!Emptys.check(list)) {
  139. return R.ok();
  140. }
  141. DeviceInfoDto.Vo deviceInfo = copy(DeviceInfoDto.Vo.class, list.get(0));
  142. int num = 0;
  143. if (obj.getIsSysinfo()) {
  144. num++;
  145. }
  146. if (obj.getIsStatus()) {
  147. num++;
  148. }
  149. if (obj.getIsRegister()) {
  150. num++;
  151. }
  152. if (num > 0) {
  153. ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, num);
  154. if (obj.getIsSysinfo()) {
  155. execute.execute(() -> {
  156. //系统信息
  157. DeviceSysinfoDto.Vo deviceSysinfo = deviceSysinfoService.get(new DeviceSysinfoDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  158. deviceInfo.setDeviceSysinfo(deviceSysinfo);
  159. });
  160. }
  161. if (obj.getIsStatus()) {
  162. execute.execute(() -> {
  163. //状态信息
  164. DeviceStatusDto.Vo deviceStatus = deviceStatusService.obj(new DeviceStatusDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  165. deviceInfo.setDeviceStatus(deviceStatus);
  166. });
  167. }
  168. if (obj.getIsRegister()) {
  169. execute.execute(() -> {
  170. //注册信息
  171. DeviceRegisterDto.Vo deviceRegister = deviceRegisterService.obj(new DeviceRegisterDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  172. deviceInfo.setDeviceRegister(deviceRegister);
  173. });
  174. }
  175. execute.end();
  176. }
  177. return R.ok(deviceInfo);
  178. }
  179. @ApiOperation("反显设备名称")
  180. @Override
  181. public R<Map<Long, String>> getDeviceNameList(DeviceInfoDto.DeviceIdDto dto) {
  182. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<DeviceInfo>()
  183. .in(DeviceInfo::getDeviceId, dto.getDeviceId())
  184. .select(DeviceInfo::getDeviceId, DeviceInfo::getDeviceName);
  185. List<DeviceInfo> deviceInfoList = list(lqw);
  186. return R.ok(deviceInfoList.stream().collect(Collectors.toMap(DeviceInfo::getDeviceId, i -> Optional.ofNullable(i.getDeviceName()).orElse(""))));
  187. }
  188. @Override
  189. public R<DeviceSysinfoDto.Vo> getDeviceSysinfo(DeviceSysinfoDto.DeviceSysInfo dto) {
  190. String dtoDeviceSN = dto.getDeviceSN();
  191. DeviceSysinfo one = deviceSysinfoService.getOne(Wrappers.<DeviceSysinfo>lambdaQuery().eq(DeviceSysinfo::getDeviceSn, dtoDeviceSN));
  192. return R.ok(BeanUtil.copyProperties(one, DeviceSysinfoDto.Vo.class));
  193. }
  194. @Override
  195. @ApiOperation("设备访问历史添加")
  196. public R history(DeviceInfoDto.Obj obj) {
  197. //获取字典
  198. SysDictRedis sysDictRedis = SysDictUtils.get(EnumDataClearSize.Code.CODE.getCode(), EnumDataClearSize.DEVICE_HISTORY_TWIG.getCode());
  199. Integer value = Integer.valueOf(sysDictRedis.getValue());
  200. //获取redis
  201. String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
  202. List<String> list = redisService.getList(key);
  203. list.add(0, String.valueOf(obj.getDeviceId()));
  204. //去重
  205. List<String> redisList = new ArrayList<>();
  206. JList<String> comparing = new JArrayList<>(list).comparing();
  207. if (comparing.size() > value) {
  208. for (int i = 0; i < value; i++) {
  209. redisList.add(comparing.get(i));
  210. }
  211. } else {
  212. redisList = comparing;
  213. }
  214. redisService.removeList(key);
  215. redisService.setList(key, redisList);
  216. return R.ok();
  217. }
  218. @Override
  219. @ApiOperation("开门检测")
  220. public R<DeviceInfoDto.Vo> checkOpenDoor(DeviceInfoDto.Obj obj) {
  221. DeviceInfoDto.Vo deviceInfo = obj(new DeviceInfoDto.Obj()
  222. .setDeviceId(obj.getDeviceId())
  223. .setIsStatus(true)
  224. ).getData();
  225. if (deviceInfo == null) {
  226. return R.fail("设备不存在");
  227. }
  228. DeviceStatusDto.Vo deviceStatus = deviceInfo.getDeviceStatus();
  229. SysDictRedis sysDictRedis = SysDictUtils.get(EnumDeviceFaultLevelPayThreshold.Code.CODE.getCode(), EnumDeviceFaultLevelPayThreshold.NOT_PAY.getCode());
  230. if (deviceInfo.getFaultLevel() >= Integer.valueOf(sysDictRedis.getValue())) {
  231. return R.fail("设备故障");
  232. }
  233. check(deviceStatus.getNetState(), 2, "设备已离线");
  234. check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getDoorStateR() : deviceStatus.getDoorStateL(), 1, "设备正在使用中,请稍后");
  235. //质检用户不检查
  236. SysDictRedis qualitySets = SysDictUtils.get(EnumQualityMercSets.Code.CODE.getCode(), EnumQualityMercSets.MERC_CODE.getCode());
  237. if (!qualitySets.getValue().equals(deviceInfo.getMercCode())) {
  238. check(deviceInfo.getFreezeStatus(), 2, "设备已冻结");
  239. check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getLockStateR() : deviceStatus.getLockStateL(), 2, "设备已锁机");
  240. }
  241. return R.ok(deviceInfo);
  242. }
  243. @PostMapping("historyList")
  244. @ApiOperation("设备访问历史查询")
  245. public R<List<DeviceInfoDto.Vo>> historyList() {
  246. //获取redis
  247. String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
  248. List<String> deviceIds = redisService.getList(key);
  249. if (!Emptys.check(deviceIds)) {
  250. return R.ok();
  251. }
  252. //查询数据库
  253. List<DeviceInfo> list = list(new LambdaQueryWrapper<DeviceInfo>().in(DeviceInfo::getDeviceId, deviceIds));
  254. return R.ok(copy(DeviceInfoDto.Vo.class, list));
  255. }
  256. @ApiOperation("修改")
  257. @PostMapping("update")
  258. public R update(@RequestBody @Validated DeviceInfoDto.Update update) {
  259. DeviceInfo deviceInfo = copy(DeviceInfo.class, update)
  260. .setUpdateTime(LocalDateTime.now());
  261. updateById(deviceInfo);
  262. DeviceInfoDto.Vo device = R.feignCheckData(this.obj(new DeviceInfoDto.Obj().setDeviceId(update.getDeviceId()).setIsSysinfo(true)));
  263. Integer deviceType = device.getDeviceType();
  264. if (ObjectUtil.equals(DeviceTypeEnum.TYPE5.getCode(), deviceType)) {
  265. DeviceUpdateDTO deviceUpdateDTO = new DeviceUpdateDTO();
  266. if (StrUtil.isNotEmpty(update.getDeviceName())) {
  267. deviceUpdateDTO.setDeviceName(update.getDeviceName());
  268. }
  269. String deviceSn = device.getDeviceSysinfo().getDeviceSn();
  270. if (deviceUpdateDTO != null) {
  271. deviceUpdateDTO.setTerminalId(String.valueOf(update.getDeviceId()));
  272. deviceUpdateDTO.setBoardSn(deviceSn);
  273. alipayDeviceService.deviceUpdate(deviceUpdateDTO);
  274. }
  275. }
  276. return R.ok();
  277. }
  278. @ApiOperation("批量修改")
  279. @PostMapping("updateBatch")
  280. public R updateBatch(@RequestBody List<DeviceInfoDto.Update> updates) {
  281. LocalDateTime now = LocalDateTime.now();
  282. List<DeviceInfo> deviceInfos = new ArrayList<>(updates.size());
  283. updates.forEach(update -> deviceInfos.add(copy(DeviceInfo.class, update).setUpdateTime(now)));
  284. updateBatchById(deviceInfos);
  285. return R.ok();
  286. }
  287. @Override
  288. @ApiOperation("更新商户线路")
  289. public R updateLine(@RequestBody @Validated DeviceInfoDto.UpdateLine updateLine) {
  290. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>().eq(DeviceInfo::getMercId, updateLine.getMercId());
  291. //绑定线路,更换线路
  292. if (DeviceInfoDto.UPDATE.equals(updateLine.getType())) {
  293. DeviceInfo deviceInfo = new DeviceInfo();
  294. deviceInfo.setPlaceLineId(updateLine.getPlaceLineId());
  295. luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
  296. baseMapper.update(deviceInfo, luw);
  297. }
  298. //删除线路
  299. if (DeviceInfoDto.DEL.equals(updateLine.getType())) {
  300. luw.eq(DeviceInfo::getPlaceLineId, updateLine.getWherePlaceLineId());
  301. luw.set(DeviceInfo::getPlaceLineId, null);
  302. baseMapper.update(null, luw);
  303. }
  304. //解绑线路 设置线路ID为null
  305. if (DeviceInfoDto.CLEAR.equals(updateLine.getType())) {
  306. luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
  307. luw.set(DeviceInfo::getPlaceLineId, null);
  308. baseMapper.update(null, luw);
  309. }
  310. return R.ok();
  311. }
  312. @Override
  313. @ApiOperation("更新商户点位")
  314. public R updatePlace(@RequestBody @Validated DeviceInfoDto.UpdatePlace updatePlace) {
  315. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>().eq(DeviceInfo::getMercId, updatePlace.getMercId());
  316. //绑定点位,更换点位
  317. if (DeviceInfoDto.UPDATE.equals(updatePlace.getType())) {
  318. DeviceInfo deviceInfo = new DeviceInfo();
  319. deviceInfo.setPlaceId(updatePlace.getPlaceId());
  320. luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
  321. baseMapper.update(deviceInfo, luw);
  322. }
  323. //删除点位
  324. if (DeviceInfoDto.DEL.equals(updatePlace.getType())) {
  325. luw.eq(DeviceInfo::getPlaceId, updatePlace.getWherePlaceId());
  326. luw.set(DeviceInfo::getPlaceId, null);
  327. baseMapper.update(null, luw);
  328. }
  329. //解绑点位 设置点位ID为null
  330. if (DeviceInfoDto.CLEAR.equals(updatePlace.getType())) {
  331. luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
  332. luw.set(DeviceInfo::getPlaceId, null);
  333. baseMapper.update(null, luw);
  334. }
  335. return R.ok();
  336. }
  337. @ApiOperation("分页查询")
  338. @Override
  339. public R<PageBean<DeviceInfoDto.Vo2>> page(@RequestBody DeviceInfoDto.Page page) {
  340. return R.ok(queryPage(page));
  341. }
  342. @ApiOperation("导出设备列表")
  343. @PostMapping("exportDevices")
  344. public void exportDevices(HttpServletResponse response, @RequestBody @Valid DeviceInfoDto.Page page) throws IOException {
  345. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  346. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  347. List<DeviceInfoDto.DeviceExcelVO> deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
  348. // 输出
  349. ExcelUtils.write(response, "设备列表.xls", "设备列表", DeviceInfoDto.DeviceExcelVO.class, deviceExcelVOS);
  350. }
  351. @ApiOperation("导出设备(异步)")
  352. @PostMapping("exportDevices/async")
  353. public void exportDevicesAsync(@RequestBody @Validated DeviceInfoDto.Page page) {
  354. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  355. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  356. List<DeviceInfoDto.DeviceExcelVO> deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
  357. //异步导出参数封装
  358. ExcelDTO<DeviceInfoDto.DeviceExcelVO> excelDTO = new ExcelDTO<>();
  359. excelDTO.setData(deviceExcelVOS);
  360. excelDTO.setHead(DeviceInfoDto.DeviceExcelVO.class);
  361. excelDTO.setSheetName(FileExportType.DEVICE_INFO.getDescription());
  362. excelDTO.setFileExportType(FileExportType.DEVICE_INFO);
  363. //执行导出
  364. fileExportService.exportExcelAsync(excelDTO);
  365. }
  366. @PostMapping("nearbyPage")
  367. @ApiOperation("附近设备分页查询")
  368. public R<PageBean<DeviceInfoDto.Vo2>> nearbyPage(@RequestBody DeviceInfoDto.Page page) {
  369. if (!Emptys.check(page.getLon()) || !Emptys.check(page.getLat())) {
  370. throw new CommRuntimeException("经纬度不能为空");
  371. }
  372. return R.ok(queryPage(page));
  373. }
  374. @ApiOperation(value = "商户设备授权", hidden = true)
  375. @Override
  376. @Transactional(rollbackFor = Exception.class)
  377. public R<Boolean> mercDeviceAuth(DeviceInfoDto.MercDeviceAuthDto auth) {
  378. Long mercId = auth.getMercId();
  379. String mercCode = auth.getMercCode();
  380. Long algorithmId = auth.getAlgorithmId();
  381. String mercName = auth.getMercName();
  382. //商户最终设备列表
  383. List<Long> deviceIds = auth.getDeviceIds();
  384. List<DeviceInfo> devices = getDevicesByMercId(mercId);
  385. //取消商户设备授权
  386. if (CollUtil.isEmpty(deviceIds)) {
  387. if (CollUtil.isEmpty(devices)) {
  388. return R.ok(Boolean.TRUE);
  389. }
  390. }
  391. //更新商户设备授权
  392. List<DeviceInfo> deviceInfos = this.listByIds(deviceIds);
  393. for (DeviceInfo deviceInfo : deviceInfos) {
  394. Integer deviceType = deviceInfo.getDeviceType();
  395. Long deviceId = deviceInfo.getDeviceId();
  396. if (ObjectUtil.equals(deviceType, DeviceTypeEnum.TYPE5.getCode())) {
  397. //支付宝设备算法
  398. algorithmId = AlgorithmTypeEnum.ALIPAY.getId();
  399. } else {
  400. //非支付宝算法
  401. if (AlgorithmTypeEnum.CLOUD.getId() == algorithmId) {
  402. //云从算法
  403. boolean b = cloudWalkApiService.checkDeviceExist(deviceId);
  404. if (!b) {
  405. //货柜不存在,新增
  406. cloudWalkApiService.containerAdd(new ContainerAddDTO().setContainerCode(String.valueOf(deviceId)));
  407. }
  408. }
  409. }
  410. int refMercId = deviceInfo.getMercId().intValue();
  411. String refMercCode = deviceInfo.getMercCode();
  412. // 只有解绑后,才能给顶级商户授权
  413. if (refMercId != -1 && refMercId != mercId.intValue()) {
  414. StrBuilder sb = StrBuilder.create();
  415. String errMsg = sb.append("设备[")
  416. .append(deviceInfo.getDeviceId())
  417. .append("]")
  418. .append("已被商户[")
  419. .append(deviceInfo.getMercName())
  420. .append("]绑定,请先进行解绑!")
  421. .toString();
  422. //非质检商户需要进行判断,质检商户跳过
  423. if (!QA_MERC_CODE.equals(refMercCode)) {
  424. //已关联别商户
  425. return R.fail(errMsg, Boolean.FALSE);
  426. }
  427. }
  428. //绑定关系
  429. deviceInfo.setMercId(mercId).setMercCode(mercCode).setAlgorithmId(algorithmId).setMercName(mercName);
  430. }
  431. saveOrUpdateBatch(deviceInfos);
  432. // //原来存在的设备关系,不在最终设备列表中的移除
  433. // if (CollUtil.isNotEmpty(devices)) {
  434. // List<Long> oldIds = new ArrayList<>();
  435. // List<Long> removeIds = new ArrayList<>();
  436. // devices.forEach(device -> oldIds.add(device.getDeviceId()));
  437. // oldIds.forEach(deviceId -> {
  438. // //不在最终设备列表中的待移除
  439. // if (!deviceIds.contains(deviceId)) {
  440. // removeIds.add(deviceId);
  441. // }
  442. // });
  443. // if (CollUtil.isNotEmpty(removeIds)) {
  444. // List<DeviceInfo> removeList = this.listByIds(removeIds);
  445. // removeMerRefDevices(removeList, parentId);
  446. // }
  447. // }
  448. return R.ok(Boolean.TRUE);
  449. }
  450. /**
  451. * 解绑机器 回收
  452. *
  453. * @param dto
  454. * @return
  455. */
  456. @Override
  457. public R<Boolean> unBindMercDevice(DeviceInfoDto.MercDeviceUnBindDto dto) {
  458. List<DeviceInfo> deviceInfos = this.listByIds(dto.getDeviceIds());
  459. return R.ok(removeMerRefDevicesToTopMerc(deviceInfos));
  460. }
  461. /**
  462. * 回收机器
  463. *
  464. * @param deviceInfos
  465. * @return
  466. */
  467. private Boolean removeMerRefDevicesToTopMerc(List<DeviceInfo> deviceInfos) {
  468. MercDto.Vo mercCheck = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setMercCode(QA_MERC_CODE)));
  469. if (CollUtil.isNotEmpty(deviceInfos)) {
  470. deviceInfos.forEach(deviceInfo -> {
  471. //回收
  472. deviceInfo.setMercId(mercCheck.getId());
  473. deviceInfo.setMercDeviceCode(StrUtil.EMPTY);
  474. deviceInfo.setMercName(mercCheck.getName());
  475. deviceInfo.setMercCode(QA_MERC_CODE);
  476. });
  477. //批量更新
  478. return updateBatchById(deviceInfos);
  479. }
  480. return Boolean.FALSE;
  481. }
  482. /**
  483. * 批量移除商户设备绑定关系
  484. *
  485. * @param deviceInfos
  486. * @return
  487. */
  488. private Boolean removeMerRefDevices(List<DeviceInfo> deviceInfos, Long parentId) {
  489. if (CollUtil.isNotEmpty(deviceInfos) && parentId != null) {
  490. deviceInfos.forEach(deviceInfo -> {
  491. //非顶级兴元商户,解绑后,机器归父商户
  492. if (parentId != 1) {
  493. MercDto.Vo mercParent = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setId(parentId)));
  494. if (mercParent != null) {
  495. deviceInfo.setMercId(mercParent.getId());
  496. deviceInfo.setMercName(mercParent.getName());
  497. deviceInfo.setMercCode(mercParent.getMercCode());
  498. } else {
  499. deviceInfo.setMercId(-1L);
  500. deviceInfo.setMercName(StrUtil.EMPTY);
  501. deviceInfo.setMercCode(StrUtil.EMPTY);
  502. }
  503. }
  504. //一级商户,解绑后,直接释放
  505. if (parentId == 0) {
  506. MercDto.Vo mercCheck = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setMercCode(QA_MERC_CODE)));
  507. if (mercCheck != null) {
  508. deviceInfo.setMercId(mercCheck.getId());
  509. deviceInfo.setMercName(mercCheck.getName());
  510. deviceInfo.setMercCode(mercCheck.getMercCode());
  511. } else {
  512. deviceInfo.setMercId(-1L);
  513. deviceInfo.setMercName(StrUtil.EMPTY);
  514. deviceInfo.setMercCode(StrUtil.EMPTY);
  515. }
  516. }
  517. });
  518. //批量更新
  519. return updateBatchById(deviceInfos);
  520. }
  521. return Boolean.FALSE;
  522. }
  523. @ApiOperation("集合查询")
  524. @Override
  525. public R<List<DeviceInfoDto.Vo>> list(DeviceInfoDto.ListDto dto) {
  526. List<DeviceInfo> list = list(new LambdaQueryWrapper<DeviceInfo>()
  527. .in(CollUtil.isNotEmpty(dto.getDeviceIds()), DeviceInfo::getDeviceId, dto.getDeviceIds())
  528. .eq(Emptys.check(dto.getMercId()), DeviceInfo::getMercId, dto.getMercId())
  529. );
  530. return R.ok(copy(DeviceInfoDto.Vo.class, list));
  531. }
  532. @Override
  533. @ApiOperation("通用集合查询")
  534. public R<List<DeviceInfoDto.Vo>> listCommon(DeviceInfoDto.ListCommon dto) {
  535. String deviceSearch = dto.getDeviceSearch();
  536. DeviceInfoDto.Vo vo = dto.getVo();
  537. QueryWrapper<DeviceInfo> queryWrapper = new MybatisPlusQuery().eqWrapper(vo == null ? new DeviceInfoDto.Vo() : vo, DeviceInfo.class).buildQW();
  538. List<Long> placeLineIds = dto.getPlaceLineIds();
  539. List<Long> deviceIds = dto.getDeviceIds();
  540. List<String> columnList = dto.getColumnList();
  541. if (CollUtil.isNotEmpty(placeLineIds)) {
  542. queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getPlaceLineId), placeLineIds);
  543. }
  544. //fixed
  545. if (StrUtil.isNotEmpty(deviceSearch)) {
  546. queryWrapper.and(wrapper -> wrapper.likeRight(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceName), deviceSearch).or()
  547. .eq(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceSearch));
  548. }
  549. if (CollUtil.isNotEmpty(columnList)) {
  550. queryWrapper.select(columnList.stream().toArray(String[]::new));
  551. }
  552. if (CollUtil.isNotEmpty(deviceIds)) {
  553. queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceIds);
  554. }
  555. return R.ok(copy(DeviceInfoDto.Vo.class, list(queryWrapper)));
  556. }
  557. @Override
  558. @ApiOperation("商户设备算法列表")
  559. public R<List<Long>> mercAlgorithmIdList(DeviceInfoDto.MercAlgorithmIdListDto dto) {
  560. String algorithmIdStr = LambdaUtils.getProperty(DeviceInfo::getAlgorithmId);
  561. String mercStr = StringTools.humpToLine(LambdaUtils.getProperty(DeviceInfo::getMercId));
  562. QueryWrapper<DeviceInfo> lqw = new QueryWrapper<DeviceInfo>()
  563. .isNotNull(StringTools.humpToLine(algorithmIdStr))
  564. .eq(mercStr, dto.getMercId())
  565. .select(String.format("DISTINCT (%s) as %s", StringTools.humpToLine(algorithmIdStr), algorithmIdStr));
  566. List<Long> list = listObjs(lqw, (MapperFunction<Object, Long>) o -> (Long) o);
  567. return R.ok(list);
  568. }
  569. @Override
  570. @ApiOperation("小程序-商户设备首页统计")
  571. public R<DeviceInfoDto.MercHomeStatisticalVO> mercHomeStatistical(DeviceInfoDto.MercHomeQueryDTO dto) {
  572. Long mercId = dto.getMercId();
  573. List<Long> myDeviceIds = dto.getMyDeviceIds();
  574. //初始化数据
  575. DeviceInfoDto.MercHomeStatisticalVO mercHomeStatisticalVO = new DeviceInfoDto.MercHomeStatisticalVO()
  576. .setClosedNum(0).setOfflineNum(0)
  577. .setOnlineNum(0).setOperatingNum(0).setNeedToFillNum(0);
  578. if (CollUtil.isEmpty(myDeviceIds)) {
  579. return R.ok(mercHomeStatisticalVO);
  580. }
  581. List<DeviceInfo> mercDevices = listByIds(myDeviceIds);
  582. if (CollUtil.isEmpty(mercDevices)) {
  583. return R.ok(mercHomeStatisticalVO);
  584. }
  585. //在线、离线
  586. List<DeviceStatus> deviceStatuses = deviceStatusService.listByIds(myDeviceIds);
  587. //分组统计
  588. Map<Integer, Long> countNetstateMap = deviceStatuses.stream().collect(Collectors
  589. .groupingBy(DeviceStatus::getNetState, Collectors.counting()));
  590. Integer onlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.CONNECTED.getCode(), Integer.class);
  591. Integer offlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.DISCONNECT.getCode(), Integer.class);
  592. int onlineNum = countNetstateMap.get(onlineDictValue) == null ? 0 : countNetstateMap.get(onlineDictValue).intValue();
  593. int offlineNum = countNetstateMap.get(offlineDictValue) == null ? 0 : countNetstateMap.get(offlineDictValue).intValue();
  594. mercHomeStatisticalVO.setOnlineNum(onlineNum);
  595. mercHomeStatisticalVO.setOfflineNum(offlineNum);
  596. //锁机、未锁机
  597. Map<Integer, Long> countLockLstateMap = deviceStatuses.stream().collect(Collectors
  598. .groupingBy(DeviceStatus::getLockStateL, Collectors.counting()));
  599. Long lockLStateNum = countLockLstateMap.get(DeviceLockState.LOCK.getCode());
  600. Long unLockLStateNum = countLockLstateMap.get(DeviceLockState.UN_LOCK.getCode());
  601. mercHomeStatisticalVO.setOperatingNum(unLockLStateNum == null ? 0 : unLockLStateNum.intValue());
  602. mercHomeStatisticalVO.setClosedNum(lockLStateNum == null ? 0 : lockLStateNum.intValue());
  603. //待补货
  604. Integer deviceNum = R.feignCheckData(goodsDeviceService.countOutOfStockDevice(new GoodsDeviceDto.CountOutOfStockDevice().setMercId(mercId)));
  605. mercHomeStatisticalVO.setNeedToFillNum(deviceNum);
  606. return R.ok(mercHomeStatisticalVO);
  607. }
  608. @Override
  609. @ApiOperation("小程序-商户设备首页列表")
  610. public R<List<DeviceInfoDto.MercHomeListVO>> mercHomeList(DeviceInfoDto.MercHomeQueryDTO dto) {
  611. Long mercId = dto.getMercId();
  612. String deviceName = dto.getDeviceName();
  613. Long deviceId = dto.getDeviceId();
  614. boolean isQa = false;
  615. if ("393010594508869".equals(String.valueOf(mercId))) {
  616. isQa = true;
  617. } else {
  618. dto.setActiveState("1");
  619. }
  620. Integer busyStatus = dto.getBusyStatus();
  621. Integer onlineStatus = dto.getOnlineStatus();
  622. Integer deviceType = dto.getDeviceType();
  623. //条件查询 在线状态,运营状态,设备类型,
  624. List<Long> deviceIdList = new ArrayList<>();
  625. if (deviceId != null) {
  626. deviceIdList.add(deviceId);
  627. }
  628. if (onlineStatus != null) {
  629. List<DeviceStatus> deviceStatusList = deviceStatusService.list(Wrappers.<DeviceStatus>lambdaQuery()
  630. .eq(onlineStatus != null, DeviceStatus::getNetState, onlineStatus)
  631. );
  632. if (CollUtil.isNotEmpty(deviceStatusList)) {
  633. List<Long> dbDeviceIdList = deviceStatusList.stream().map(DeviceStatus::getDeviceId).collect(Collectors.toList());
  634. deviceIdList.addAll(dbDeviceIdList);
  635. }
  636. }
  637. dto.setDeviceIdList(deviceIdList);
  638. List<DeviceInfoDto.MercHomeCountVO> list = this.baseMapper.merHomeCountList(dto);
  639. if (CollUtil.isEmpty(list)) {
  640. return R.ok(Collections.emptyList());
  641. }
  642. List<DeviceInfoDto.MercHomeListVO> dataList = new ArrayList<>(list.size());
  643. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<>();
  644. //非质检商户才需要激活
  645. lqw.eq(!isQa, DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode());
  646. lqw.eq(mercId != null, DeviceInfo::getMercId, mercId);
  647. lqw.eq(busyStatus != null, DeviceInfo::getBusyState, busyStatus);
  648. lqw.eq(deviceType != null, DeviceInfo::getDeviceType, deviceType);
  649. lqw.in(CollUtil.isNotEmpty(deviceIdList), DeviceInfo::getDeviceId, deviceIdList);
  650. lqw.like(StrUtil.isNotEmpty(deviceName), DeviceInfo::getDeviceName, deviceName).orderByAsc(true, DeviceInfo::getDeviceName, DeviceInfo::getDeviceId);
  651. List<DeviceInfo> deviceInfoList = this.list(lqw);
  652. //未分配线路的设置默认值
  653. deviceInfoList.stream().filter(s -> s.getPlaceLineId() == null).forEach(s -> s.setPlaceLineId(-1L));
  654. //根据线路id分组
  655. Map<Long, List<DeviceInfo>> deviceMap = deviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfo::getPlaceLineId));
  656. for (DeviceInfoDto.MercHomeCountVO v : list) {
  657. DeviceInfoDto.MercHomeListVO vo = new DeviceInfoDto.MercHomeListVO();
  658. Long placeLineId = v.getPlaceLineId();
  659. vo.setDeviceNum(v.getDeviceNum());
  660. vo.setPlaceLineId(v.getPlaceLineId());
  661. //线路下的设备列表
  662. List<DeviceInfoDto.MercHomeDeviceVo> deviceInfos = BeanUtil.copyToList(deviceMap.get(placeLineId), DeviceInfoDto.MercHomeDeviceVo.class);
  663. if (CollUtil.isEmpty(deviceInfos)) {
  664. continue;
  665. }
  666. //设备销售统计
  667. String todayDate = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
  668. List<Long> deviceIds = deviceInfos.stream().map(DeviceInfoDto.MercHomeDeviceVo::getDeviceId).collect(Collectors.toList());
  669. //统计条件:当天
  670. DeviceDataDto.ListDTO deviceDataListDTO = new DeviceDataDto.ListDTO()
  671. .setDeviceIds(deviceIds).setType(EnumDeviceDataType.DAY.getCode()).setDateValue(Integer.valueOf(todayDate)).setMercId(mercId);
  672. List<DeviceDataDto.Vo> deviceDataList = deviceDataService.list(deviceDataListDTO);
  673. Map<Long, DeviceDataDto.Vo> dataMap = new HashMap<>();
  674. if (CollUtil.isNotEmpty(deviceDataList)) {
  675. dataMap = deviceDataList.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDeviceId, d -> d));
  676. }
  677. //设备状态查询
  678. List<DeviceStatusDto.Vo> deviceStatusList = deviceStatusService.list(new DeviceStatusDto.SelectList().setDeviceIds(deviceIds)).getData();
  679. Map<Long, DeviceStatusDto.Vo> datdeviceStatusMap = new HashMap<>();
  680. if (CollUtil.isNotEmpty(deviceStatusList)) {
  681. datdeviceStatusMap = deviceStatusList.stream().collect(Collectors.toMap(DeviceStatusDto.Vo::getDeviceId, d -> d));
  682. }
  683. List<GoodsDeviceDto.Vo> goodsDeviceList = R.feignCheckData(goodsService.queryGoodsDeviceInfo(new GoodsDto.QueryGoodsDeviceInfo().setMercId(mercId).setDeviceIds(deviceIds)));
  684. Map<Long, List<GoodsDeviceDto.Vo>> deviceIdGoodsMap = goodsDeviceList.stream()
  685. .collect(Collectors.groupingBy(GoodsDeviceDto.Vo::getDeviceId));
  686. for (DeviceInfoDto.MercHomeDeviceVo device : deviceInfos) {
  687. Long dId = device.getDeviceId();
  688. //设备类型 反显
  689. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(device.getDeviceType()));
  690. device.setDeviceTypeName(dictDeviceType.getMsg());
  691. //运营状态 反显
  692. SysDictRedis dictBusyState = SysDictUtils.get(DictConsts.DEVICE_BUSY_STATUS, String.valueOf(device.getBusyState()));
  693. device.setBusyStateName(dictBusyState.getMsg());
  694. DeviceDataDto.Vo deviceData = dataMap.get(dId);
  695. Integer zero = new Integer(0);
  696. //今日销售、库存情况 反显
  697. device.setDayOrderNum(deviceData != null ? deviceData.getSalesCount() : zero);
  698. device.setDaySalesPrice(deviceData != null ? deviceData.getSalesMoney() : zero);
  699. DeviceStatusDto.Vo deviceStatus = datdeviceStatusMap.get(device.getDeviceId());
  700. if (CollUtil.isNotEmpty(deviceIdGoodsMap)) {
  701. List<GoodsDeviceDto.Vo> goodsDevice = deviceIdGoodsMap.get(dId);
  702. if (CollUtil.isNotEmpty(goodsDevice)) {
  703. device.setOnSaleNum(goodsDevice.stream().mapToInt(GoodsDeviceDto.Vo::getStock).sum());
  704. device.setFillNum(goodsDevice.stream().mapToInt(GoodsDeviceDto.Vo::getFillCount).sum());
  705. }
  706. }
  707. //联网状态
  708. Integer netState = deviceStatus == null ? DeviceNetSateType.DISCONNECT.getCode() : deviceStatus.getNetState();
  709. device.setNetState(netState);
  710. if (netState == null) {
  711. device.setNetStateName(StrUtil.EMPTY);
  712. } else {
  713. device.setNetStateName(DeviceNetSateType.getEnumByCode(netState).getDescription());
  714. }
  715. if (deviceStatus != null) {
  716. Integer deviceStateL = deviceStatus.getLockStateL();
  717. Integer deviceStateR = deviceStatus.getLockStateR();
  718. device.setDeviceStateL(deviceStateL);
  719. device.setDeviceStateR(deviceStateR);
  720. DeviceLockState deviceLockStateL = DeviceLockState.getEnumByCode(deviceStateL);
  721. DeviceLockState deviceLockStateR = DeviceLockState.getEnumByCode(deviceStateR);
  722. device.setDeviceStateRName(deviceLockStateR == null ? "未知" : deviceLockStateR.getDescription());
  723. device.setDeviceStateLName(deviceLockStateL == null ? "未知" : deviceLockStateL.getDescription());
  724. }
  725. }
  726. //名称排序
  727. if (CollUtil.isNotEmpty(deviceInfos)) {
  728. deviceInfos = ListUtil.sortByProperty(deviceInfos, LambdaUtils.getProperty(DeviceInfoDto.MercHomeDeviceVo::getDeviceName));
  729. deviceInfos = ListUtil.sortByProperty(deviceInfos, LambdaUtils.getProperty(DeviceInfoDto.MercHomeDeviceVo::getDeviceId));
  730. }
  731. vo.setDeviceInfos(deviceInfos);
  732. dataList.add(vo);
  733. }
  734. return R.ok(dataList);
  735. }
  736. /**
  737. * 获取商户设备列表
  738. *
  739. * @param mercId
  740. * @return
  741. */
  742. private List<DeviceInfo> getDevicesByMercId(Long mercId) {
  743. return list(Wrappers.<DeviceInfo>lambdaQuery().eq(DeviceInfo::getMercId, mercId).eq(DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode()));
  744. }
  745. /**
  746. * 小程序商户设备搜索
  747. *
  748. * @param page
  749. * @return
  750. */
  751. @Override
  752. public R<PageBean<DeviceInfoDto.MerHomeSearchVO>> mercDeviceSearchPage(@RequestBody DeviceInfoDto.Page page) {
  753. PageBean<DeviceInfoDto.MerHomeSearchVO> pageData = new PageBean<>();
  754. //小程序独有查询字段 缺货状态:stockStatus ,是否查故障设备:fault
  755. Boolean fault = page.getFault();
  756. Long mercId = page.getMercId();
  757. List<Long> myDeviceIds = page.getMyDeviceIds();
  758. if (CollUtil.isEmpty(myDeviceIds)) {
  759. //无设备
  760. return R.ok(pageData);
  761. }
  762. if (BooleanUtil.isTrue(fault)) {
  763. //查询故障设备
  764. List<DeviceEventMsg> deviceEventMsgs = deviceEventMsgService.list(Wrappers.<DeviceEventMsg>lambdaQuery()
  765. .eq(mercId != null, DeviceEventMsg::getMercId, page.getMercId())
  766. .in(DeviceEventMsg::getDeviceId, myDeviceIds));
  767. List<Long> deviceIdList = deviceEventMsgs.stream().map(DeviceEventMsg::getDeviceId).collect(Collectors.toList());
  768. if (CollUtil.isEmpty(deviceIdList)) {
  769. return R.ok(new PageBean<>());
  770. }
  771. page.setDeviceIdList(deviceIdList);
  772. } else {
  773. page.setDeviceIdList(myDeviceIds);
  774. }
  775. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  776. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  777. if (CollUtil.isEmpty(records)) {
  778. return R.ok(pageData);
  779. }
  780. List<DeviceInfoDto.MerHomeSearchVO> merHomeSearchRecords = new ArrayList<>();
  781. BeanUtil.copyProperties(pageBean, pageData);
  782. if (CollUtil.isNotEmpty(records)) {
  783. List<Long> deviceIds = records.stream().map(DeviceInfoDto.Vo2::getDeviceId).collect(Collectors.toList());
  784. String type = EnumDeviceDataType.DAY.getCode();
  785. String todayDate = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
  786. //查询当天
  787. DeviceDataDto.ListDTO dto = new DeviceDataDto.ListDTO()
  788. .setDeviceIds(deviceIds).setType(type).setDateValue(Integer.valueOf(todayDate)).setMercId(mercId);
  789. List<DeviceDataDto.Vo> deviceDataList = deviceDataService.list(dto);
  790. Map<Long, DeviceDataDto.Vo> dataMap = MapUtil.newHashMap();
  791. //统计数据反显
  792. if (CollUtil.isNotEmpty(deviceDataList)) {
  793. dataMap = deviceDataList.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDeviceId, d -> d));
  794. }
  795. for (DeviceInfoDto.Vo2 v : records) {
  796. DeviceInfoDto.MerHomeSearchVO merHomeSearchVO = new DeviceInfoDto.MerHomeSearchVO();
  797. Long deviceId = v.getDeviceId();
  798. DeviceDataDto.Vo vo = dataMap.get(deviceId);
  799. if (vo != null) {
  800. //今日订单数
  801. v.setDayOrderNum(vo != null ? vo.getSalesCount() : 0);
  802. v.setDaySalesPrice(vo != null ? vo.getSalesMoney() : 0);
  803. BeanUtil.copyProperties(vo, merHomeSearchVO);
  804. }
  805. DeviceSysinfoDto.Vo deviceSysinfo = v.getDeviceSysinfo();
  806. DeviceStatusDto.Vo deviceStatus = v.getDeviceStatus();
  807. merHomeSearchVO.setAppUpmVersion(deviceSysinfo.getAppUpmVersion());
  808. merHomeSearchVO.setTempValue(deviceStatus.getTempValue());
  809. merHomeSearchVO.setNetDbm(deviceStatus.getNetDbm());
  810. Integer deviceStateL = deviceStatus.getLockStateL();
  811. Integer deviceStateR = deviceStatus.getLockStateR();
  812. merHomeSearchVO.setDeviceStateL(deviceStateL);
  813. merHomeSearchVO.setDeviceStateR(deviceStateR);
  814. DeviceLockState deviceLockStateL = DeviceLockState.getEnumByCode(deviceStateL);
  815. DeviceLockState deviceLockStateR = DeviceLockState.getEnumByCode(deviceStateR);
  816. merHomeSearchVO.setDeviceStateRName(deviceLockStateR == null ? "未知" : deviceLockStateR.getDescription());
  817. merHomeSearchVO.setDeviceStateLName(deviceLockStateL == null ? "未知" : deviceLockStateL.getDescription());
  818. merHomeSearchRecords.add(merHomeSearchVO);
  819. }
  820. pageData.setRecords(merHomeSearchRecords);
  821. }
  822. return R.ok(pageData);
  823. }
  824. /**
  825. * 小程序商户设备详情-数据统计
  826. *
  827. * @param dto
  828. * @return
  829. */
  830. @Override
  831. public R<DeviceInfoDto.DeviceDataCountVO> dataCount(@RequestBody DeviceInfoDto.DeviceDataCountDTO dto) {
  832. // 统计类型(1=经营数据,2=经营图表,3=温度图表,4=信号图表,5=商品管理)
  833. Integer type = dto.getType();
  834. switch (type) {
  835. case 1:
  836. //经营数据
  837. return R.ok(dataCount1(dto));
  838. case 2:
  839. //经营图表
  840. return R.ok(dataCount2(dto));
  841. case 3:
  842. //温度图表
  843. return R.ok(dataCount3(dto));
  844. case 4:
  845. //信号图表
  846. return R.ok(dataCount4(dto));
  847. case 5:
  848. //商品管理
  849. return R.ok(dataCount5(dto));
  850. default:
  851. break;
  852. }
  853. return R.ok();
  854. }
  855. /**
  856. * 经营数据
  857. *
  858. * @param dto
  859. * @return
  860. */
  861. private DeviceInfoDto.DeviceDataCountVO dataCount1(DeviceInfoDto.DeviceDataCountDTO dto) {
  862. Long deviceId = dto.getDeviceId();
  863. Long mercId = dto.getMercId();
  864. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  865. //当天
  866. DeviceDataDto.Vo dayData = deviceDataService.getByDay(deviceId, mercId, DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN));
  867. if (dayData != null) {
  868. //今日退款成功订单金额
  869. OrderRefundDto.CountByDateVO countByDateVO = R.feignCheckData(
  870. refundService.countByDate(new OrderRefundDto.CountByDateDTO()
  871. .setDate(DateTime.now())
  872. .setMercId(mercId).setDeviceId(deviceId)));
  873. Integer hisRefundMoney = countByDateVO.getHisRefundMoney();
  874. Integer todayRefundMoney = countByDateVO.getTodayRefundMoney();
  875. //今日销售额 (扣除今日的订单的退款金额)
  876. if (dayData.getSalesMoney() == null) {
  877. dayData.setSalesMoney(0);
  878. } else {
  879. dayData.setSalesMoney(dayData.getSalesMoney() - todayRefundMoney);
  880. }
  881. dayData.setRefundMoney(hisRefundMoney + todayRefundMoney);
  882. }
  883. //当月
  884. DeviceDataDto.Vo monthData = deviceDataService.getByMonth(deviceId, mercId, DateUtil.format(new Date(), DatePattern.SIMPLE_MONTH_PATTERN));
  885. if (monthData != null) {
  886. OrderRefundDto.CountByMonthVO countByMonthVO = R.feignCheckData(refundService.countByMonth(new OrderRefundDto.CountByMonthDTO().setCurMonthDate(DateTime.now()).setMercId(mercId).setDeviceId(deviceId)));
  887. Integer hisMonthRefundMoney = countByMonthVO.getHisMonthRefundMoney();
  888. Integer monthRefundMoney = countByMonthVO.getMonthRefundMoney();
  889. monthData.setRefundMoney(hisMonthRefundMoney + monthRefundMoney);
  890. if (monthData.getSalesMoney() == null) {
  891. monthData.setSalesMoney(0);
  892. } else {
  893. monthData.setSalesMoney(monthData.getSalesMoney() - monthRefundMoney);
  894. }
  895. }
  896. vo.setDayBusinessData(copy(DeviceInfoDto.BusinessData.class, dayData));
  897. vo.setMonthBusinessData(copy(DeviceInfoDto.BusinessData.class, monthData));
  898. return vo;
  899. }
  900. /**
  901. * 经营图表
  902. *
  903. * @param dto
  904. * @return
  905. */
  906. private DeviceInfoDto.DeviceDataCountVO dataCount2(DeviceInfoDto.DeviceDataCountDTO dto) {
  907. Long deviceId = dto.getDeviceId();
  908. //近一个月 (销售额,订单数,退款金额,退款数)
  909. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  910. DeviceInfoDto.BusinessChart businessChart = new DeviceInfoDto.BusinessChart();
  911. List<String> categories = DataTime.dayListByLastDay(30);
  912. List<Integer> dateList = new ArrayList<>(30);
  913. businessChart.setCategories(categories);
  914. categories.forEach(d -> {
  915. dateList.add(Integer.valueOf(d.replaceAll(StrUtil.DASHED, StrUtil.EMPTY)));
  916. });
  917. Integer startDay = dateList.get(0);
  918. Integer endDay = dateList.get(categories.size() - 1);
  919. List<DeviceDataDto.Vo> listByDay = deviceDataService.getListByDay(deviceId, startDay, endDay);
  920. if (CollUtil.isEmpty(listByDay)) {
  921. return vo;
  922. }
  923. //每天的数据
  924. Map<Integer, DeviceDataDto.Vo> dataDayMap = listByDay.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDateValue, i -> i));
  925. List<DeviceInfoDto.MyChartSeries3> series = new ArrayList<>();
  926. String[] names = {"销售额", "订单数", "退款金额", "退款数"};
  927. for (int i = 0; i < names.length; i++) {
  928. DeviceInfoDto.MyChartSeries3 myChartSeries = new DeviceInfoDto.MyChartSeries3();
  929. //某个类型每天的数据
  930. List<String> data = new ArrayList<>();
  931. if (i == 0) {
  932. //销售额
  933. dateList.forEach(d -> {
  934. //每日数据填充
  935. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  936. if (deviceData == null) {
  937. data.add(String.valueOf(BigDecimal.ZERO));
  938. } else {
  939. Integer salesMoney = deviceData.getSalesMoney() == null ? 0 : deviceData.getSalesMoney();
  940. BigDecimal day = BigDecimal.valueOf(salesMoney).divide(BigDecimal.valueOf(100));
  941. data.add(String.valueOf(day));
  942. }
  943. });
  944. } else if (i == 1) {
  945. //订单数
  946. dateList.forEach(d -> {
  947. //每日数据填充
  948. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  949. String value = "0";
  950. if (deviceData == null) {
  951. data.add(value);
  952. } else {
  953. data.add(deviceData == null ? value : String.valueOf(deviceData.getSalesCount()));
  954. }
  955. });
  956. } else if (i == 2) {
  957. //退款金额
  958. dateList.forEach(d -> {
  959. //每日数据填充
  960. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  961. if (deviceData == null) {
  962. data.add(String.valueOf(BigDecimal.ZERO));
  963. } else {
  964. Integer refundMoney = deviceData.getRefundMoney() == null ? 0 : deviceData.getRefundMoney();
  965. BigDecimal day = BigDecimal.valueOf(refundMoney).divide(BigDecimal.valueOf(100));
  966. data.add(String.valueOf(day));
  967. }
  968. });
  969. } else if (i == 3) {
  970. //退款数
  971. dateList.forEach(d -> {
  972. //每日数据填充
  973. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  974. if (deviceData == null) {
  975. data.add("0");
  976. } else {
  977. data.add(deviceData == null ? "0" : String.valueOf(deviceData.getRefundCount()));
  978. }
  979. });
  980. }
  981. myChartSeries.setName(names[i]);
  982. myChartSeries.setData(data);
  983. series.add(myChartSeries);
  984. }
  985. businessChart.setSeries(series);
  986. vo.setBusinessChart(businessChart);
  987. return vo;
  988. }
  989. /**
  990. * 温度图表
  991. *
  992. * @param dto
  993. * @return
  994. */
  995. private DeviceInfoDto.DeviceDataCountVO dataCount3(DeviceInfoDto.DeviceDataCountDTO dto) {
  996. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  997. Long deviceId = dto.getDeviceId();
  998. String choosDate = dto.getChoosDate();
  999. if (StrUtil.isEmpty(choosDate)) {
  1000. choosDate = DateUtil.formatDate(new Date());
  1001. }
  1002. String startTime = choosDate + " 00:00:00";
  1003. String endTime = choosDate + " 23:59:59";
  1004. //查询选定日期的温度数据
  1005. List<DeviceTempRecords> deviceTempRecords = deviceTempRecordsService.list(Wrappers.<DeviceTempRecords>lambdaQuery()
  1006. .eq(DeviceTempRecords::getDeviceId, deviceId).between(DeviceTempRecords::getCreateTime, startTime, endTime)
  1007. .orderBy(true, true, DeviceTempRecords::getCreateTime));
  1008. if (CollUtil.isEmpty(deviceTempRecords)) {
  1009. return vo;
  1010. }
  1011. DeviceInfoDto.TemperatureChart temperatureChart = new DeviceInfoDto.TemperatureChart();
  1012. List<String> categories = new ArrayList<>();
  1013. String name = "温度";
  1014. List<DeviceInfoDto.MyChartSeries2> series = new ArrayList<>();
  1015. List<BigDecimal> data = new ArrayList<>();
  1016. deviceTempRecords.forEach(d -> {
  1017. int tempValue = d.getTempValue();
  1018. LocalDateTime createTime = d.getCreateTime();
  1019. String time = DateUtil.format(createTime, "HH:mm:ss");
  1020. categories.add(time);
  1021. data.add(NumberUtil.toBigDecimal(tempValue));
  1022. });
  1023. DeviceInfoDto.MyChartSeries2 myChartSeries = new DeviceInfoDto.MyChartSeries2();
  1024. myChartSeries.setName(name);
  1025. myChartSeries.setData(data);
  1026. series.add(myChartSeries);
  1027. temperatureChart.setSeries(series);
  1028. temperatureChart.setCategories(categories);
  1029. vo.setTemperatureChart(temperatureChart);
  1030. return vo;
  1031. }
  1032. /**
  1033. * 信号图表
  1034. *
  1035. * @param dto
  1036. * @return
  1037. */
  1038. private DeviceInfoDto.DeviceDataCountVO dataCount4(DeviceInfoDto.DeviceDataCountDTO dto) {
  1039. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1040. Long deviceId = dto.getDeviceId();
  1041. String choosDate = dto.getChoosDate();
  1042. if (StrUtil.isEmpty(choosDate)) {
  1043. choosDate = DateUtil.formatDate(new Date());
  1044. }
  1045. String startTime = choosDate + " 00:00:00";
  1046. String endTime = choosDate + " 23:59:59";
  1047. //查询选定日期的温度数据
  1048. List<DeviceNetRecord> deviceNetRecords = deviceNetRecordService.list(Wrappers.<DeviceNetRecord>lambdaQuery()
  1049. .eq(DeviceNetRecord::getDeviceId, deviceId).between(DeviceNetRecord::getCreateTime, startTime, endTime)
  1050. .orderBy(true, true, DeviceNetRecord::getCreateTime));
  1051. if (CollUtil.isEmpty(deviceNetRecords)) {
  1052. return vo;
  1053. }
  1054. DeviceInfoDto.SignalChart signalChart = new DeviceInfoDto.SignalChart();
  1055. List<String> categories = new ArrayList<>();
  1056. String name = "信号";
  1057. List<DeviceInfoDto.MyChartSeries> series = new ArrayList<>();
  1058. List<Integer> data = new ArrayList<>();
  1059. deviceNetRecords.forEach(d -> {
  1060. Integer value = d.getSimDbm();
  1061. LocalDateTime createTime = d.getCreateTime();
  1062. String time = DateUtil.format(createTime, "HH:mm:ss");
  1063. categories.add(time);
  1064. data.add(value);
  1065. });
  1066. DeviceInfoDto.MyChartSeries myChartSeries = new DeviceInfoDto.MyChartSeries();
  1067. myChartSeries.setName(name);
  1068. myChartSeries.setData(data);
  1069. series.add(myChartSeries);
  1070. signalChart.setSeries(series);
  1071. signalChart.setCategories(categories);
  1072. vo.setSignalChart(signalChart);
  1073. return vo;
  1074. }
  1075. /**
  1076. * 商品管理
  1077. *
  1078. * @param dto
  1079. * @return
  1080. */
  1081. private DeviceInfoDto.DeviceDataCountVO dataCount5(DeviceInfoDto.DeviceDataCountDTO dto) {
  1082. Long deviceId = dto.getDeviceId();
  1083. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1084. DeviceInfoDto.GoodsData goodsData = new DeviceInfoDto.GoodsData();
  1085. //在售商品种类
  1086. //根据设备ID查商品id
  1087. GoodsDeviceDto.SelectList selectList = new GoodsDeviceDto.SelectList();
  1088. selectList.setDeviceIds(CollUtil.newArrayList(deviceId));
  1089. selectList.setMercId(dto.getMercId());
  1090. R<List<GoodsDeviceDto.Vo>> r = R.feignCheck(goodsDeviceService.list(selectList));
  1091. List<GoodsDeviceDto.Vo> goodsDeviceList = r.getData();
  1092. Integer stock = 0;
  1093. Integer afterFillStock = 0;
  1094. if (CollUtil.isNotEmpty(goodsDeviceList)) {
  1095. stock = goodsDeviceList.stream().mapToInt(GoodsDeviceDto.Vo::getStock).sum();
  1096. afterFillStock = goodsDeviceList.stream().mapToInt(GoodsDeviceDto.Vo::getFillCount).sum();
  1097. }
  1098. goodsData.setAfterFillStock(afterFillStock);
  1099. goodsData.setStock(stock);
  1100. goodsData.setCategoryNum(CollUtil.isNotEmpty(goodsDeviceList) ? goodsDeviceList.size() : new Integer(0));
  1101. vo.setGoodsData(goodsData);
  1102. return vo;
  1103. }
  1104. public PageBean<DeviceInfoDto.Vo2> queryPage(DeviceInfoDto.Page page) {
  1105. IPage<DeviceInfoQueryPage> iPage = baseMapper.queryPage(toIPage(page.getPage()), page);
  1106. return toPageBean(DeviceInfoDto.Vo2.class, iPage);
  1107. }
  1108. private <T> void check(T value, T value2, String msg) {
  1109. if (value.toString().equals(value2.toString())) {
  1110. throw new CommRuntimeException(msg);
  1111. }
  1112. }
  1113. }