DeviceInfoServiceImpl.java 101 KB

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