DeviceInfoServiceImpl.java 66 KB

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