DeviceInfoServiceImpl.java 53 KB

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