DeviceInfoServiceImpl.java 93 KB

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