DeviceInfoServiceImpl.java 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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 cn.hutool.json.JSONUtil;
  15. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  16. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  17. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  18. import com.baomidou.mybatisplus.core.metadata.IPage;
  19. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  20. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  21. import com.xy.collections.list.JArrayList;
  22. import com.xy.collections.list.JList;
  23. import com.xy.collections.map.JMap;
  24. import com.xy.config.DeviceThreadPoolConfig;
  25. import com.xy.consts.DictConsts;
  26. import com.xy.device.*;
  27. import com.xy.dto.*;
  28. import com.xy.dto.api.biz.ContainerAddDTO;
  29. import com.xy.dto.be.MercDto;
  30. import com.xy.dto.common.MercPlaceDto;
  31. import com.xy.dto.device.DeviceQueryDTO;
  32. import com.xy.dto.device.DeviceRegDTO;
  33. import com.xy.dto.mini.MiniDeviceInfoDto;
  34. import com.xy.entity.*;
  35. import com.xy.enums.FileExportType;
  36. import com.xy.error.CommRuntimeException;
  37. import com.xy.mapper.DeviceInfoMapper;
  38. import com.xy.mapper.entity.DeviceInfoQueryPage;
  39. import com.xy.service.be.MercFeignService;
  40. import com.xy.service.be.MercService;
  41. import com.xy.service.common.MercPlaceService;
  42. import com.xy.service.common.MercRegionService;
  43. import com.xy.sys.EnumDataClearSize;
  44. import com.xy.util.ExcelUtils;
  45. import com.xy.utils.*;
  46. import com.xy.utils.Enum.AlgorithmTypeEnum;
  47. import com.xy.utils.enums.*;
  48. import com.xy.vo.DeviceQueryVO;
  49. import io.swagger.annotations.Api;
  50. import io.swagger.annotations.ApiOperation;
  51. import jodd.introspector.MapperFunction;
  52. import lombok.RequiredArgsConstructor;
  53. import lombok.extern.slf4j.Slf4j;
  54. import org.springframework.stereotype.Service;
  55. import org.springframework.transaction.annotation.Transactional;
  56. import org.springframework.validation.annotation.Validated;
  57. import org.springframework.web.bind.annotation.PostMapping;
  58. import org.springframework.web.bind.annotation.RequestBody;
  59. import javax.servlet.http.HttpServletResponse;
  60. import javax.validation.Valid;
  61. import java.io.IOException;
  62. import java.math.BigDecimal;
  63. import java.time.LocalDateTime;
  64. import java.util.*;
  65. import java.util.stream.Collectors;
  66. import static com.xy.utils.PlusBeans.*;
  67. /**
  68. * <p>
  69. * 设备-信息 服务实现类
  70. * </p>
  71. *
  72. * @author lijin
  73. * @since 2022-12-23
  74. */
  75. @Slf4j
  76. @Service
  77. @RequiredArgsConstructor
  78. @Api(tags = "设备-信息")
  79. public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements DeviceInfoService {
  80. /**
  81. * 质检商户code
  82. */
  83. public static final String QA_MERC_CODE = "10001";
  84. private final CloudWalkApiService cloudWalkApiService;
  85. private final AlgorithmService algorithmService;
  86. private final CountApiService countApiService;
  87. private final TyApiService tyApiService;
  88. private final MercFeignService mercFeignService;
  89. private final DeviceSysinfoServiceImpl deviceSysinfoService;
  90. private final DeviceStatusServiceImpl deviceStatusService;
  91. private final DeviceRegisterServiceImpl deviceRegisterService;
  92. private final DeviceEventMsgServiceImpl deviceEventMsgService;
  93. private final DeviceDataServiceImpl deviceDataService;
  94. private final DeviceTempRecordsServiceImpl deviceTempRecordsService;
  95. private final DeviceNetRecordServiceImpl deviceNetRecordService;
  96. private final GoodsDeviceService goodsDeviceService;
  97. private final FileExportService fileExportService;
  98. private final RedisService<String> redisService;
  99. private final GoodsService goodsService;
  100. private final AlipayDeviceService alipayDeviceService;
  101. private final String keyPrefix = "device:history:";
  102. private final MercRegionService mercRegionService;
  103. private final DeviceAlgorithmChargingServiceImpl deviceAlgorithmChargingService;
  104. private final MercPlaceService mercPlaceService;
  105. private final MercUserBindDeviceService mercUserBindDeviceService;
  106. private final DevicePartServiceImpl devicePartService;
  107. private final MercService mercService;
  108. @Override
  109. @ApiOperation("设备列表-管理员")
  110. public R<List<DeviceInfoDto.ListByAdminName>> listByAdminName(@RequestBody @Validated DeviceInfoDto.GroupByAdminNameDto dto) {
  111. String noAdmin = "未分配管理员";
  112. List<DeviceInfoDto.ListByAdminName> deviceInfoList = baseMapper.listByAdminName(dto);
  113. deviceInfoList.forEach(i -> {
  114. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(i.getDeviceType()));
  115. i.setDeviceTypeName(dictDeviceType.getMsg());
  116. if (!Emptys.check(i.getDeviceName())) {
  117. i.setDeviceName("");
  118. }
  119. });
  120. // 没有管理员的的设置默认值
  121. deviceInfoList.stream().filter(s -> s.getAdminName() == null).forEach(s -> s.setAdminName(noAdmin));
  122. return R.ok(deviceInfoList);
  123. }
  124. //@Override
  125. @ApiOperation("设备分页-管理员")
  126. @Override
  127. public R<PageBean<DeviceInfoDto.ListByAdminName>> pageByAdminName(@RequestBody @Validated DeviceInfoDto.PageByAdminNameDto dto) {
  128. PageBean page = dto.getPage();
  129. String noAdmin = "未分配管理员";
  130. IPage<DeviceInfoDto.ListByAdminName> deviceInfoList = baseMapper.pageByAdminName(toIPage(page), dto);
  131. deviceInfoList.getRecords().forEach(i -> {
  132. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(i.getDeviceType()));
  133. i.setDeviceTypeName(dictDeviceType.getMsg());
  134. if (!Emptys.check(i.getDeviceName())) {
  135. i.setDeviceName("");
  136. }
  137. });
  138. // 没有管理员的的设置默认值
  139. deviceInfoList.getRecords().stream().filter(s -> s.getAdminName() == null).forEach(s -> s.setAdminName(noAdmin));
  140. return R.ok(toPageBean(DeviceInfoDto.ListByAdminName.class, deviceInfoList));
  141. }
  142. @Override
  143. @ApiOperation("设备分组-管理员")
  144. public R<List<DeviceInfoDto.GroupByAdminNameVo>> groupByAdminName(@RequestBody @Validated DeviceInfoDto.GroupByAdminNameDto dto) {
  145. String noAdmin = "未分配管理员";
  146. List<DeviceInfoDto.ListByAdminName> deviceInfoList = listByAdminName(dto).getData();
  147. // 根据管理员名字分组
  148. Map<String, List<DeviceInfoDto.ListByAdminName>> deviceMap = deviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfoDto.ListByAdminName::getAdminName));
  149. List<DeviceInfoDto.GroupByAdminNameVo> list = new ArrayList<>();
  150. // 不包含未分配管理员的
  151. deviceMap.forEach(
  152. (k, i) -> {
  153. DeviceInfoDto.GroupByAdminNameVo vo = new DeviceInfoDto.GroupByAdminNameVo();
  154. if (!noAdmin.equals(k)) {
  155. vo.setAdminName(k)
  156. .setDeviceNum(i.size())
  157. .setDeviceInfos(i);
  158. list.add(vo);
  159. }
  160. }
  161. );
  162. // 包含分配管理员的
  163. List<DeviceInfoDto.ListByAdminName> noAdminNamesList = deviceMap.get(noAdmin);
  164. if (Emptys.check(noAdminNamesList)) {
  165. DeviceInfoDto.GroupByAdminNameVo vo = new DeviceInfoDto.GroupByAdminNameVo();
  166. vo.setAdminName(noAdmin)
  167. .setDeviceNum(noAdminNamesList.size())
  168. .setDeviceInfos(noAdminNamesList);
  169. list.add(vo);
  170. }
  171. return R.ok(list);
  172. }
  173. public R<List<DeviceInfoDto.GroupByAdminNameVo>> groupByAdminCount(@RequestBody @Validated DeviceInfoDto.GroupByAdminNameDto dto) {
  174. String noAdmin = "未分配管理员";
  175. List<DeviceInfoDto.ListByAdminName> deviceInfoList = listByAdminName(dto).getData();
  176. // 根据管理员名字分组
  177. Map<String, List<DeviceInfoDto.ListByAdminName>> deviceMap = deviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfoDto.ListByAdminName::getAdminName));
  178. List<DeviceInfoDto.GroupByAdminNameVo> list = new ArrayList<>();
  179. // 不包含未分配管理员的
  180. deviceMap.forEach(
  181. (k, i) -> {
  182. DeviceInfoDto.GroupByAdminNameVo vo = new DeviceInfoDto.GroupByAdminNameVo();
  183. if (!noAdmin.equals(k)) {
  184. vo.setAdminName(k)
  185. .setDeviceNum(i.size())
  186. .setDeviceInfos(i);
  187. list.add(vo);
  188. }
  189. }
  190. );
  191. // 包含分配管理员的
  192. List<DeviceInfoDto.ListByAdminName> noAdminNamesList = deviceMap.get(noAdmin);
  193. if (Emptys.check(noAdminNamesList)) {
  194. DeviceInfoDto.GroupByAdminNameVo vo = new DeviceInfoDto.GroupByAdminNameVo();
  195. vo.setAdminName(noAdmin)
  196. .setDeviceNum(noAdminNamesList.size())
  197. .setDeviceInfos(noAdminNamesList);
  198. list.add(vo);
  199. }
  200. return R.ok(list);
  201. }
  202. @PostMapping("eventList")
  203. @ApiOperation("根据事件编码查询设备")
  204. public R<PageBean<DeviceInfoDto.EventListVo>> eventList(@RequestBody @Validated DeviceInfoDto.EventList eventList) {
  205. String countAs = "id";
  206. QueryWrapper<DeviceEventMsg> queryWrapper = new QueryWrapper<DeviceEventMsg>()
  207. .select(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getDeviceId) + " as " + LambdaUtils.getProperty(DeviceEventMsg::getDeviceId), "count(*) as " + countAs)
  208. .eq(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getCode), eventList.getCode())
  209. .between(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getCreateTime), eventList.getBeginTime(), eventList.getEndTime())
  210. .groupBy(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getDeviceId))
  211. .last("HAVING " + countAs + " >= " + eventList.getSize());
  212. IPage<DeviceEventMsg> iPage = deviceEventMsgService.page(toIPage(eventList.getPage()), queryWrapper);
  213. PageBean<DeviceInfoDto.EventListVo> pageBean = new PageBean<DeviceInfoDto.EventListVo>()
  214. .setSize(iPage.getSize())
  215. .setCurrent(iPage.getCurrent())
  216. .setTotal(iPage.getTotal());
  217. List<DeviceEventMsg> list = iPage.getRecords();
  218. if (!Emptys.check(list)) {
  219. return R.ok(pageBean);
  220. }
  221. List<DeviceInfo> deviceInfos = listByIds(new JArrayList<>(list).getProperty(DeviceEventMsg::getDeviceId));
  222. List<DeviceInfoDto.EventListVo> eventListVos = copy(DeviceInfoDto.EventListVo.class, deviceInfos);
  223. List<DeviceInfoDto.EventListVo> builder = copy(eventListVos)
  224. .target(list, DeviceInfoDto.EventListVo::getDeviceId, DeviceInfoDto.EventListVo::getSize, DeviceEventMsg::getDeviceId, DeviceEventMsg::getId)
  225. .builder();
  226. return R.ok(pageBean.setRecords(builder));
  227. }
  228. @ApiOperation("拓元算法设备列表")
  229. @PostMapping("tyDeviceList")
  230. public R<PageBean<DeviceInfoDto.Vo>> tyDeviceList(@RequestBody @Validated DeviceInfoDto.TyDeviceDTO dto) {
  231. PageBean pageBean = dto.getPage();
  232. String searchKey = dto.getSearchKey();
  233. Integer thirdStatus = dto.getThirdStatus();
  234. List<String> statusList = new ArrayList<>();
  235. // 1待审核,2已审核
  236. if (thirdStatus != null && thirdStatus.intValue() == 1) {
  237. statusList = CollUtil.newArrayList("1000", "2000", "3000", "5000");
  238. }
  239. // 2已审核 4000: 设备登记成功
  240. if (thirdStatus != null && thirdStatus.intValue() == 2) {
  241. statusList.add("4000");
  242. }
  243. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<>();
  244. lqw.eq(DeviceInfo::getAlgorithmId, AlgorithmTypeEnum.TY.getId());
  245. lqw.in(CollUtil.isNotEmpty(statusList), DeviceInfo::getThirdStatus, statusList);
  246. lqw.and(StrUtil.isNotEmpty(searchKey), wrapper -> wrapper
  247. .eq(DeviceInfo::getDeviceId, searchKey)
  248. .or()
  249. .likeRight(DeviceInfo::getDeviceName, searchKey));
  250. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  251. List<DeviceInfo> records = iPage.getRecords();
  252. if (CollUtil.isNotEmpty(records)) {
  253. ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.TY_DEVICE_STATUS, records.size());
  254. records.forEach(deviceInfo -> execute.execute(() -> {
  255. Long deviceId = deviceInfo.getDeviceId();
  256. /**
  257. * 请求状态码。
  258. * 1000: 未知的设备CPUID
  259. * 2000: 设备登记 审批中
  260. * 3000: 设备登记已拒绝
  261. * 4000: 设备登记成功
  262. * 5000: 设备已禁⽤
  263. */
  264. String tyStatus = deviceInfo.getThirdStatus();
  265. if (!"4000".equals(tyStatus)) {
  266. // 未登记成功的需要实时查询
  267. // 查询登记设备
  268. DeviceQueryVO deviceQueryVO = tyApiService.deviceQuery(new DeviceQueryDTO().setCpuId(String.valueOf(deviceId)));
  269. Integer status = deviceQueryVO.getStatus();
  270. String message = deviceQueryVO.getMessage();
  271. deviceInfo.setThirdStatus(String.valueOf(status));
  272. deviceInfo.setThirdResult(message);
  273. updateById(deviceInfo);
  274. }
  275. }));
  276. execute.end();
  277. }
  278. PageBean<DeviceInfoDto.Vo> voPageBean = toPageBean(DeviceInfoDto.Vo.class, iPage);
  279. return R.ok(voPageBean);
  280. }
  281. /**
  282. * 商户设备列表分页-穿梭框用
  283. *
  284. * @param dto
  285. * @return {@link R}<{@link List}<{@link DeviceInfoDto.MyDeviceInfo}>>
  286. */
  287. public R<PageBean<DeviceInfoDto.MyDeviceInfo>> myDeviceList(@RequestBody @Validated DeviceInfoDto.MyDeviceDTO dto) {
  288. PageBean pageBean = dto.getPage();
  289. String searchKey = dto.getSearchKey();
  290. Long mercId = dto.getMercId();
  291. Integer type = dto.getType();
  292. Long strategyId = dto.getStrategyId();
  293. GoodsDeviceDto.SelectList selectList = new GoodsDeviceDto.SelectList();
  294. if (strategyId != null) {
  295. selectList.setMercId(mercId).setPriceStrategyId(strategyId);
  296. }
  297. List<GoodsDeviceDto.Vo> goodsDevices = R.feignCheckData(goodsDeviceService.list(selectList));
  298. if (type == 2 && CollUtil.isEmpty(goodsDevices)) {
  299. // 右侧已选的
  300. return R.ok(new PageBean<>());
  301. }
  302. List<Long> deviceIds = goodsDevices.stream().map(GoodsDeviceDto.Vo::getDeviceId).collect(Collectors.toList());
  303. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  304. .build();
  305. lqw.in(type == 2 && CollUtil.isNotEmpty(deviceIds), DeviceInfo::getDeviceId, deviceIds);
  306. // 左侧排除掉已关联的
  307. lqw.notIn(type == 1 && CollUtil.isNotEmpty(deviceIds), DeviceInfo::getDeviceId, deviceIds);
  308. lqw.and(StrUtil.isNotEmpty(searchKey), wrapper -> wrapper
  309. .eq(DeviceInfo::getDeviceId, searchKey)
  310. .or()
  311. .likeRight(DeviceInfo::getDeviceName, searchKey));
  312. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  313. return R.ok(toPageBean(DeviceInfoDto.MyDeviceInfo.class, iPage));
  314. }
  315. @ApiOperation("点位设备数量查询")
  316. @Override
  317. public R<List<DeviceInfoDto.PlaceDeviceNumVo>> placeDeviceNum(@RequestBody @Validated DeviceInfoDto.PlaceDeviceNumDto dto) {
  318. return R.ok(baseMapper.placeDeviceNum(dto));
  319. }
  320. @Override
  321. @ApiOperation("设备列表带卡包数")
  322. public R<PageBean<DeviceInfoDto.AlgorithmChargingVo>> algorithmChargingDevice(@RequestBody @Validated DeviceInfoDto.AlgorithmCharging algorithmCharging) {
  323. PageBean pageBean = algorithmCharging.getPage();
  324. // 查询设备
  325. Integer value = SysDictUtils.getValue(EnumDeviceActiveStatus.Code.CODE.getCode(), EnumDeviceActiveStatus.N_1.getCode(), Integer.class);
  326. LambdaUpdateWrapper<DeviceInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<DeviceInfo>()
  327. .eq(DeviceInfo::getMercId, algorithmCharging.getMercId())
  328. .eq(DeviceInfo::getActiveState, value)
  329. .in(Emptys.check(algorithmCharging.getDeviceIds()), DeviceInfo::getDeviceId, algorithmCharging.getDeviceIds())
  330. .orderByDesc(DeviceInfo::getCreateTime);
  331. IPage<DeviceInfo> page = page(toIPage(pageBean), lambdaUpdateWrapper);
  332. PageBean<DeviceInfoDto.AlgorithmChargingVo> algorithmChargingVoPageBean = toPageBean(DeviceInfoDto.AlgorithmChargingVo.class, page);
  333. List<DeviceInfoDto.AlgorithmChargingVo> algorithmChargingVos = algorithmChargingVoPageBean.getRecords();
  334. if (!Emptys.check(algorithmChargingVos)) {
  335. return R.ok(algorithmChargingVoPageBean);
  336. }
  337. // 查询卡包数量
  338. List<DeviceAlgorithmChargingDto.CountVo> data = deviceAlgorithmChargingService.count(new DeviceAlgorithmChargingDto.Count()
  339. .setDeviceIds(new JArrayList<>(algorithmChargingVos).getProperty(DeviceInfoDto.AlgorithmChargingVo::getDeviceId))
  340. .setMercId(algorithmCharging.getMercId())
  341. ).getData();
  342. JMap<Long, DeviceAlgorithmChargingDto.CountVo> cover = new JArrayList<>(data).toMap(DeviceAlgorithmChargingDto.CountVo::getDeviceId).cover();
  343. algorithmChargingVos.forEach(algorithmChargingVo -> {
  344. DeviceAlgorithmChargingDto.CountVo countVo = cover.get(algorithmChargingVo.getDeviceId());
  345. Beans.copy(algorithmChargingVo, countVo);
  346. });
  347. return R.ok(algorithmChargingVoPageBean);
  348. }
  349. @Override
  350. @ApiOperation("设备在线数查询")
  351. public R<Map<Long, DeviceInfoDto.NetStateCountVo>> netStateCount(DeviceInfoDto.NetStateCountDto dto) {
  352. List<DeviceInfoDto.NetStateCount> netStateCountList = baseMapper.netStateCount(dto.getMercIdList());
  353. List<Long> netStateCountMercList = netStateCountList.stream().map(DeviceInfoDto.NetStateCount::getMercId).distinct().collect(Collectors.toList());
  354. Map<Long, DeviceInfoDto.NetStateCountVo> voMap = new HashMap<>();
  355. List<Long> mercIdList = Emptys.check(dto.getMercIdList()) ? dto.getMercIdList() : netStateCountMercList;
  356. if (!Emptys.check(mercIdList)) {
  357. return R.ok();
  358. }
  359. for (Long mercId : mercIdList) {
  360. Integer count = 0;
  361. Integer offline = 0;
  362. DeviceInfoDto.NetStateCountVo vo = new DeviceInfoDto.NetStateCountVo().setMercId(mercId);
  363. for (DeviceInfoDto.NetStateCount i : netStateCountList) {
  364. if (Objects.equals(i.getMercId(), mercId)) {
  365. count += i.getCount();
  366. if ("1".equals(i.getNetState())) {
  367. vo.setOnLineCount(i.getCount());
  368. } else {
  369. offline += i.getCount();
  370. }
  371. }
  372. }
  373. vo.setCount(count).setOffLineCount(offline);
  374. voMap.put(mercId, vo);
  375. }
  376. return R.ok(voMap);
  377. }
  378. @Override
  379. @ApiOperation("分页")
  380. public R<PageBean<DeviceInfoDto.Vo>> pageSingle(DeviceInfoDto.PageSingle dto) {
  381. PageBean pageBean = dto.getPage();
  382. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  383. .build();
  384. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  385. return R.ok(toPageBean(DeviceInfoDto.Vo.class, iPage));
  386. }
  387. @Override
  388. @ApiOperation("设备流水统计分页")
  389. public R<PageBean<DeviceDataDto.DeviceFlowCountVO>> deviceFlowCountPage(@RequestBody @Valid DeviceDataDto.DeviceFlowCountDTO dto) {
  390. Long curMercId = dto.getCurMercId();
  391. PageBean pageBean = dto.getPage();
  392. List<Long> mercIds = new ArrayList<>();
  393. Long chooseMercId = dto.getChooseMercId();
  394. if (chooseMercId == null) {
  395. // 未指定商户 查下级商户含自身
  396. mercIds = R.feignCheckData(mercService.getAllSubMercIds(new MercDto.QuerySubDTO().setParentMercID(curMercId)));
  397. } else {
  398. mercIds.add(chooseMercId);
  399. }
  400. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  401. .ge(DeviceInfo::getActiveTime, dto.getActiveStartTime())
  402. .le(DeviceInfo::getActiveTime, dto.getActiveEndStartTime())
  403. .build().in(DeviceInfo::getMercId, mercIds).eq(DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode());
  404. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  405. PageBean<DeviceDataDto.DeviceFlowCountVO> dataPage = toPageBean(DeviceDataDto.DeviceFlowCountVO.class, iPage);
  406. List<DeviceDataDto.DeviceFlowCountVO> records = dataPage.getRecords();
  407. if (CollUtil.isNotEmpty(records)) {
  408. List<Long> placeIds = records.stream().filter(s -> s.getPlaceId() != null).map(DeviceDataDto.DeviceFlowCountVO::getPlaceId).distinct().collect(Collectors.toList());
  409. List<MercPlaceDto.Vo> merPlaceList = R.feignCheckData(mercPlaceService.list(new MercPlaceDto.ListDto().setIds(placeIds)));
  410. if (Emptys.check(merPlaceList)) {
  411. Map<Long, MercPlaceDto.Vo> placeMap = merPlaceList.stream().collect(Collectors.toMap(MercPlaceDto.Vo::getId, p -> p));
  412. for (DeviceDataDto.DeviceFlowCountVO record : records) {
  413. Long placeId = record.getPlaceId();
  414. if (placeId == null) {
  415. continue;
  416. }
  417. // 点位 区域 反显
  418. MercPlaceDto.Vo placeVo = placeMap.get(placeId);
  419. if (placeVo != null) {
  420. record.setDistrictName(placeVo.getRegionName());
  421. record.setPlaceName(placeVo.getPlaceName());
  422. }
  423. }
  424. dataPage.setRecords(records);
  425. }
  426. }
  427. return R.ok(dataPage);
  428. }
  429. /**
  430. * 按商户分页查设备
  431. *
  432. * @param dto
  433. * @return {@link PageBean}<{@link DeviceInfoDto.Vo}>
  434. */
  435. public PageBean<DeviceInfoDto.Vo> pageByMerc(DeviceInfoDto.PageByMercDto dto) {
  436. PageBean pageBean = dto.getPage();
  437. String deviceSearch = dto.getDeviceSearch();
  438. List<Long> myDeviceIds = dto.getMyDeviceIds();
  439. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  440. .build();
  441. List<Long> deviceIds = dto.getDeviceIds();
  442. Boolean choosed = dto.getChoosed();
  443. if (BooleanUtil.isFalse(choosed)) {
  444. // 未选择 排除已选择
  445. if (CollUtil.isNotEmpty(deviceIds)) {
  446. lqw.notIn(DeviceInfo::getDeviceId, deviceIds);
  447. } else {
  448. if (CollUtil.isEmpty(myDeviceIds)) {
  449. return new PageBean<>();
  450. }
  451. lqw.in(DeviceInfo::getDeviceId, myDeviceIds);
  452. }
  453. } else {
  454. // 已选择
  455. // 指定设备 已选择
  456. if (CollUtil.isNotEmpty(deviceIds)) {
  457. lqw.in(DeviceInfo::getDeviceId, deviceIds);
  458. } else {
  459. return new PageBean<>();
  460. }
  461. }
  462. // 设备搜索
  463. lqw.and(StrUtil.isNotEmpty(deviceSearch),
  464. wrapper -> wrapper
  465. .likeRight(DeviceInfo::getDeviceName, deviceSearch)
  466. .or()
  467. .likeRight(DeviceInfo::getDeviceId, deviceSearch)
  468. );
  469. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  470. return toPageBean(DeviceInfoDto.Vo.class, iPage);
  471. }
  472. @Override
  473. @ApiOperation("对象查询")
  474. public R<DeviceInfoDto.Vo> obj(DeviceInfoDto.Obj obj) {
  475. // 设备信息
  476. LambdaQueryWrapper<DeviceInfo> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(obj, DeviceInfo.class).build();
  477. List<DeviceInfo> list = list(lambdaQueryWrapper);
  478. if (!Emptys.check(list)) {
  479. return R.ok();
  480. }
  481. DeviceInfoDto.Vo deviceInfo = copy(DeviceInfoDto.Vo.class, list.get(0));
  482. int num = 0;
  483. if (obj.getIsSysinfo()) {
  484. num++;
  485. }
  486. if (obj.getIsStatus()) {
  487. num++;
  488. }
  489. if (obj.getIsRegister()) {
  490. num++;
  491. }
  492. if (num > 0) {
  493. ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, num);
  494. if (obj.getIsSysinfo()) {
  495. execute.execute(() -> {
  496. // 系统信息
  497. DeviceSysinfoDto.Vo deviceSysinfo = deviceSysinfoService.get(new DeviceSysinfoDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  498. deviceInfo.setDeviceSysinfo(deviceSysinfo);
  499. });
  500. }
  501. if (obj.getIsStatus()) {
  502. execute.execute(() -> {
  503. // 状态信息
  504. DeviceStatusDto.Vo deviceStatus = deviceStatusService.obj(new DeviceStatusDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  505. deviceInfo.setDeviceStatus(deviceStatus);
  506. });
  507. }
  508. if (obj.getIsRegister()) {
  509. execute.execute(() -> {
  510. // 注册信息
  511. DeviceRegisterDto.Vo deviceRegister = deviceRegisterService.obj(new DeviceRegisterDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  512. deviceInfo.setDeviceRegister(deviceRegister);
  513. });
  514. }
  515. execute.end();
  516. }
  517. return R.ok(deviceInfo);
  518. }
  519. @ApiOperation("反显设备名称")
  520. @Override
  521. public R<Map<Long, String>> getDeviceNameList(DeviceInfoDto.DeviceIdDto dto) {
  522. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<DeviceInfo>()
  523. .in(DeviceInfo::getDeviceId, dto.getDeviceId())
  524. .select(DeviceInfo::getDeviceId, DeviceInfo::getDeviceName);
  525. List<DeviceInfo> deviceInfoList = list(lqw);
  526. return R.ok(deviceInfoList.stream().collect(Collectors.toMap(DeviceInfo::getDeviceId, i -> Optional.ofNullable(i.getDeviceName()).orElse(""))));
  527. }
  528. @Override
  529. public R<DeviceSysinfoDto.Vo> getDeviceSysinfo(DeviceSysinfoDto.DeviceSysInfo dto) {
  530. String dtoDeviceSN = dto.getDeviceSN();
  531. DeviceSysinfo one = deviceSysinfoService.getOne(Wrappers.<DeviceSysinfo>lambdaQuery().eq(DeviceSysinfo::getDeviceSn, dtoDeviceSN));
  532. return R.ok(BeanUtil.copyProperties(one, DeviceSysinfoDto.Vo.class));
  533. }
  534. @Override
  535. @ApiOperation("设备访问历史添加")
  536. public R history(DeviceInfoDto.Obj obj) {
  537. // 获取字典
  538. SysDictRedis sysDictRedis = SysDictUtils.get(EnumDataClearSize.Code.CODE.getCode(), EnumDataClearSize.DEVICE_HISTORY_TWIG.getCode());
  539. Integer value = Integer.valueOf(sysDictRedis.getValue());
  540. // 获取redis
  541. String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
  542. List<String> list = redisService.getList(key);
  543. list.add(0, String.valueOf(obj.getDeviceId()));
  544. // 去重
  545. List<String> redisList = new ArrayList<>();
  546. JList<String> comparing = new JArrayList<>(list).comparing();
  547. if (comparing.size() > value) {
  548. for (int i = 0; i < value; i++) {
  549. redisList.add(comparing.get(i));
  550. }
  551. } else {
  552. redisList = comparing;
  553. }
  554. redisService.removeList(key);
  555. redisService.setList(key, redisList);
  556. return R.ok();
  557. }
  558. @Override
  559. @ApiOperation("开门检测")
  560. public R<DeviceInfoDto.Vo> checkOpenDoor(DeviceInfoDto.Obj obj) {
  561. DeviceInfoDto.Vo deviceInfo = obj(new DeviceInfoDto.Obj()
  562. .setDeviceId(obj.getDeviceId())
  563. .setIsStatus(true)
  564. ).getData();
  565. if (deviceInfo == null) {
  566. return R.fail("设备不存在");
  567. }
  568. DeviceStatusDto.Vo deviceStatus = deviceInfo.getDeviceStatus();
  569. SysDictRedis qualitySets = SysDictUtils.get(EnumQualityMercSets.Code.CODE.getCode(), EnumQualityMercSets.MERC_CODE.getCode());
  570. List<String> qualitySetList = Arrays.asList(qualitySets.getValue().split(","));
  571. // 设备当前商户是质检时不检查
  572. if (!qualitySetList.contains(deviceInfo.getMercCode())) {
  573. check(deviceInfo.getActiveState(), 2, "设备未激活");
  574. check(deviceInfo.getFreezeStatus(), 2, "设备已冻结");
  575. check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getLockStateR() : deviceStatus.getLockStateL(), 2, "设备已锁机");
  576. check(deviceStatus.getIsPay(), Boolean.FALSE, "设备不可交易");
  577. check(deviceInfo.getBusyState(), 2, "设备已停运");
  578. SysDictRedis sysDictRedis = SysDictUtils.get(EnumDeviceFaultLevelPayThreshold.Code.CODE.getCode(), EnumDeviceFaultLevelPayThreshold.NOT_PAY.getCode());
  579. check(deviceInfo.getFaultLevel(), sysDictRedis.getValue(), "设备故障");
  580. }
  581. check(deviceStatus.getNetState(), 2, "设备已离线");
  582. check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getDoorStateR() : deviceStatus.getDoorStateL(), 1, "设备正在使用中,请稍后");
  583. return R.ok(deviceInfo);
  584. }
  585. @PostMapping("historyList")
  586. @ApiOperation("设备访问历史查询")
  587. public R<List<DeviceInfoDto.Vo>> historyList() {
  588. // 获取redis
  589. String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
  590. List<String> deviceIds = redisService.getList(key);
  591. if (!Emptys.check(deviceIds)) {
  592. return R.ok();
  593. }
  594. // 查询数据库
  595. List<DeviceInfo> list = list(new LambdaQueryWrapper<DeviceInfo>().in(DeviceInfo::getDeviceId, deviceIds));
  596. return R.ok(copy(DeviceInfoDto.Vo.class, list));
  597. }
  598. @ApiOperation("根据点位ID更新坐标")
  599. public R updateLonByPlaceId(@RequestBody @Validated DeviceInfoDto.UpdateLonByPlaceId dto) {
  600. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>()
  601. .eq(DeviceInfo::getPlaceId, dto.getPlaceId())
  602. .set(DeviceInfo::getLon, dto.getLon())
  603. .set(DeviceInfo::getLat, dto.getLat());
  604. update(luw);
  605. return R.ok();
  606. }
  607. @ApiOperation("修改")
  608. @PostMapping("update")
  609. public R update(@RequestBody @Validated DeviceInfoDto.Update update) {
  610. DeviceInfo deviceInfo = getById(update.getDeviceId());
  611. // 判断是否更新了点位
  612. if (Emptys.check(update.getPlaceId()) && !Objects.equals(deviceInfo.getPlaceId(), update.getPlaceId())) {
  613. // 把设备绑定给点位管理员
  614. MercUserBindDeviceDto.BindByDeviceUpdate bindByPlaceIdDTto = new MercUserBindDeviceDto.BindByDeviceUpdate();
  615. bindByPlaceIdDTto.setDeviceId(update.getDeviceId())
  616. .setPlaceId(update.getPlaceId())
  617. .setMercId(deviceInfo.getMercId());
  618. mercUserBindDeviceService.bindByDeviceUpdate(bindByPlaceIdDTto);
  619. // 更新坐标为点位的坐标
  620. MercPlaceDto.ObjVo place = mercPlaceService.obj(new MercPlaceDto.Obj().setId(update.getPlaceId())).getData();
  621. update.setLat(place.getLat()).setLon(place.getLon());
  622. }
  623. DeviceInfo deviceSave = copy(DeviceInfo.class, update)
  624. .setUpdateTime(LocalDateTime.now());
  625. updateById(deviceSave);
  626. Long deviceId = deviceSave.getDeviceId();
  627. DeviceInfoDto.Vo device = R.feignCheckData(this.obj(new DeviceInfoDto.Obj().setDeviceId(deviceId).setIsSysinfo(true)));
  628. Long algorithmId = update.getAlgorithmId();
  629. if (algorithmId != null && AlgorithmTypeEnum.CLOUD.getId() == algorithmId) {
  630. // 云从算法
  631. boolean b = cloudWalkApiService.checkDeviceExist(deviceId);
  632. if (!b) {
  633. // 货柜不存在,新增
  634. cloudWalkApiService.containerAdd(new ContainerAddDTO().setContainerCode(String.valueOf(deviceId)));
  635. }
  636. } else if (algorithmId != null && AlgorithmTypeEnum.TY.getId() == algorithmId) {
  637. // 拓元算法
  638. // 查询登记设备
  639. DeviceQueryVO deviceQueryVO = tyApiService.deviceQuery(new DeviceQueryDTO().setCpuId(String.valueOf(deviceId)));
  640. Integer status = deviceQueryVO.getStatus();
  641. if (status != null && 1000 == status.intValue()) {
  642. // 未知的设备CPUID,进行登记
  643. tyApiService.deviceReg(new DeviceRegDTO()
  644. .setCpuId(String.valueOf(deviceId))
  645. .setDeviceNumber(String.valueOf(deviceId))
  646. );
  647. }
  648. }
  649. Integer deviceType = device.getDeviceType();
  650. if (ObjectUtil.equals(DeviceTypeEnum.TYPE5.getCode(), deviceType)) {
  651. DeviceUpdateDTO deviceUpdateDTO = new DeviceUpdateDTO();
  652. if (StrUtil.isNotEmpty(update.getDeviceName())) {
  653. deviceUpdateDTO.setDeviceName(update.getDeviceName());
  654. }
  655. String deviceSn = device.getDeviceSysinfo().getDeviceSn();
  656. if (deviceUpdateDTO != null) {
  657. deviceUpdateDTO.setTerminalId(String.valueOf(update.getDeviceId()));
  658. deviceUpdateDTO.setBoardSn(deviceSn);
  659. alipayDeviceService.deviceUpdate(deviceUpdateDTO);
  660. }
  661. }
  662. return R.ok();
  663. }
  664. @Override
  665. @ApiOperation("批量修改")
  666. public R updateBatch(@RequestBody List<DeviceInfoDto.Update> updates) {
  667. LocalDateTime now = LocalDateTime.now();
  668. List<DeviceInfo> deviceInfos = new ArrayList<>(updates.size());
  669. updates.forEach(update -> deviceInfos.add(copy(DeviceInfo.class, update).setUpdateTime(now)));
  670. updateBatchById(deviceInfos);
  671. return R.ok();
  672. }
  673. @Override
  674. @ApiOperation("激活数量统计")
  675. public R<Integer> activationCount(DeviceInfoDto.ActivationCount activationCount) {
  676. LambdaQueryWrapper<DeviceInfo> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceInfo>()
  677. .eq(DeviceInfo::getMercId, activationCount.getMercId())
  678. .eq(DeviceInfo::getActiveState, 1)
  679. .ne(Emptys.check(activationCount.getNotDeviceType()), DeviceInfo::getDeviceType, activationCount.getNotDeviceType())
  680. .between(Emptys.check(activationCount.getBeginTime()), DeviceInfo::getActiveTime, activationCount.getBeginTime(), activationCount.getEndTime());
  681. long count = count(lambdaQueryWrapper);
  682. return R.ok((int) count);
  683. }
  684. @Override
  685. @ApiOperation("更新商户线路")
  686. public R updateLine(@RequestBody @Validated DeviceInfoDto.UpdateLine updateLine) {
  687. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>().eq(DeviceInfo::getMercId, updateLine.getMercId());
  688. // 绑定线路,更换线路
  689. if (DeviceInfoDto.UPDATE.equals(updateLine.getType())) {
  690. DeviceInfo deviceInfo = new DeviceInfo();
  691. deviceInfo.setPlaceLineId(updateLine.getPlaceLineId());
  692. luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
  693. baseMapper.update(deviceInfo, luw);
  694. }
  695. // 删除线路
  696. if (DeviceInfoDto.DEL.equals(updateLine.getType())) {
  697. luw.eq(DeviceInfo::getPlaceLineId, updateLine.getWherePlaceLineId());
  698. luw.set(DeviceInfo::getPlaceLineId, null);
  699. baseMapper.update(null, luw);
  700. }
  701. // 解绑线路 设置线路ID为null
  702. if (DeviceInfoDto.CLEAR.equals(updateLine.getType())) {
  703. luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
  704. luw.set(DeviceInfo::getPlaceLineId, null);
  705. baseMapper.update(null, luw);
  706. }
  707. return R.ok();
  708. }
  709. @Override
  710. @ApiOperation("更新商户点位")
  711. public R updatePlace(@RequestBody @Validated DeviceInfoDto.UpdatePlace updatePlace) {
  712. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>().eq(DeviceInfo::getMercId, updatePlace.getMercId());
  713. // 绑定点位,更换点位
  714. if (DeviceInfoDto.UPDATE.equals(updatePlace.getType())) {
  715. DeviceInfo deviceInfo = new DeviceInfo();
  716. deviceInfo.setPlaceId(updatePlace.getPlaceId());
  717. luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
  718. baseMapper.update(deviceInfo, luw);
  719. }
  720. // 删除点位
  721. if (DeviceInfoDto.DEL.equals(updatePlace.getType())) {
  722. luw.eq(DeviceInfo::getPlaceId, updatePlace.getWherePlaceId());
  723. luw.set(DeviceInfo::getPlaceId, null);
  724. baseMapper.update(null, luw);
  725. }
  726. // 解绑点位 设置点位ID为null
  727. if (DeviceInfoDto.CLEAR.equals(updatePlace.getType())) {
  728. luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
  729. luw.set(DeviceInfo::getPlaceId, null);
  730. baseMapper.update(null, luw);
  731. }
  732. return R.ok();
  733. }
  734. @ApiOperation("分页查询")
  735. @Override
  736. public R<PageBean<DeviceInfoDto.Vo2>> page(@RequestBody DeviceInfoDto.Page page) {
  737. return R.ok(queryPage(page));
  738. }
  739. @ApiOperation("导出设备列表")
  740. @PostMapping("exportDevices")
  741. public void exportDevices(HttpServletResponse response, @RequestBody @Valid DeviceInfoDto.Page page) throws IOException {
  742. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  743. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  744. List<DeviceInfoDto.DeviceExcelVO> deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
  745. // 输出
  746. ExcelUtils.write(response, "设备列表.xls", "设备列表", DeviceInfoDto.DeviceExcelVO.class, deviceExcelVOS);
  747. }
  748. @ApiOperation("导出设备(异步)")
  749. @PostMapping("exportDevices/async")
  750. public void exportDevicesAsync(@RequestBody @Validated DeviceInfoDto.Page page) {
  751. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  752. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  753. List<DeviceInfoDto.DeviceExcelVO> deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
  754. // 异步导出参数封装
  755. ExcelDTO<DeviceInfoDto.DeviceExcelVO> excelDTO = new ExcelDTO<>();
  756. excelDTO.setData(deviceExcelVOS);
  757. excelDTO.setHead(DeviceInfoDto.DeviceExcelVO.class);
  758. excelDTO.setSheetName(FileExportType.DEVICE_INFO.getDescription());
  759. excelDTO.setFileExportType(FileExportType.DEVICE_INFO);
  760. // 执行导出
  761. fileExportService.exportExcelAsync(excelDTO);
  762. }
  763. @PostMapping("nearbyPage")
  764. @ApiOperation("附近设备分页查询")
  765. public R<PageBean<DeviceInfoDto.Vo2>> nearbyPage(@RequestBody DeviceInfoDto.Page page) {
  766. if (!Emptys.check(page.getLon()) || !Emptys.check(page.getLat())) {
  767. throw new CommRuntimeException("经纬度不能为空");
  768. }
  769. return R.ok(queryPage(page));
  770. }
  771. @ApiOperation(value = "商户设备授权", hidden = true)
  772. @Override
  773. @Transactional(rollbackFor = Exception.class)
  774. public R<Boolean> mercDeviceAuth(DeviceInfoDto.MercDeviceAuthDto auth) {
  775. Long mercId = auth.getMercId();
  776. String mercCode = auth.getMercCode();
  777. Long algorithmId = auth.getAlgorithmId();
  778. String mercName = auth.getMercName();
  779. // 商户最终设备列表
  780. List<Long> deviceIds = auth.getDeviceIds();
  781. List<DeviceInfo> devices = getDevicesByMercId(mercId);
  782. // 取消商户设备授权
  783. if (CollUtil.isEmpty(deviceIds)) {
  784. if (CollUtil.isEmpty(devices)) {
  785. return R.ok(Boolean.TRUE);
  786. }
  787. }
  788. // 更新商户设备授权
  789. List<DeviceInfo> deviceInfos = this.listByIds(deviceIds);
  790. // 绑定前,先进行旧的用户设备关联表清理
  791. R.feignCheckData(mercService.unBindSubMercDevice(new MiniDeviceInfoDto.MercDeviceUnBindDto().setDeviceIds(deviceIds)));
  792. List<DeviceStatus> deviceStatuses = new ArrayList<>();
  793. for (DeviceInfo deviceInfo : deviceInfos) {
  794. Integer deviceType = deviceInfo.getDeviceType();
  795. Long deviceId = deviceInfo.getDeviceId();
  796. // 清理点位
  797. deviceInfo.setPlaceId(-1L);
  798. if (ObjectUtil.equals(deviceType, DeviceTypeEnum.TYPE5.getCode())) {
  799. // 支付宝设备算法
  800. algorithmId = AlgorithmTypeEnum.ALIPAY.getId();
  801. } else {
  802. // 非支付宝算法
  803. if (AlgorithmTypeEnum.CLOUD.getId() == algorithmId) {
  804. // 云从算法
  805. boolean b = cloudWalkApiService.checkDeviceExist(deviceId);
  806. if (!b) {
  807. // 货柜不存在,新增
  808. cloudWalkApiService.containerAdd(new ContainerAddDTO().setContainerCode(String.valueOf(deviceId)));
  809. }
  810. } else if (AlgorithmTypeEnum.TY.getId() == algorithmId) {
  811. // 拓元算法
  812. // 查询登记设备
  813. DeviceQueryVO deviceQueryVO = tyApiService.deviceQuery(new DeviceQueryDTO().setCpuId(String.valueOf(deviceId)));
  814. Integer status = deviceQueryVO.getStatus();
  815. if (status != null && 1000 == status.intValue()) {
  816. // 未知的设备CPUID,进行登记
  817. tyApiService.deviceReg(new DeviceRegDTO()
  818. .setCpuId(String.valueOf(deviceId))
  819. .setDeviceNumber(String.valueOf(deviceId))
  820. );
  821. }
  822. }
  823. }
  824. int refMercId = deviceInfo.getMercId().intValue();
  825. String refMercCode = deviceInfo.getMercCode();
  826. if (BooleanUtil.isFalse(auth.getMercOperate())) {
  827. // 非商户操作
  828. // 只有解绑后,才能给顶级商户授权
  829. if (refMercId != -1 && refMercId != mercId.intValue()) {
  830. StrBuilder sb = StrBuilder.create();
  831. String errMsg = sb.append("设备[")
  832. .append(deviceInfo.getDeviceId())
  833. .append("]")
  834. .append("已被商户[")
  835. .append(deviceInfo.getMercName())
  836. .append("]绑定,请先进行解绑!")
  837. .toString();
  838. // 非质检商户需要进行判断,质检商户跳过
  839. if (!QA_MERC_CODE.equals(refMercCode)) {
  840. // 已关联别商户
  841. return R.fail(errMsg, Boolean.FALSE);
  842. }
  843. }
  844. } else {
  845. // 商户操作直接转移给子商户
  846. }
  847. // 绑定关系
  848. deviceInfo.setMercId(mercId).setMercCode(mercCode).setAlgorithmId(algorithmId).setMercName(mercName);
  849. // 标记机器可交易
  850. DeviceStatus deviceStatus = new DeviceStatus()
  851. .setDeviceId(deviceInfo.getDeviceId())
  852. .setIsPay(true);
  853. deviceStatuses.add(deviceStatus);
  854. }
  855. saveOrUpdateBatch(deviceInfos);
  856. if (Emptys.check(deviceStatuses)) {
  857. deviceStatusService.updateBatchById(deviceStatuses);
  858. }
  859. return R.ok(Boolean.TRUE);
  860. }
  861. /**
  862. * 解绑机器 回收
  863. *
  864. * @param dto
  865. * @return
  866. */
  867. @Override
  868. public R<Boolean> unBindMercDevice(DeviceInfoDto.MercDeviceUnBindDto dto) {
  869. List<DeviceInfo> deviceInfos = this.listByIds(dto.getDeviceIds());
  870. return R.ok(removeMerRefDevicesToTopMerc(deviceInfos));
  871. }
  872. /**
  873. * 商户解绑设备 --商家端
  874. *
  875. * @param dto
  876. * @return {@link Boolean}
  877. */
  878. public Boolean unBindDeviceByMerc(DeviceInfoDto.MercDeviceUnBindDto dto) {
  879. List<DeviceInfo> deviceInfos = this.listByIds(dto.getDeviceIds());
  880. if (CollUtil.isNotEmpty(deviceInfos)) {
  881. List<Long> mercIds = deviceInfos.stream().map(DeviceInfo::getMercId).collect(Collectors.toList());
  882. mercIds.forEach(mercId -> {
  883. if (mercId.longValue() == dto.getMercId().longValue()) {
  884. throw new CommRuntimeException("只能选择子商户设备进行解绑,请检查是否选择了当前商户下的设备!");
  885. }
  886. });
  887. }
  888. return removeMerDevicesByDeviceIds(dto, deviceInfos);
  889. }
  890. private Boolean removeMerDevicesByDeviceIds(DeviceInfoDto.MercDeviceUnBindDto dto, List<DeviceInfo> deviceInfos) {
  891. if (CollUtil.isNotEmpty(deviceInfos)) {
  892. deviceInfos.forEach(deviceInfo -> {
  893. // 回收 到当前操作商户
  894. deviceInfo.setMercId(dto.getMercId());
  895. deviceInfo.setMercDeviceCode(StrUtil.EMPTY);
  896. // 清理点位信息
  897. deviceInfo.setPlaceId(-1L);
  898. deviceInfo.setMercName(dto.getMercName());
  899. deviceInfo.setMercCode(dto.getMercCode());
  900. });
  901. // 用户设备关联表清理
  902. List<Long> deviceIds = deviceInfos.stream().map(DeviceInfo::getDeviceId).collect(Collectors.toList());
  903. R.feignCheckData(mercService.unBindSubMercDevice(new MiniDeviceInfoDto.MercDeviceUnBindDto().setDeviceIds(deviceIds)));
  904. // 批量更新
  905. return updateBatchById(deviceInfos);
  906. }
  907. return Boolean.FALSE;
  908. }
  909. /**
  910. * 回收机器
  911. *
  912. * @param deviceInfos
  913. * @return
  914. */
  915. private Boolean removeMerRefDevicesToTopMerc(List<DeviceInfo> deviceInfos) {
  916. MercDto.Vo mercCheck = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setMercCode(QA_MERC_CODE)));
  917. if (CollUtil.isNotEmpty(deviceInfos)) {
  918. deviceInfos.forEach(deviceInfo -> {
  919. // 回收
  920. deviceInfo.setMercId(mercCheck.getId());
  921. deviceInfo.setMercDeviceCode(StrUtil.EMPTY);
  922. deviceInfo.setMercName(mercCheck.getName());
  923. deviceInfo.setMercCode(QA_MERC_CODE);
  924. // 清理点位
  925. deviceInfo.setPlaceId(-1L);
  926. });
  927. // 批量更新
  928. return updateBatchById(deviceInfos);
  929. }
  930. return Boolean.FALSE;
  931. }
  932. /**
  933. * 批量移除商户设备绑定关系
  934. *
  935. * @param deviceInfos
  936. * @return
  937. */
  938. private Boolean removeMerRefDevices(List<DeviceInfo> deviceInfos, Long parentId) {
  939. if (CollUtil.isNotEmpty(deviceInfos) && parentId != null) {
  940. deviceInfos.forEach(deviceInfo -> {
  941. // 非顶级兴元商户,解绑后,机器归父商户
  942. if (parentId != 1) {
  943. MercDto.Vo mercParent = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setId(parentId)));
  944. if (mercParent != null) {
  945. deviceInfo.setMercId(mercParent.getId());
  946. deviceInfo.setMercName(mercParent.getName());
  947. deviceInfo.setMercCode(mercParent.getMercCode());
  948. } else {
  949. deviceInfo.setMercId(-1L);
  950. deviceInfo.setMercName(StrUtil.EMPTY);
  951. deviceInfo.setMercCode(StrUtil.EMPTY);
  952. }
  953. }
  954. // 一级商户,解绑后,直接释放
  955. if (parentId == 0) {
  956. MercDto.Vo mercCheck = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setMercCode(QA_MERC_CODE)));
  957. if (mercCheck != null) {
  958. deviceInfo.setMercId(mercCheck.getId());
  959. deviceInfo.setMercName(mercCheck.getName());
  960. deviceInfo.setMercCode(mercCheck.getMercCode());
  961. } else {
  962. deviceInfo.setMercId(-1L);
  963. deviceInfo.setMercName(StrUtil.EMPTY);
  964. deviceInfo.setMercCode(StrUtil.EMPTY);
  965. }
  966. }
  967. });
  968. // 批量更新
  969. return updateBatchById(deviceInfos);
  970. }
  971. return Boolean.FALSE;
  972. }
  973. @ApiOperation("集合查询")
  974. @Override
  975. public R<List<DeviceInfoDto.Vo>> list(DeviceInfoDto.ListDto dto) {
  976. List<DeviceInfo> list = list(new LambdaQueryWrapper<DeviceInfo>()
  977. .eq(Emptys.check(dto.getFreezeStatus()), DeviceInfo::getFreezeStatus, dto.getFreezeStatus())
  978. .in(CollUtil.isNotEmpty(dto.getDeviceIds()), DeviceInfo::getDeviceId, dto.getDeviceIds())
  979. .eq(Emptys.check(dto.getMercId()), DeviceInfo::getMercId, dto.getMercId())
  980. );
  981. return R.ok(copy(DeviceInfoDto.Vo.class, list));
  982. }
  983. @Override
  984. @ApiOperation("通用集合查询")
  985. public R<List<DeviceInfoDto.Vo>> listCommon(DeviceInfoDto.ListCommon dto) {
  986. String deviceSearch = dto.getDeviceSearch();
  987. DeviceInfoDto.Vo vo = dto.getVo();
  988. QueryWrapper<DeviceInfo> queryWrapper = new MybatisPlusQuery().eqWrapper(vo == null ? new DeviceInfoDto.Vo() : vo, DeviceInfo.class).buildQW();
  989. // List<Long> placeLineIds = dto.getPlaceLineIds();
  990. List<Long> deviceIds = dto.getDeviceIds();
  991. List<String> columnList = dto.getColumnList();
  992. // if (CollUtil.isNotEmpty(placeLineIds)) {
  993. // queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getPlaceLineId), placeLineIds);
  994. // }
  995. // fixed
  996. if (StrUtil.isNotEmpty(deviceSearch)) {
  997. queryWrapper.and(wrapper -> wrapper.likeRight(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceName), deviceSearch).or()
  998. .eq(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceSearch));
  999. }
  1000. if (CollUtil.isNotEmpty(columnList)) {
  1001. queryWrapper.select(columnList.stream().toArray(String[]::new));
  1002. }
  1003. if (CollUtil.isNotEmpty(deviceIds)) {
  1004. queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceIds);
  1005. }
  1006. return R.ok(copy(DeviceInfoDto.Vo.class, list(queryWrapper)));
  1007. }
  1008. @Override
  1009. @ApiOperation("商户设备算法列表")
  1010. public R<List<Long>> mercAlgorithmIdList(DeviceInfoDto.MercAlgorithmIdListDto dto) {
  1011. String algorithmIdStr = LambdaUtils.getProperty(DeviceInfo::getAlgorithmId);
  1012. String mercStr = StringTools.humpToLine(LambdaUtils.getProperty(DeviceInfo::getMercId));
  1013. QueryWrapper<DeviceInfo> lqw = new QueryWrapper<DeviceInfo>()
  1014. .isNotNull(StringTools.humpToLine(algorithmIdStr))
  1015. .eq(mercStr, dto.getMercId())
  1016. .select(String.format("DISTINCT (%s) as %s", StringTools.humpToLine(algorithmIdStr), algorithmIdStr));
  1017. List<Long> list = listObjs(lqw, (MapperFunction<Object, Long>) o -> (Long) o);
  1018. return R.ok(list);
  1019. }
  1020. @Override
  1021. @ApiOperation("小程序-商户设备首页统计")
  1022. public R<DeviceInfoDto.MercHomeStatisticalVO> mercHomeStatistical(DeviceInfoDto.MercHomeQueryDTO dto) {
  1023. Long mercId = dto.getMercId();
  1024. List<Long> myDeviceIds = dto.getMyDeviceIds();
  1025. // 初始化数据
  1026. DeviceInfoDto.MercHomeStatisticalVO mercHomeStatisticalVO = new DeviceInfoDto.MercHomeStatisticalVO()
  1027. .setClosedNum(0).setOfflineNum(0)
  1028. .setOnlineNum(0).setOperatingNum(0).setNeedToFillNum(0);
  1029. if (CollUtil.isEmpty(myDeviceIds)) {
  1030. return R.ok(mercHomeStatisticalVO);
  1031. }
  1032. List<DeviceInfo> mercDevices = listByIds(myDeviceIds);
  1033. if (CollUtil.isEmpty(mercDevices)) {
  1034. return R.ok(mercHomeStatisticalVO);
  1035. }
  1036. // 在线、离线
  1037. List<DeviceStatus> deviceStatuses = deviceStatusService.listByIds(myDeviceIds);
  1038. // 分组统计
  1039. Map<Integer, Long> countNetstateMap = deviceStatuses.stream().collect(Collectors
  1040. .groupingBy(DeviceStatus::getNetState, Collectors.counting()));
  1041. Integer onlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.CONNECTED.getCode(), Integer.class);
  1042. Integer offlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.DISCONNECT.getCode(), Integer.class);
  1043. int onlineNum = countNetstateMap.get(onlineDictValue) == null ? 0 : countNetstateMap.get(onlineDictValue).intValue();
  1044. int offlineNum = countNetstateMap.get(offlineDictValue) == null ? 0 : countNetstateMap.get(offlineDictValue).intValue();
  1045. mercHomeStatisticalVO.setOnlineNum(onlineNum);
  1046. mercHomeStatisticalVO.setOfflineNum(offlineNum);
  1047. // 锁机、未锁机
  1048. // Map<Integer, Long> countLockLstateMap = deviceStatuses.stream().collect(Collectors
  1049. // .groupingBy(DeviceStatus::getLockStateL, Collectors.counting()));
  1050. // Long lockLStateNum = countLockLstateMap.get(DeviceLockState.LOCK.getCode());
  1051. // Long unLockLStateNum = countLockLstateMap.get(DeviceLockState.UN_LOCK.getCode());
  1052. Map<Integer, Long> countLockLstateMap = mercDevices.stream().collect(Collectors
  1053. .groupingBy(DeviceInfo::getBusyState, Collectors.counting()));
  1054. // 运营
  1055. Long operatingNum = countLockLstateMap.get(DeviceBusySateType.OPERATING.getCode());
  1056. // 停运
  1057. Long suspendedNum = countLockLstateMap.get(DeviceBusySateType.SUSPENDED.getCode());
  1058. mercHomeStatisticalVO.setOperatingNum(operatingNum == null ? 0 : operatingNum.intValue());
  1059. mercHomeStatisticalVO.setClosedNum(suspendedNum == null ? 0 : suspendedNum.intValue());
  1060. // 待补货
  1061. Integer deviceNum = R.feignCheckData(goodsDeviceService.countOutOfStockDevice(new GoodsDeviceDto.CountOutOfStockDevice().setMercId(mercId).setDeviceIds(myDeviceIds)));
  1062. mercHomeStatisticalVO.setNeedToFillNum(deviceNum);
  1063. return R.ok(mercHomeStatisticalVO);
  1064. }
  1065. @Override
  1066. @ApiOperation("小程序-商户设备首页列表")
  1067. public R<List<DeviceInfoDto.MercHomeListVO>> mercHomeList(DeviceInfoDto.MercHomeQueryDTO dto) {
  1068. String searchKey = dto.getSearchKey();
  1069. Long mercId = dto.getMercId();
  1070. String deviceName = dto.getDeviceName();
  1071. Long deviceId = dto.getDeviceId();
  1072. List<Long> searchPlaceIdList = new ArrayList<>();
  1073. // 根据管理员名字查询点位ID列表
  1074. if (Emptys.check(dto.getAdminName())) {
  1075. MercPlaceDto.ListDto placeDto = new MercPlaceDto.ListDto();
  1076. placeDto.setMercId(mercId).setAdminName(dto.getAdminName());
  1077. List<MercPlaceDto.Vo> searchPlaceList = mercPlaceService.list(placeDto).getData();
  1078. if (Emptys.check(searchPlaceList)) {
  1079. searchPlaceIdList = searchPlaceList.stream().map(MercPlaceDto.Vo::getId).distinct().collect(Collectors.toList());
  1080. } else {
  1081. return R.ok();
  1082. }
  1083. }
  1084. boolean isQa = BooleanUtil.isTrue(dto.getIsQaMode());
  1085. List<Long> searchDeviceIds = new ArrayList<>();
  1086. if (StrUtil.isNotEmpty(searchKey) || StrUtil.isNotEmpty(dto.getAdminName())) {
  1087. // 名称或者编号搜索设备
  1088. LambdaQueryWrapper<DeviceInfo> deviceLqw = Wrappers.<DeviceInfo>lambdaQuery()
  1089. .eq(DeviceInfo::getMercId, mercId)
  1090. .in(Emptys.check(searchPlaceIdList), DeviceInfo::getPlaceId, searchPlaceIdList);
  1091. if (Emptys.check(searchKey)) {
  1092. deviceLqw.and(wrapper -> wrapper
  1093. .eq(Emptys.check(searchKey), DeviceInfo::getDeviceId, searchKey)
  1094. .or()
  1095. .like(Emptys.check(searchKey), DeviceInfo::getDeviceName, searchKey)
  1096. );
  1097. }
  1098. List<DeviceInfo> list = list(deviceLqw);
  1099. if (CollUtil.isEmpty(list)) {
  1100. return R.ok(new ArrayList<>());
  1101. }
  1102. searchDeviceIds = list.stream().map(DeviceInfo::getDeviceId).collect(Collectors.toList());
  1103. }
  1104. List<Long> myDeviceIds = dto.getDeviceIdList();
  1105. Integer busyStatus = dto.getBusyStatus();
  1106. Integer onlineStatus = dto.getOnlineStatus();
  1107. Integer deviceType = dto.getDeviceType();
  1108. // 条件查询 在线状态,运营状态,设备类型,
  1109. List<Long> deviceIdList = new ArrayList<>();
  1110. if (deviceId != null && myDeviceIds.indexOf(deviceId) > 0) {
  1111. deviceIdList.add(deviceId);
  1112. }
  1113. if (CollUtil.isNotEmpty(myDeviceIds) && deviceId == null) {
  1114. deviceIdList.addAll(myDeviceIds);
  1115. }
  1116. if (CollUtil.isNotEmpty(deviceIdList)) {
  1117. dto.setDeviceIdList(deviceIdList);
  1118. List<Long> queryDeviceIds = new ArrayList<>();
  1119. if (CollUtil.isNotEmpty(searchDeviceIds)) {
  1120. for (Long id : searchDeviceIds) {
  1121. int index = deviceIdList.indexOf(id);
  1122. if (index >= 0) {
  1123. queryDeviceIds.add(id);
  1124. }
  1125. }
  1126. // 无符合权限的搜索设备,返空
  1127. if (CollUtil.isEmpty(queryDeviceIds)) {
  1128. return R.ok(new ArrayList<>());
  1129. } else {
  1130. deviceIdList = queryDeviceIds;
  1131. dto.setDeviceIdList(queryDeviceIds);
  1132. }
  1133. }
  1134. } else {
  1135. return R.ok(new ArrayList<>());
  1136. }
  1137. if (onlineStatus != null) {
  1138. List<DeviceStatus> deviceStatusList = deviceStatusService.list(Wrappers.<DeviceStatus>lambdaQuery()
  1139. .eq(DeviceStatus::getNetState, onlineStatus)
  1140. .in(CollUtil.isNotEmpty(myDeviceIds), DeviceStatus::getDeviceId, deviceIdList)
  1141. );
  1142. if (CollUtil.isNotEmpty(deviceStatusList)) {
  1143. List<Long> dbDeviceIdList = deviceStatusList.stream().map(DeviceStatus::getDeviceId).collect(Collectors.toList());
  1144. deviceIdList = dbDeviceIdList;
  1145. } else {
  1146. return R.ok(new ArrayList<>());
  1147. }
  1148. }
  1149. List<DeviceInfoDto.MercHomeCountVO> list = this.baseMapper.merHomeCountList(dto);
  1150. if (CollUtil.isEmpty(list)) {
  1151. return R.ok(Collections.emptyList());
  1152. }
  1153. List<DeviceInfoDto.MercHomeListVO> dataList = new ArrayList<>(list.size());
  1154. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<>();
  1155. // 非质检商户才需要激活
  1156. lqw.eq(!isQa, DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode());
  1157. lqw.eq(mercId != null, DeviceInfo::getMercId, mercId);
  1158. lqw.eq(busyStatus != null, DeviceInfo::getBusyState, busyStatus);
  1159. lqw.eq(deviceType != null, DeviceInfo::getDeviceType, deviceType);
  1160. lqw.in(CollUtil.isNotEmpty(deviceIdList), DeviceInfo::getDeviceId, deviceIdList);
  1161. lqw.like(StrUtil.isNotEmpty(deviceName), DeviceInfo::getDeviceName, deviceName).orderByAsc(true, DeviceInfo::getDeviceName, DeviceInfo::getDeviceId);
  1162. List<DeviceInfoDto.Vo> deviceInfoList = copy(DeviceInfoDto.Vo.class, this.list(lqw));
  1163. // 根据点位ID查询管理员名字
  1164. List<Long> placeIdList = deviceInfoList.stream().map(DeviceInfoDto.Vo::getPlaceId).distinct().filter(Objects::nonNull).collect(Collectors.toList());
  1165. List<MercPlaceDto.Vo> mercPlaceList = mercPlaceService.list(new MercPlaceDto.ListDto().setIds(placeIdList)).getData();
  1166. if (Emptys.check(mercPlaceList)) {
  1167. // Map<Long, String> mercPlaceMap = mercPlaceList.stream().collect(Collectors.toMap(i -> i.getId(), i -> i.getAdminName()));
  1168. Map<Long, String> mercPlaceMap = mercPlaceList.stream().collect(HashMap::new, (map, item) -> map.put(item.getId(), item.getAdminName()), HashMap::putAll);
  1169. deviceInfoList.forEach(i -> i.setAdminName(mercPlaceMap.get(i.getPlaceId())));
  1170. }
  1171. // 没有管理员的的设置默认值
  1172. deviceInfoList.stream().filter(s -> s.getAdminName() == null).forEach(s -> s.setAdminName("未分配管理员"));
  1173. List<String> adminNameList = deviceInfoList.stream().map(DeviceInfoDto.Vo::getAdminName).distinct().collect(Collectors.toList());
  1174. List<Long> dIds = deviceInfoList.stream().map(DeviceInfoDto.Vo::getDeviceId).distinct().collect(Collectors.toList());
  1175. List<DeviceSysinfoDto.Vo> deviceSysList = R.feignCheckData(deviceSysinfoService.list(new DeviceSysinfoDto.SelectList().setDeviceIds(dIds)));
  1176. Map<Long, DeviceSysinfoDto.Vo> deviceSysInfoMap = new HashMap<>();
  1177. if (CollUtil.isNotEmpty(deviceSysList)) {
  1178. deviceSysInfoMap = deviceSysList.stream().collect(Collectors.toMap(DeviceSysinfoDto.Vo::getDeviceId, i -> i));
  1179. }
  1180. // 根据管理员名字分组
  1181. Map<String, List<DeviceInfoDto.Vo>> deviceMap = deviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfoDto.Vo::getAdminName));
  1182. DateTime date = DateTime.now();
  1183. DateTime start = DateUtil.beginOfDay(date);
  1184. DateTime end = DateUtil.endOfDay(date);
  1185. for (String adminName : adminNameList) {
  1186. DeviceInfoDto.MercHomeListVO vo = new DeviceInfoDto.MercHomeListVO();
  1187. vo.setAdminName(adminName);
  1188. // 区域下的设备列表
  1189. List<DeviceInfoDto.MercHomeDeviceVo> deviceInfos = BeanUtil.copyToList(deviceMap.get(adminName), DeviceInfoDto.MercHomeDeviceVo.class);
  1190. vo.setDeviceNum(deviceInfos.size());
  1191. if (CollUtil.isEmpty(deviceInfos)) {
  1192. continue;
  1193. }
  1194. // 设备销售统计
  1195. List<Long> deviceIds = deviceInfos.stream().map(DeviceInfoDto.MercHomeDeviceVo::getDeviceId).collect(Collectors.toList());
  1196. // 设备状态查询
  1197. List<DeviceStatusDto.Vo> deviceStatusList = deviceStatusService.list(new DeviceStatusDto.SelectList().setDeviceIds(deviceIds)).getData();
  1198. Map<Long, DeviceStatusDto.Vo> datdeviceStatusMap = new HashMap<>();
  1199. if (CollUtil.isNotEmpty(deviceStatusList)) {
  1200. datdeviceStatusMap = deviceStatusList.stream().collect(Collectors.toMap(DeviceStatusDto.Vo::getDeviceId, d -> d));
  1201. }
  1202. List<GoodsDeviceDto.Vo> goodsDeviceList = R.feignCheckData(goodsService.queryGoodsDeviceInfo(new GoodsDto.QueryGoodsDeviceInfo().setMercId(mercId).setDeviceIds(deviceIds)));
  1203. Map<Long, List<GoodsDeviceDto.Vo>> deviceIdGoodsMap = goodsDeviceList.stream()
  1204. .collect(Collectors.groupingBy(GoodsDeviceDto.Vo::getDeviceId));
  1205. List<AlgorithmDto.ListNameId> algorithmList = R.feignCheckData(algorithmService.ListNameId());
  1206. Map<Long, String> algorithmListMap = algorithmList.stream().collect(Collectors.toMap(AlgorithmDto.ListNameId::getId, AlgorithmDto.ListNameId::getAlias));
  1207. for (DeviceInfoDto.MercHomeDeviceVo device : deviceInfos) {
  1208. Long dId = device.getDeviceId();
  1209. // 设备类型 反显
  1210. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(device.getDeviceType()));
  1211. device.setDeviceTypeName(dictDeviceType.getMsg());
  1212. // 运营状态 反显
  1213. SysDictRedis dictBusyState = SysDictUtils.get(DictConsts.DEVICE_BUSY_STATUS, String.valueOf(device.getBusyState()));
  1214. device.setBusyStateName(dictBusyState.getMsg());
  1215. Integer zero = new Integer(0);
  1216. CountDto.OrderByCreateTimeAndMercId orderByCreateTimeAndMercId = new CountDto.OrderByCreateTimeAndMercId()
  1217. .setMerdId(mercId).setBeginTime(start).setEndTime(end).setDeviceIds(CollUtil.newArrayList(dId));
  1218. log.info("设备订单统计:{}", JSONUtil.toJsonPrettyStr(orderByCreateTimeAndMercId));
  1219. // 完成订单
  1220. CountDto.SuccessVo successVo = R.feignCheckData(countApiService.orderBySuccess(orderByCreateTimeAndMercId));
  1221. // 今日销售、库存情况 反显
  1222. device.setDayOrderNum(successVo != null ? successVo.getOrdersSize() : zero);
  1223. device.setDaySalesPrice(successVo != null ? successVo.getOrderTotalMoney() : zero);
  1224. DeviceStatusDto.Vo deviceStatus = datdeviceStatusMap.get(device.getDeviceId());
  1225. if (CollUtil.isNotEmpty(deviceIdGoodsMap)) {
  1226. List<GoodsDeviceDto.Vo> goodsDevice = deviceIdGoodsMap.get(dId);
  1227. if (CollUtil.isNotEmpty(goodsDevice)) {
  1228. //过滤商品id为1 和 2的商品
  1229. goodsDevice = goodsDevice.stream().filter(g -> g.getGoodsId() != 1 && g.getGoodsId() != 2).collect(Collectors.toList());
  1230. device.setOnSaleNum(goodsDevice.stream().mapToInt(GoodsDeviceDto.Vo::getStock).sum());
  1231. device.setFillNum(goodsDevice.stream().mapToInt(GoodsDeviceDto.Vo::getFillCount).sum());
  1232. }
  1233. }
  1234. // 温控仪 反显
  1235. DeviceSysinfoDto.Vo dSysInfo = deviceSysInfoMap.get(dId);
  1236. if (dSysInfo == null) {
  1237. device.setIsHaveTemp(false);
  1238. } else {
  1239. device.setIsHaveTemp(BooleanUtil.isTrue(dSysInfo.getIsHaveTemp()));
  1240. }
  1241. // 算法類型
  1242. Long algorithmId = device.getAlgorithmId();
  1243. if (algorithmId != null) {
  1244. String name = algorithmListMap.get(algorithmId);
  1245. device.setAlgorithmAlias(name);
  1246. }
  1247. // 联网状态
  1248. Integer netState = deviceStatus == null ? DeviceNetSateType.DISCONNECT.getCode() : deviceStatus.getNetState();
  1249. device.setNetState(netState);
  1250. if (netState == null) {
  1251. device.setNetStateName(StrUtil.EMPTY);
  1252. } else {
  1253. device.setNetStateName(DeviceNetSateType.getEnumByCode(netState).getDescription());
  1254. }
  1255. if (deviceStatus != null) {
  1256. Integer deviceStateL = deviceStatus.getLockStateL();
  1257. Integer deviceStateR = deviceStatus.getLockStateR();
  1258. device.setTempValue(deviceStatus.getTempValue());
  1259. device.setDeviceStateL(deviceStateL);
  1260. device.setDeviceStateR(deviceStateR);
  1261. Boolean isUseBattery = deviceStatus.getIsUseBattery();
  1262. if (BooleanUtil.isTrue(isUseBattery)) {
  1263. // 使用电池。即断电状态
  1264. device.setSysPower(2);
  1265. } else {
  1266. device.setSysPower(1);
  1267. }
  1268. DeviceLockState deviceLockStateL = DeviceLockState.getEnumByCode(deviceStateL);
  1269. DeviceLockState deviceLockStateR = DeviceLockState.getEnumByCode(deviceStateR);
  1270. device.setDeviceStateRName(deviceLockStateR == null ? "未知" : deviceLockStateR.getDescription());
  1271. device.setDeviceStateLName(deviceLockStateL == null ? "未知" : deviceLockStateL.getDescription());
  1272. }
  1273. }
  1274. // 名称排序
  1275. if (CollUtil.isNotEmpty(deviceInfos)) {
  1276. deviceInfos = ListUtil.sortByProperty(deviceInfos, LambdaUtils.getProperty(DeviceInfoDto.MercHomeDeviceVo::getDeviceName));
  1277. deviceInfos = ListUtil.sortByProperty(deviceInfos, LambdaUtils.getProperty(DeviceInfoDto.MercHomeDeviceVo::getDeviceId));
  1278. }
  1279. vo.setDeviceInfos(deviceInfos);
  1280. dataList.add(vo);
  1281. }
  1282. DeviceInfoDto.MercHomeListVO orderTemp = null;
  1283. for (int i = 0; i < dataList.size(); i++) {
  1284. if ("未分配管理员".equals(dataList.get(i).getAdminName())) {
  1285. orderTemp = dataList.get(i);
  1286. dataList.remove(i);
  1287. dataList.add(orderTemp);
  1288. break;
  1289. }
  1290. }
  1291. return R.ok(dataList);
  1292. }
  1293. /**
  1294. * 获取商户设备列表
  1295. *
  1296. * @param mercId
  1297. * @return
  1298. */
  1299. private List<DeviceInfo> getDevicesByMercId(Long mercId) {
  1300. return list(Wrappers.<DeviceInfo>lambdaQuery().eq(DeviceInfo::getMercId, mercId).eq(DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode()));
  1301. }
  1302. @Override
  1303. @ApiOperation("通用商户设备搜索组件")
  1304. public R<PageBean<DeviceInfoDto.SimpleDeviceSearchPageVo>> simpleDeviceSearchPage(@RequestBody DeviceInfoDto.SimpleDeviceSearchPageDto dto) {
  1305. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<DeviceInfo>()
  1306. .select(DeviceInfo::getDeviceId, DeviceInfo::getDeviceName)
  1307. .and(Emptys.check(dto.getSearchKey()), wrapper -> {
  1308. wrapper.like(DeviceInfo::getDeviceId, dto.getSearchKey())
  1309. .or()
  1310. .like(DeviceInfo::getDeviceName, dto.getSearchKey());
  1311. })
  1312. .in(Emptys.check(dto.getMyDeviceIds()), DeviceInfo::getDeviceId, dto.getMyDeviceIds())
  1313. .eq(DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode())
  1314. .eq(Emptys.check(dto.getMercId()), DeviceInfo::getMercId, dto.getMercId());
  1315. IPage page = page(toIPage(dto.getPage()), lqw);
  1316. PageBean<DeviceInfoDto.SimpleDeviceSearchPageVo> simpleDeviceSearchPageVoPageBean = toPageBean(DeviceInfoDto.SimpleDeviceSearchPageVo.class, page);
  1317. return R.ok(simpleDeviceSearchPageVoPageBean);
  1318. }
  1319. /**
  1320. * 小程序商户设备搜索
  1321. *
  1322. * @param page
  1323. * @return
  1324. */
  1325. @Override
  1326. public R<PageBean<DeviceInfoDto.MerHomeSearchVO>> mercDeviceSearchPage(@RequestBody DeviceInfoDto.Page page) {
  1327. PageBean<DeviceInfoDto.MerHomeSearchVO> pageData = new PageBean<>();
  1328. // 小程序独有查询字段 缺货状态:stockStatus ,是否查故障设备:fault
  1329. Boolean fault = page.getFault();
  1330. Long mercId = page.getMercId();
  1331. List<Long> myDeviceIds = page.getMyDeviceIds();
  1332. if (CollUtil.isEmpty(myDeviceIds)) {
  1333. // 无设备
  1334. return R.ok(pageData);
  1335. }
  1336. if (BooleanUtil.isTrue(fault)) {
  1337. // 查询故障设备
  1338. List<DeviceEventMsg> deviceEventMsgs = deviceEventMsgService.list(Wrappers.<DeviceEventMsg>lambdaQuery()
  1339. .eq(mercId != null, DeviceEventMsg::getMercId, page.getMercId())
  1340. .in(DeviceEventMsg::getDeviceId, myDeviceIds));
  1341. List<Long> deviceIdList = deviceEventMsgs.stream().map(DeviceEventMsg::getDeviceId).collect(Collectors.toList());
  1342. if (CollUtil.isEmpty(deviceIdList)) {
  1343. return R.ok(new PageBean<>());
  1344. }
  1345. page.setDeviceIdList(deviceIdList);
  1346. } else {
  1347. page.setDeviceIdList(myDeviceIds);
  1348. }
  1349. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  1350. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  1351. if (CollUtil.isEmpty(records)) {
  1352. return R.ok(pageData);
  1353. }
  1354. List<DeviceInfoDto.MerHomeSearchVO> merHomeSearchRecords = new ArrayList<>();
  1355. BeanUtil.copyProperties(pageBean, pageData);
  1356. if (CollUtil.isNotEmpty(records)) {
  1357. List<Long> deviceIds = records.stream().map(DeviceInfoDto.Vo2::getDeviceId).collect(Collectors.toList());
  1358. String type = EnumDeviceDataType.DAY.getCode();
  1359. String todayDate = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
  1360. // 查询当天
  1361. DeviceDataDto.ListDTO dto = new DeviceDataDto.ListDTO()
  1362. .setDeviceIds(deviceIds).setType(type).setDateValue(Integer.valueOf(todayDate)).setMercId(mercId);
  1363. List<DeviceDataDto.Vo> deviceDataList = deviceDataService.list(dto);
  1364. Map<Long, DeviceDataDto.Vo> dataMap = MapUtil.newHashMap();
  1365. // 统计数据反显
  1366. if (CollUtil.isNotEmpty(deviceDataList)) {
  1367. dataMap = deviceDataList.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDeviceId, d -> d));
  1368. }
  1369. for (DeviceInfoDto.Vo2 v : records) {
  1370. DeviceInfoDto.MerHomeSearchVO merHomeSearchVO = new DeviceInfoDto.MerHomeSearchVO();
  1371. Long deviceId = v.getDeviceId();
  1372. DeviceDataDto.Vo vo = dataMap.get(deviceId);
  1373. if (vo != null) {
  1374. // 今日订单数
  1375. v.setDayOrderNum(vo != null ? vo.getSalesCount() : 0);
  1376. v.setDaySalesPrice(vo != null ? vo.getSalesMoney() : 0);
  1377. BeanUtil.copyProperties(vo, merHomeSearchVO);
  1378. }
  1379. DeviceSysinfoDto.Vo deviceSysinfo = v.getDeviceSysinfo();
  1380. DeviceStatusDto.Vo deviceStatus = v.getDeviceStatus();
  1381. merHomeSearchVO.setAppUpmVersion(deviceSysinfo.getAppUpmVersion());
  1382. merHomeSearchVO.setTempValue(deviceStatus.getTempValue());
  1383. merHomeSearchVO.setNetDbm(deviceStatus.getNetDbm());
  1384. Integer deviceStateL = deviceStatus.getLockStateL();
  1385. Integer deviceStateR = deviceStatus.getLockStateR();
  1386. merHomeSearchVO.setDeviceStateL(deviceStateL);
  1387. merHomeSearchVO.setDeviceStateR(deviceStateR);
  1388. DeviceLockState deviceLockStateL = DeviceLockState.getEnumByCode(deviceStateL);
  1389. DeviceLockState deviceLockStateR = DeviceLockState.getEnumByCode(deviceStateR);
  1390. merHomeSearchVO.setDeviceStateRName(deviceLockStateR == null ? "未知" : deviceLockStateR.getDescription());
  1391. merHomeSearchVO.setDeviceStateLName(deviceLockStateL == null ? "未知" : deviceLockStateL.getDescription());
  1392. merHomeSearchRecords.add(merHomeSearchVO);
  1393. }
  1394. pageData.setRecords(merHomeSearchRecords);
  1395. }
  1396. return R.ok(pageData);
  1397. }
  1398. /**
  1399. * 小程序商户设备详情-数据统计
  1400. *
  1401. * @param dto
  1402. * @return
  1403. */
  1404. @Override
  1405. @ApiOperation("商户设备详情-数据统计")
  1406. public R<DeviceInfoDto.DeviceDataCountVO> dataCount(DeviceInfoDto.DeviceDataCountDTO dto) {
  1407. // 统计类型(1=经营数据,2=经营图表,3=温度图表,4=信号图表,5=商品管理)
  1408. Integer type = dto.getType();
  1409. switch (type) {
  1410. case 1:
  1411. // 经营数据
  1412. return R.ok(dataCount1(dto));
  1413. case 2:
  1414. // 经营图表
  1415. return R.ok(dataCount2(dto));
  1416. case 3:
  1417. // 温度图表
  1418. return R.ok(dataCount3(dto));
  1419. case 4:
  1420. // 信号图表
  1421. return R.ok(dataCount4(dto));
  1422. case 5:
  1423. // 商品管理
  1424. return R.ok(dataCount5(dto));
  1425. default:
  1426. break;
  1427. }
  1428. return R.ok();
  1429. }
  1430. /**
  1431. * 经营数据
  1432. *
  1433. * @param dto
  1434. * @return
  1435. */
  1436. private DeviceInfoDto.DeviceDataCountVO dataCount1(DeviceInfoDto.DeviceDataCountDTO dto) {
  1437. Long deviceId = dto.getDeviceId();
  1438. Long mercId = dto.getMercId();
  1439. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1440. // 当天
  1441. DateTime date = DateTime.now();
  1442. DateTime start = DateUtil.beginOfDay(date);
  1443. DateTime end = DateUtil.endOfDay(date);
  1444. List<Long> deviceIds = CollUtil.newArrayList(deviceId);
  1445. CountDto.OrderByCreateTimeAndMercId orderByCreateTimeAndMercId = new CountDto.OrderByCreateTimeAndMercId()
  1446. .setMerdId(mercId).setBeginTime(start).setEndTime(end).setDeviceIds(deviceIds);
  1447. // 完成订单
  1448. CountDto.SuccessVo successVo = R.feignCheckData(countApiService.orderBySuccess(orderByCreateTimeAndMercId));
  1449. DeviceDataDto.Vo dayData = new DeviceDataDto.Vo();
  1450. if (successVo != null) {
  1451. dayData.setSalesMoney(successVo.getOrderTotalMoney());
  1452. dayData.setRefundMoney(successVo.getRefundMoney());
  1453. dayData.setSalesCount(successVo.getOrdersSize());
  1454. }
  1455. // 当月
  1456. DeviceDataDto.Vo monthData = new DeviceDataDto.Vo();
  1457. // 月度统计
  1458. DateTime startM = DateUtil.beginOfMonth(date);
  1459. DateTime endM = DateUtil.endOfMonth(date);
  1460. CountDto.OrderByCreateTimeAndMercId monthOrder = new CountDto.OrderByCreateTimeAndMercId()
  1461. .setMerdId(mercId).setBeginTime(startM).setEndTime(endM).setDeviceIds(deviceIds);
  1462. // 完成订单
  1463. CountDto.SuccessVo successVoM = R.feignCheckData(countApiService.orderBySuccess(monthOrder));
  1464. if (monthData != null) {
  1465. monthData.setRefundMoney(successVoM.getRefundMoney());
  1466. monthData.setSalesMoney(successVoM.getOrderTotalMoney());
  1467. monthData.setSalesCount(successVoM.getOrdersSize());
  1468. }
  1469. vo.setDayBusinessData(copy(DeviceInfoDto.BusinessData.class, dayData));
  1470. vo.setMonthBusinessData(copy(DeviceInfoDto.BusinessData.class, monthData));
  1471. return vo;
  1472. }
  1473. /**
  1474. * 经营图表
  1475. *
  1476. * @param dto
  1477. * @return
  1478. */
  1479. private DeviceInfoDto.DeviceDataCountVO dataCount2(DeviceInfoDto.DeviceDataCountDTO dto) {
  1480. Long deviceId = dto.getDeviceId();
  1481. // 近一个月 (销售额,订单数,退款金额,退款数)
  1482. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1483. DeviceInfoDto.BusinessChart businessChart = new DeviceInfoDto.BusinessChart();
  1484. List<String> categories = DataTime.dayListByLastDay(30);
  1485. List<Integer> dateList = new ArrayList<>(30);
  1486. businessChart.setCategories(categories);
  1487. categories.forEach(d -> {
  1488. dateList.add(Integer.valueOf(d.replaceAll(StrUtil.DASHED, StrUtil.EMPTY)));
  1489. });
  1490. Integer startDay = dateList.get(0);
  1491. Integer endDay = dateList.get(categories.size() - 1);
  1492. List<DeviceDataDto.Vo> listByDay = deviceDataService.getListByDay(deviceId, startDay, endDay);
  1493. if (CollUtil.isEmpty(listByDay)) {
  1494. return vo;
  1495. }
  1496. // 每天的数据
  1497. Map<Integer, DeviceDataDto.Vo> dataDayMap = listByDay.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDateValue, i -> i));
  1498. List<DeviceInfoDto.MyChartSeries3> series = new ArrayList<>();
  1499. String[] names = {"销售额", "订单数", "退款金额", "退款数"};
  1500. for (int i = 0; i < names.length; i++) {
  1501. DeviceInfoDto.MyChartSeries3 myChartSeries = new DeviceInfoDto.MyChartSeries3();
  1502. // 某个类型每天的数据
  1503. List<String> data = new ArrayList<>();
  1504. if (i == 0) {
  1505. // 销售额
  1506. dateList.forEach(d -> {
  1507. // 每日数据填充
  1508. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1509. if (deviceData == null) {
  1510. data.add(String.valueOf(BigDecimal.ZERO));
  1511. } else {
  1512. Integer salesMoney = deviceData.getSalesMoney() == null ? 0 : deviceData.getSalesMoney();
  1513. BigDecimal day = BigDecimal.valueOf(salesMoney).divide(BigDecimal.valueOf(100));
  1514. data.add(String.valueOf(day));
  1515. }
  1516. });
  1517. } else if (i == 1) {
  1518. // 订单数
  1519. dateList.forEach(d -> {
  1520. // 每日数据填充
  1521. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1522. String value = "0";
  1523. if (deviceData == null) {
  1524. data.add(value);
  1525. } else {
  1526. data.add(deviceData == null ? value : String.valueOf(deviceData.getSalesCount()));
  1527. }
  1528. });
  1529. } else if (i == 2) {
  1530. // 退款金额
  1531. dateList.forEach(d -> {
  1532. // 每日数据填充
  1533. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1534. if (deviceData == null) {
  1535. data.add(String.valueOf(BigDecimal.ZERO));
  1536. } else {
  1537. Integer refundMoney = deviceData.getRefundMoney() == null ? 0 : deviceData.getRefundMoney();
  1538. BigDecimal day = BigDecimal.valueOf(refundMoney).divide(BigDecimal.valueOf(100));
  1539. data.add(String.valueOf(day));
  1540. }
  1541. });
  1542. } else if (i == 3) {
  1543. // 退款数
  1544. dateList.forEach(d -> {
  1545. // 每日数据填充
  1546. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1547. if (deviceData == null) {
  1548. data.add("0");
  1549. } else {
  1550. data.add(deviceData == null ? "0" : String.valueOf(deviceData.getRefundCount()));
  1551. }
  1552. });
  1553. }
  1554. myChartSeries.setName(names[i]);
  1555. myChartSeries.setData(data);
  1556. series.add(myChartSeries);
  1557. }
  1558. businessChart.setSeries(series);
  1559. vo.setBusinessChart(businessChart);
  1560. return vo;
  1561. }
  1562. /**
  1563. * 温度图表
  1564. *
  1565. * @param dto
  1566. * @return
  1567. */
  1568. private DeviceInfoDto.DeviceDataCountVO dataCount3(DeviceInfoDto.DeviceDataCountDTO dto) {
  1569. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1570. Long deviceId = dto.getDeviceId();
  1571. String choosDate = dto.getChoosDate();
  1572. if (StrUtil.isEmpty(choosDate)) {
  1573. choosDate = DateUtil.formatDate(new Date());
  1574. }
  1575. String startTime = choosDate + " 00:00:00";
  1576. String endTime = choosDate + " 23:59:59";
  1577. // 查询选定日期的温度数据
  1578. List<DeviceTempRecords> deviceTempRecords = deviceTempRecordsService.list(Wrappers.<DeviceTempRecords>lambdaQuery()
  1579. .eq(DeviceTempRecords::getDeviceId, deviceId).between(DeviceTempRecords::getCreateTime, startTime, endTime)
  1580. .orderBy(true, true, DeviceTempRecords::getCreateTime));
  1581. if (CollUtil.isEmpty(deviceTempRecords)) {
  1582. return vo;
  1583. }
  1584. DeviceInfoDto.TemperatureChart temperatureChart = new DeviceInfoDto.TemperatureChart();
  1585. List<String> categories = new ArrayList<>();
  1586. String name = "温度";
  1587. List<DeviceInfoDto.MyChartSeries2> series = new ArrayList<>();
  1588. List<BigDecimal> data = new ArrayList<>();
  1589. deviceTempRecords.forEach(d -> {
  1590. Integer tempValue = d.getTempValue();
  1591. LocalDateTime createTime = d.getCreateTime();
  1592. String time = DateUtil.format(createTime, "HH:mm:ss");
  1593. categories.add(time);
  1594. data.add(tempValue == null ? null : NumberUtil.toBigDecimal(tempValue));
  1595. });
  1596. DeviceInfoDto.MyChartSeries2 myChartSeries = new DeviceInfoDto.MyChartSeries2();
  1597. myChartSeries.setName(name);
  1598. myChartSeries.setData(data);
  1599. series.add(myChartSeries);
  1600. temperatureChart.setSeries(series);
  1601. temperatureChart.setCategories(categories);
  1602. vo.setTemperatureChart(temperatureChart);
  1603. return vo;
  1604. }
  1605. /**
  1606. * 信号图表
  1607. *
  1608. * @param dto
  1609. * @return
  1610. */
  1611. private DeviceInfoDto.DeviceDataCountVO dataCount4(DeviceInfoDto.DeviceDataCountDTO dto) {
  1612. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1613. Long deviceId = dto.getDeviceId();
  1614. String choosDate = dto.getChoosDate();
  1615. if (StrUtil.isEmpty(choosDate)) {
  1616. choosDate = DateUtil.formatDate(new Date());
  1617. }
  1618. String startTime = choosDate + " 00:00:00";
  1619. String endTime = choosDate + " 23:59:59";
  1620. // 查询选定日期的温度数据
  1621. List<DeviceNetRecord> deviceNetRecords = deviceNetRecordService.list(Wrappers.<DeviceNetRecord>lambdaQuery()
  1622. .eq(DeviceNetRecord::getDeviceId, deviceId).between(DeviceNetRecord::getCreateTime, startTime, endTime)
  1623. .orderBy(true, true, DeviceNetRecord::getCreateTime));
  1624. if (CollUtil.isEmpty(deviceNetRecords)) {
  1625. return vo;
  1626. }
  1627. DeviceInfoDto.SignalChart signalChart = new DeviceInfoDto.SignalChart();
  1628. List<String> categories = new ArrayList<>();
  1629. String name = "信号";
  1630. List<DeviceInfoDto.MyChartSeries> series = new ArrayList<>();
  1631. List<Integer> data = new ArrayList<>();
  1632. deviceNetRecords.forEach(d -> {
  1633. Integer value = d.getSimDbm();
  1634. LocalDateTime createTime = d.getCreateTime();
  1635. String time = DateUtil.format(createTime, "HH:mm:ss");
  1636. categories.add(time);
  1637. data.add(value);
  1638. });
  1639. DeviceInfoDto.MyChartSeries myChartSeries = new DeviceInfoDto.MyChartSeries();
  1640. myChartSeries.setName(name);
  1641. myChartSeries.setData(data);
  1642. series.add(myChartSeries);
  1643. signalChart.setSeries(series);
  1644. signalChart.setCategories(categories);
  1645. vo.setSignalChart(signalChart);
  1646. return vo;
  1647. }
  1648. /**
  1649. * 商品管理
  1650. *
  1651. * @param dto
  1652. * @return
  1653. */
  1654. private DeviceInfoDto.DeviceDataCountVO dataCount5(DeviceInfoDto.DeviceDataCountDTO dto) {
  1655. Long deviceId = dto.getDeviceId();
  1656. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1657. DeviceInfoDto.GoodsData goodsData = new DeviceInfoDto.GoodsData();
  1658. // 在售商品种类
  1659. // 根据设备ID查商品id
  1660. GoodsDeviceDto.SelectList selectList = new GoodsDeviceDto.SelectList();
  1661. selectList.setDeviceIds(CollUtil.newArrayList(deviceId));
  1662. selectList.setMercId(dto.getMercId());
  1663. R<List<GoodsDeviceDto.Vo>> r = R.feignCheck(goodsDeviceService.list(selectList));
  1664. List<GoodsDeviceDto.Vo> goodsDeviceList = r.getData();
  1665. Integer stock = 0;
  1666. Integer afterFillStock = 0;
  1667. if (CollUtil.isNotEmpty(goodsDeviceList)) {
  1668. //goodsDeviceList 过滤商品id=1和商品id=2的商品
  1669. goodsDeviceList = goodsDeviceList.stream().filter(g -> g.getGoodsId() != 1 && g.getGoodsId() != 2).collect(Collectors.toList());
  1670. stock = goodsDeviceList.stream().mapToInt(GoodsDeviceDto.Vo::getStock).sum();
  1671. afterFillStock = goodsDeviceList.stream().mapToInt(GoodsDeviceDto.Vo::getFillCount).sum();
  1672. }
  1673. goodsData.setAfterFillStock(afterFillStock);
  1674. goodsData.setStock(stock);
  1675. goodsData.setCategoryNum(CollUtil.isNotEmpty(goodsDeviceList) ? goodsDeviceList.size() : new Integer(0));
  1676. vo.setGoodsData(goodsData);
  1677. return vo;
  1678. }
  1679. public PageBean<DeviceInfoDto.Vo2> queryPage(DeviceInfoDto.Page page) {
  1680. IPage<DeviceInfoQueryPage> iPage = baseMapper.queryPage(toIPage(page.getPage()), page);
  1681. PageBean<DeviceInfoDto.Vo2> pageBean = toPageBean(DeviceInfoDto.Vo2.class, iPage);
  1682. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  1683. JMap<Long, List<DevicePart>> cover;
  1684. if (Emptys.check(records)) {
  1685. List<DevicePart> deviceParts = devicePartService.list(new LambdaQueryWrapper<DevicePart>()
  1686. .in(DevicePart::getDeviceId, new JArrayList<>(records).getProperty(DeviceInfoDto.Vo2::getDeviceId))
  1687. .orderByDesc(DevicePart::getCode)
  1688. );
  1689. if (Emptys.check(deviceParts)) {
  1690. cover = new JArrayList<>(deviceParts).toMap(DevicePart::getDeviceId).group();
  1691. records.forEach(vo2 -> {
  1692. List<DevicePart> devicePartss = cover.get(vo2.getDeviceId());
  1693. if (Emptys.check(devicePartss)) {
  1694. vo2.setDeviceParts(copy(DevicePartDto.Vo.class, devicePartss));
  1695. }
  1696. });
  1697. }
  1698. }
  1699. return pageBean;
  1700. }
  1701. private <T> void check(T value, T value2, String msg) {
  1702. if (value.toString().equals(value2.toString())) {
  1703. throw new CommRuntimeException(msg);
  1704. }
  1705. }
  1706. @Override
  1707. @ApiOperation("商户正常设备列表")
  1708. public R<List<DeviceInfoDto.MercNormalListVo>> mercNormalList(@RequestBody @Validated DeviceInfoDto.MercNormalListDto dto) {
  1709. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<DeviceInfo>()
  1710. .select(DeviceInfo::getDeviceId, DeviceInfo::getDeviceName)
  1711. .eq(DeviceInfo::getMercId, dto.getMercId())
  1712. .eq(DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode())
  1713. .eq(DeviceInfo::getBusyState, EnumDeviceBusyStatus.N_1.getCode())
  1714. .eq(DeviceInfo::getShowStatus, true)
  1715. .eq(DeviceInfo::getFreezeStatus, EnumDeviceFreezeStatus.N_1.getCode());
  1716. List<DeviceInfo> list = list(lqw);
  1717. if (!Emptys.check(list)) {
  1718. return R.ok(new ArrayList<>());
  1719. }
  1720. List<DeviceInfoDto.MercNormalListVo> copy = copy(DeviceInfoDto.MercNormalListVo.class, list);
  1721. return R.ok(copy);
  1722. }
  1723. }