DeviceDataServiceImpl.java 24 KB

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