DeviceInfoServiceImpl.java 64 KB

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