DeviceDataServiceImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package com.xy.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.date.DateTime;
  5. import cn.hutool.core.date.DateUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.core.metadata.IPage;
  10. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  11. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  12. import com.xy.collections.list.JArrayList;
  13. import com.xy.collections.map.JConcurrentHashMap;
  14. import com.xy.collections.map.JHashMap;
  15. import com.xy.collections.map.JMap;
  16. import com.xy.device.EnumDeviceActiveStatus;
  17. import com.xy.device.EnumDeviceBusyStatus;
  18. import com.xy.device.EnumDeviceDataType;
  19. import com.xy.device.EnumDeviceOnlineStatus;
  20. import com.xy.dto.*;
  21. import com.xy.entity.*;
  22. import com.xy.mapper.DeviceDataMapper;
  23. import com.xy.utils.*;
  24. import io.swagger.annotations.Api;
  25. import io.swagger.annotations.ApiOperation;
  26. import lombok.AllArgsConstructor;
  27. import lombok.SneakyThrows;
  28. import org.springframework.context.annotation.Lazy;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.web.bind.annotation.PostMapping;
  31. import org.springframework.web.bind.annotation.RequestBody;
  32. import javax.validation.Valid;
  33. import java.math.BigDecimal;
  34. import java.time.LocalDateTime;
  35. import java.util.ArrayList;
  36. import java.util.Date;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.stream.Collectors;
  40. import static com.xy.utils.Beans.copy;
  41. import static com.xy.utils.PlusBeans.toIPage;
  42. import static com.xy.utils.PlusBeans.toPageBean;
  43. /**
  44. * <p>
  45. * 设备统计数据 服务实现类
  46. * </p>
  47. *
  48. * @author lijin
  49. * @since 2023-01-11
  50. */
  51. @Service
  52. @AllArgsConstructor(onConstructor_ = @Lazy)
  53. @Api(tags = "设备统计数据")
  54. public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements DeviceDataService {
  55. private final DeviceInfoServiceImpl deviceInfoService;
  56. private final DeviceStatusServiceImpl deviceStatusService;
  57. private final OrdersService ordersService;
  58. private final DeviceChargingServiceImpl deviceChargingService;
  59. /**
  60. * 设备统计
  61. *
  62. * @param dto
  63. * @return {@link DeviceDataDto.MercHomePageCountVO}
  64. */
  65. public DeviceDataDto.MercHomePageCountVO deviceCount(@RequestBody @Valid DeviceDataDto.MercHomePageCount dto) {
  66. DeviceDataDto.MercHomePageCountVO vo = new DeviceDataDto.MercHomePageCountVO()
  67. .setOfflineNum(0).setOnlineNum(0).setUnActiveNum(0).setOutOfServiceNum(0);
  68. Long mercId = dto.getMercId();
  69. List<Long> myDeviceIds = dto.getMyDeviceIds();
  70. if (mercId != null && CollUtil.isEmpty(myDeviceIds)) {
  71. //指定商户查询用,平台查询预留逻辑
  72. List<DeviceInfoDto.Vo> vos = R.feignCheckData(deviceInfoService.list(new DeviceInfoDto.ListDto().setMercId(mercId)));
  73. if (CollUtil.isEmpty(vos)) {
  74. return vo;
  75. }
  76. myDeviceIds = vos.stream().map(DeviceInfoDto.Vo::getDeviceId).collect(Collectors.toList());
  77. }
  78. List<DeviceInfo> mercDevices = deviceInfoService.listByIds(myDeviceIds);
  79. if (CollUtil.isEmpty(mercDevices)) {
  80. return vo;
  81. }
  82. //待激活数量
  83. Long unActiveNum = mercDevices.stream()
  84. .filter(device -> device.getActiveState() == 2)
  85. .count();
  86. //停运状态数量
  87. Long outOfServiceNum = mercDevices.stream()
  88. .filter(device -> device.getBusyState() == 2)
  89. .count();
  90. vo.setOutOfServiceNum(outOfServiceNum.intValue());
  91. vo.setUnActiveNum(unActiveNum.intValue());
  92. List<DeviceStatus> deviceStatuses = deviceStatusService.listByIds(myDeviceIds);
  93. //分组统计
  94. Map<Integer, Long> countNetstateMap = deviceStatuses.stream().collect(Collectors
  95. .groupingBy(DeviceStatus::getNetState, Collectors.counting()));
  96. Integer onlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.CONNECTED.getCode(), Integer.class);
  97. Integer offlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.DISCONNECT.getCode(), Integer.class);
  98. int onlineNum = countNetstateMap.get(onlineDictValue) == null ? 0 : countNetstateMap.get(onlineDictValue).intValue();
  99. int offlineNum = countNetstateMap.get(offlineDictValue) == null ? 0 : countNetstateMap.get(offlineDictValue).intValue();
  100. vo.setOnlineNum(onlineNum);
  101. vo.setOfflineNum(offlineNum);
  102. return vo;
  103. }
  104. @PostMapping("sumPage")
  105. @ApiOperation("设备销售统计")
  106. public R<PageBean<DeviceDataDto.SumPageVo>> sumPage(@RequestBody @Valid DeviceDataDto.SumPageDto dto) {
  107. PageBean pageBean = dto.getPage();
  108. List<Long> mercDeviceIds = dto.getMercDeviceIds();
  109. String salesCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesCount));
  110. String salesMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesMoney));
  111. String goodsCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getGoodsCount));
  112. String refundMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundMoney));
  113. String refundCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundCount));
  114. String riskCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRiskCount));
  115. String zeroCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getZeroCount));
  116. String deviceId = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getDeviceId));
  117. LambdaQueryWrapper<DeviceData> lqw = new QueryWrapper<DeviceData>()
  118. .select(String.format("%s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s"
  119. , deviceId, salesCount, salesCount, salesMoney, salesMoney, goodsCount, goodsCount
  120. , refundMoney, refundMoney, refundCount, refundCount, riskCount, riskCount, zeroCount, zeroCount
  121. ))
  122. .orderByAsc("asc".equals(dto.getOrderBy()), dto.getOrderByKey())
  123. .orderByDesc("desc".equals(dto.getOrderBy()), dto.getOrderByKey())
  124. .lambda()
  125. .eq(Emptys.check(dto.getMercId()), DeviceData::getMercId, dto.getMercId())
  126. .eq(DeviceData::getType, dto.getType())
  127. .ge(Emptys.check(dto.getBeginDate()), DeviceData::getDateValue, dto.getBeginDate())
  128. .le(Emptys.check(dto.getEndDate()), DeviceData::getDateValue, dto.getEndDate())
  129. .in(Emptys.check(mercDeviceIds), DeviceData::getDeviceId, mercDeviceIds)
  130. .groupBy(DeviceData::getDeviceId);
  131. IPage<DeviceData> ipage = page(toIPage(pageBean), lqw);
  132. if (Emptys.check(ipage.getRecords())) {
  133. List<Long> deviceIdList = ipage.getRecords().stream().map(DeviceData::getDeviceId).collect(Collectors.toList());
  134. Map<Long, String> deviceMap = deviceInfoService.getDeviceNameList(new DeviceInfoDto.DeviceIdDto().setDeviceId(deviceIdList)).getData();
  135. PageBean<DeviceDataDto.SumPageVo> sumPageVoPageBean = toPageBean(DeviceDataDto.SumPageVo.class, ipage);
  136. sumPageVoPageBean.getRecords().forEach(i -> {
  137. i.setDeviceName(deviceMap.get(i.getDeviceId()));
  138. });
  139. return R.ok(sumPageVoPageBean);
  140. }
  141. return R.ok();
  142. }
  143. @PostMapping("sumCount")
  144. @ApiOperation("设备销售统计总计")
  145. public R<DeviceDataDto.SumCountVo> sumCount(@RequestBody @Valid DeviceDataDto.SumCountDto dto) {
  146. List<Long> mercDeviceIds = dto.getMercDeviceIds();
  147. String salesCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesCount));
  148. String salesMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesMoney));
  149. String goodsCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getGoodsCount));
  150. String refundMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundMoney));
  151. String refundCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundCount));
  152. String riskCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRiskCount));
  153. String zeroCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getZeroCount));
  154. //查询总数
  155. LambdaQueryWrapper<DeviceData> lqw = new QueryWrapper<DeviceData>()
  156. .select(String.format("sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s"
  157. , salesCount, salesCount, salesMoney, salesMoney, goodsCount, goodsCount
  158. , refundMoney, refundMoney, refundCount, refundCount, riskCount, riskCount, zeroCount, zeroCount
  159. ))
  160. .lambda()
  161. .eq(Emptys.check(dto.getMercId()), DeviceData::getMercId, dto.getMercId())
  162. .eq(DeviceData::getType, dto.getType())
  163. .ge(Emptys.check(dto.getBeginDate()), DeviceData::getDateValue, dto.getBeginDate())
  164. .le(Emptys.check(dto.getEndDate()), DeviceData::getDateValue, dto.getEndDate())
  165. .in(Emptys.check(mercDeviceIds), DeviceData::getDeviceId, mercDeviceIds);
  166. return R.ok(copy(DeviceDataDto.SumCountVo.class, getOne(lqw)));
  167. }
  168. @PostMapping("obj")
  169. @ApiOperation("对象查询")
  170. public R<DeviceDataDto.Vo> obj(@RequestBody DeviceDataDto.Vo vo) {
  171. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(vo, DeviceData.class).build();
  172. List<DeviceData> list = list(lambdaQueryWrapper);
  173. if (Emptys.check(list)) {
  174. return R.ok(copy(DeviceDataDto.Vo.class, list).get(0));
  175. }
  176. return R.ok();
  177. }
  178. @PostMapping("page")
  179. @ApiOperation("分页查询")
  180. public R<PageBean<DeviceDataDto.Vo>> page(@RequestBody DeviceDataDto.Page page) {
  181. PageBean pageBean = page.getPage();
  182. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceData.class)
  183. .ge(DeviceData::getCreateTime, page.getBeginCreateTime())
  184. .le(DeviceData::getCreateTime, page.getEndCreateTime())
  185. .build()
  186. .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceData::getCreateTime);
  187. IPage<DeviceData> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
  188. return R.ok(toPageBean(DeviceDataDto.Vo.class, iPage));
  189. }
  190. @ApiOperation("添加/累加")
  191. @PostMapping("saveOrAccum")
  192. public R saveOrAccum(@RequestBody DeviceDataDto.SaveOrAccum saveOrAccum) {
  193. return new SaveOrAccum().saveOrAccum(saveOrAccum, this);
  194. }
  195. /**
  196. * 集合查询
  197. *
  198. * @param dto
  199. * @return
  200. */
  201. public List<DeviceDataDto.Vo> list(DeviceDataDto.ListDTO dto) {
  202. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, DeviceData.class).build();
  203. List<DeviceData> list = list(lambdaQueryWrapper);
  204. return copy(DeviceDataDto.Vo.class, list);
  205. }
  206. /**
  207. * 指定查询商户某设备某天数据
  208. *
  209. * @param deviceId
  210. * @param mercId
  211. * @param dateValue
  212. * @return
  213. */
  214. public DeviceDataDto.Vo getByDay(Long deviceId, Long mercId, String dateValue) {
  215. return getOneData(EnumDeviceDataType.DAY.getCode(), deviceId, mercId, dateValue);
  216. }
  217. /**
  218. * 指定查询商户所有设备某天数据
  219. *
  220. * @param mercId
  221. * @param dateValue
  222. * @return
  223. */
  224. public List<DeviceDataDto.Vo> getMercDataOneDay(Long mercId, String dateValue) {
  225. return getMercListData(EnumDeviceDataType.DAY.getCode(), mercId, dateValue);
  226. }
  227. /**
  228. * 指定查询商户所有设备某月数据
  229. *
  230. * @param mercId
  231. * @param dateValue
  232. * @return
  233. */
  234. public List<DeviceDataDto.Vo> getMercDataOneMonth(Long mercId, String dateValue) {
  235. return getMercListData(EnumDeviceDataType.MONTH.getCode(), mercId, dateValue);
  236. }
  237. /**
  238. * 指定查询商户某设备某月数据
  239. *
  240. * @param deviceId
  241. * @param mercId
  242. * @param dateValue
  243. * @return
  244. */
  245. public DeviceDataDto.Vo getByMonth(Long deviceId, Long mercId, String dateValue) {
  246. return getOneData(EnumDeviceDataType.MONTH.getCode(), deviceId, mercId, dateValue);
  247. }
  248. /**
  249. * 指定查询商户某设备某年数据
  250. *
  251. * @param deviceId
  252. * @param mercId
  253. * @param dateValue
  254. * @return
  255. */
  256. public DeviceDataDto.Vo getByYear(Long deviceId, Long mercId, String dateValue) {
  257. return getOneData(EnumDeviceDataType.YEAR.getCode(), deviceId, mercId, dateValue);
  258. }
  259. /**
  260. * 指定查询商户某设备某年、月、日数据
  261. *
  262. * @param type
  263. * @param deviceId
  264. * @param mercId
  265. * @param dateValue
  266. * @return
  267. */
  268. public DeviceDataDto.Vo getOneData(String type, Long deviceId, Long mercId, String dateValue) {
  269. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  270. lambdaQueryWrapper.eq(DeviceData::getType, type);
  271. lambdaQueryWrapper.eq(DeviceData::getDeviceId, deviceId);
  272. lambdaQueryWrapper.eq(DeviceData::getMercId, mercId);
  273. lambdaQueryWrapper.eq(DeviceData::getDateValue, Integer.valueOf(dateValue));
  274. DeviceData data = getOne(lambdaQueryWrapper);
  275. return copy(DeviceDataDto.Vo.class, data);
  276. }
  277. /**
  278. * 指定查询商户所有设备某年、月、日数据
  279. *
  280. * @param type
  281. * @param mercId
  282. * @param dateValue
  283. * @return
  284. */
  285. public List<DeviceDataDto.Vo> getMercListData(String type, Long mercId, String dateValue) {
  286. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  287. lambdaQueryWrapper.eq(DeviceData::getType, type);
  288. lambdaQueryWrapper.eq(DeviceData::getMercId, mercId);
  289. lambdaQueryWrapper.eq(DeviceData::getDateValue, Integer.valueOf(dateValue));
  290. List<DeviceData> list = list(lambdaQueryWrapper);
  291. return BeanUtil.copyToList(list, DeviceDataDto.Vo.class);
  292. }
  293. /**
  294. * 日范围内每天数据
  295. *
  296. * @param deviceId
  297. * @param startDateValue
  298. * @param endDateValue
  299. * @return
  300. */
  301. public List<DeviceDataDto.Vo> getListByDay(Long deviceId, Integer startDateValue, Integer endDateValue) {
  302. return getListData(EnumDeviceDataType.DAY.getCode(), deviceId, startDateValue, endDateValue);
  303. }
  304. /**
  305. * 日期范围查询
  306. *
  307. * @param type
  308. * @param deviceId
  309. * @param startDateValue
  310. * @param endDateValue
  311. * @return
  312. */
  313. public List<DeviceDataDto.Vo> getListData(String type, Long deviceId, Integer startDateValue, Integer endDateValue) {
  314. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  315. lambdaQueryWrapper.eq(DeviceData::getType, type);
  316. lambdaQueryWrapper.eq(DeviceData::getDeviceId, deviceId);
  317. lambdaQueryWrapper.between(DeviceData::getDateValue, startDateValue, endDateValue);
  318. lambdaQueryWrapper.orderBy(true, true, DeviceData::getDateValue);
  319. List<DeviceData> list = list(lambdaQueryWrapper);
  320. return copy(DeviceDataDto.Vo.class, list);
  321. }
  322. @Override
  323. public R<List<DeviceDataDto.Vo>> getMercDataOneDay(DeviceDataDto.CountByDay dto) {
  324. return R.ok(this.getMercDataOneDay(dto.getMercId(), dto.getDateValue()));
  325. }
  326. @Override
  327. public R<List<DeviceDataDto.Vo>> getMercDataOneMonth(DeviceDataDto.CountByMonth dto) {
  328. return R.ok(this.getMercDataOneMonth(dto.getMercId(), dto.getDateValue()));
  329. }
  330. @ApiOperation("图表数据")
  331. @Override
  332. public R<List<DeviceDataDto.DayChartVo>> dayChart(@RequestBody @Valid DeviceDataDto.DayChartDto dto) {
  333. List<DeviceDataDto.DayChartVo> dayChartVos = baseMapper.dayChart(dto.getMercId(), dto.getBeginInt(), dto.getEndDateInt(), dto.getType());
  334. Map<Integer, DeviceDataDto.DayChartVo> voMap = dayChartVos.stream().collect(Collectors.toMap(DeviceDataDto.DayChartVo::getTime, i -> i));
  335. List<String> dayList = DataTime.getBetweenDates(dto.getBeginDate().toString(), dto.getEndDate().toString(), true);
  336. List<Integer> dayList2 = dayList.stream().map(i -> Integer.valueOf(StrUtil.replace(i, "-", ""))).collect(Collectors.toList());
  337. List<DeviceDataDto.DayChartVo> list = new ArrayList<>();
  338. dayList2.forEach(
  339. i -> {
  340. Integer num = 0;
  341. Integer totalMoney = 0;
  342. BigDecimal totalMoneyYuan = new BigDecimal(0);
  343. DeviceDataDto.DayChartVo dayChartVo = voMap.get(i);
  344. if (Emptys.check(dayChartVo)) {
  345. num = dayChartVo.getNum();
  346. totalMoney = dayChartVo.getTotalMoney();
  347. totalMoneyYuan = dayChartVo.getTotalMoneyYuan();
  348. }
  349. list.add(new DeviceDataDto.DayChartVo()
  350. .setTime(i)
  351. .setNum(num)
  352. .setTotalMoney(totalMoney)
  353. .setTotalMoneyYuan(totalMoneyYuan)
  354. );
  355. }
  356. );
  357. return R.ok(list);
  358. }
  359. @Override
  360. @ApiOperation("商户日期维度查询")
  361. public R<PageBean<DeviceDataDto.Vo>> pageByMercAndDate(DeviceDataDto.PageByMercAndDate pageByMercAndDate) {
  362. PageBean pageBean = pageByMercAndDate.getPage();
  363. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(pageByMercAndDate, DeviceData.class)
  364. .in(DeviceData::getDeviceId)
  365. .ge(DeviceData::getDateValue, pageByMercAndDate.getBeginDateValue())
  366. .le(DeviceData::getDateValue, pageByMercAndDate.getEndDateValue())
  367. .build()
  368. .orderByDesc(DeviceData::getDeviceId, DeviceData::getDateValue);
  369. IPage<DeviceData> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
  370. return R.ok(toPageBean(DeviceDataDto.Vo.class, iPage));
  371. }
  372. @Override
  373. @ApiOperation("商户日期维度统计查询")
  374. public R<Long> pageByMercAndDateCount(DeviceDataDto.PageByMercAndDate pageByMercAndDate) {
  375. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(pageByMercAndDate, DeviceData.class)
  376. .in(DeviceData::getDeviceId)
  377. .ge(DeviceData::getDateValue, pageByMercAndDate.getBeginDateValue())
  378. .le(DeviceData::getDateValue, pageByMercAndDate.getEndDateValue())
  379. .build();
  380. long count = count(lambdaQueryWrapper);
  381. return R.ok(count);
  382. }
  383. public static class SaveOrAccum {
  384. public R saveOrAccum(DeviceDataDto.SaveOrAccum saveOrAccum, DeviceDataServiceImpl deviceDataService) {
  385. return new LockUtils()
  386. .name("saveOrAccum.deviceId")
  387. .prefix("data_save_accum_")
  388. .build(SaveOrAccum.class)
  389. .exec(saveOrAccum, deviceDataService);
  390. }
  391. @SneakyThrows
  392. public R exec(DeviceDataDto.SaveOrAccum saveOrAccum, DeviceDataServiceImpl deviceDataService) {
  393. LocalDateTime now = LocalDateTime.now();
  394. String yyyyMMdd = DataTime.toString(now, "yyyyMMdd");
  395. //获取字典
  396. Map<String, SysDictRedis> map = SysDictUtils.get(EnumDeviceDataType.Code.CODE.getCode());
  397. JMap<String, Integer> jMap = new JHashMap<>(map.size());
  398. map.forEach((type, sysDictRedis) -> {
  399. Integer dateValue = Integer.valueOf(yyyyMMdd.substring(0, Integer.valueOf(sysDictRedis.getValue())));
  400. jMap.put(type, dateValue);
  401. });
  402. //查询已存在数据
  403. List<DeviceData> list = deviceDataService.list(new LambdaQueryWrapper<DeviceData>()
  404. .eq(DeviceData::getDeviceId, saveOrAccum.getDeviceId())
  405. .in(DeviceData::getDateValue, jMap.toList().value())
  406. );
  407. JMap<String, DeviceData> typeMaps = new JArrayList<>(list).toMap(DeviceData::getType).cover();
  408. //新增或修改的多线程桶map集合
  409. JMap<String, DeviceData> dbMaps = new JConcurrentHashMap<>(map.size());
  410. //封装新增或修改对象
  411. jMap.forEach((type, dateValue) -> {
  412. DeviceData deviceData = typeMaps.get(type);
  413. //添加
  414. if (deviceData == null) {
  415. DeviceData saveOrUpdateInfo = copy(DeviceData.class, saveOrAccum)
  416. .setCreateTime(now)
  417. .setUpdateTime(now)
  418. .setType(type)
  419. .setDateValue(dateValue);
  420. dbMaps.put(type, saveOrUpdateInfo);
  421. return;
  422. }
  423. //累加
  424. DeviceData updateDeviceData = new DeviceData().setId(deviceData.getId()).setUpdateTime(now)
  425. .setSalesMoney(saveOrAccum.getSalesMoney() != null ? saveOrAccum.getSalesMoney() + deviceData.getSalesMoney() : null)
  426. .setSalesCount(saveOrAccum.getSalesCount() != null ? saveOrAccum.getSalesCount() + deviceData.getSalesCount() : null)
  427. .setGoodsCount(saveOrAccum.getGoodsCount() != null ? saveOrAccum.getGoodsCount() + deviceData.getGoodsCount() : null)
  428. .setRefundMoney(saveOrAccum.getRefundMoney() != null ? saveOrAccum.getRefundMoney() + deviceData.getRefundMoney() : null)
  429. .setRefundCount(saveOrAccum.getRefundCount() != null ? saveOrAccum.getRefundCount() + deviceData.getRefundCount() : null)
  430. .setZeroCount(saveOrAccum.getZeroCount() != null ? saveOrAccum.getZeroCount() + deviceData.getZeroCount() : null)
  431. .setDeviceFaultCount(saveOrAccum.getDeviceFaultCount() != null ? saveOrAccum.getDeviceFaultCount() + deviceData.getDeviceFaultCount() : null);
  432. dbMaps.put(type, updateDeviceData);
  433. });
  434. deviceDataService.saveOrUpdateBatch(dbMaps.toList().value());
  435. return R.ok();
  436. }
  437. }
  438. @ApiOperation("设备统计-运营概况")
  439. @PostMapping("countDevice")
  440. R<DeviceDataDto.CountVO> countDevice(@RequestBody DeviceDataDto.CountDTO dto) {
  441. DeviceDataDto.CountVO vo = new DeviceDataDto.CountVO();
  442. Integer timeType = dto.getTimeType();
  443. int day = 0;
  444. if (timeType == 1) {
  445. //天
  446. day = 1;
  447. } else if (timeType == 2) {
  448. //周
  449. day = 7;
  450. } else if (timeType == 3) {
  451. //月
  452. day = 30;
  453. }
  454. Date now = DateUtil.date();
  455. Date queryDate = DateUtil.offsetDay(now, -day);
  456. //新增
  457. Long newAdd = deviceInfoService.count(Wrappers.<DeviceInfo>lambdaQuery()
  458. .ge(DeviceInfo::getCreateTime, queryDate).eq(DeviceInfo::getActiveState, EnumDeviceActiveStatus.N_1.getCode()));
  459. vo.setNewNum(newAdd.intValue());
  460. //活跃 产生订单的
  461. Integer activeNum = R.feignCheckData(ordersService.deviceActiveCount(new CountDto.MercCountDTO().setStartTime(queryDate)));
  462. vo.setActiveNum(activeNum);
  463. //设备管理费
  464. Long debtNum = deviceChargingService.count(Wrappers.<DeviceCharging>lambdaQuery().lt(DeviceCharging::getTimeout, DateUtil.now()));
  465. vo.setDebtNum(debtNum);
  466. Long outOfServiceNum = deviceInfoService.count(Wrappers.<DeviceInfo>lambdaQuery().eq(DeviceInfo::getBusyState, EnumDeviceBusyStatus.N_2.getCode()));
  467. vo.setOutOfServiceNum(outOfServiceNum.intValue());
  468. return R.ok(vo);
  469. }
  470. }