DeviceInfoServiceImpl.java 82 KB

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