DeviceInfoServiceImpl.java 74 KB

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