DeviceInfoServiceImpl.java 90 KB

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