DeviceDataServiceImpl.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.xy.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.xy.annotation.Lock;
  6. import com.xy.collections.list.JArrayList;
  7. import com.xy.collections.map.JConcurrentHashMap;
  8. import com.xy.collections.map.JHashMap;
  9. import com.xy.collections.map.JMap;
  10. import com.xy.dto.DeviceDataDto;
  11. import com.xy.entity.DeviceData;
  12. import com.xy.entity.SysDictRedis;
  13. import com.xy.mapper.DeviceDataMapper;
  14. import com.xy.utils.*;
  15. import com.xy.utils.enums.DictEnum;
  16. import com.xy.utils.enums.DictSonEnum;
  17. import io.swagger.annotations.Api;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.AllArgsConstructor;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.validation.annotation.Validated;
  22. import org.springframework.web.bind.annotation.PostMapping;
  23. import org.springframework.web.bind.annotation.RequestBody;
  24. import java.time.LocalDateTime;
  25. import java.util.List;
  26. import java.util.Map;
  27. import static com.xy.utils.Beans.copy;
  28. import static com.xy.utils.PlusBeans.toIPage;
  29. import static com.xy.utils.PlusBeans.toPageBean;
  30. /**
  31. * <p>
  32. * 设备统计数据 服务实现类
  33. * </p>
  34. *
  35. * @author lijin
  36. * @since 2023-01-11
  37. */
  38. @Service
  39. @AllArgsConstructor
  40. @Api(tags = "设备统计数据")
  41. public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements DeviceDataService {
  42. @PostMapping("obj")
  43. @ApiOperation("对象查询")
  44. public R<DeviceDataDto.Vo> obj(@RequestBody DeviceDataDto.Vo vo) {
  45. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(vo, DeviceData.class).build();
  46. List<DeviceData> list = list(lambdaQueryWrapper);
  47. if (Emptys.check(list)) {
  48. return R.ok(copy(DeviceDataDto.Vo.class, list).get(0));
  49. }
  50. return R.ok();
  51. }
  52. @PostMapping("page")
  53. @ApiOperation("分页查询")
  54. public R<PageBean<DeviceDataDto.Vo>> page(@RequestBody DeviceDataDto.Page page) {
  55. PageBean pageBean = page.getPage();
  56. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceData.class)
  57. .ge(DeviceData::getCreateTime, page.getBeginCreateTime())
  58. .le(DeviceData::getCreateTime, page.getEndCreateTime())
  59. .build()
  60. .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceData::getCreateTime);
  61. IPage<DeviceData> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
  62. return R.ok(toPageBean(DeviceDataDto.Vo.class, iPage));
  63. }
  64. @PostMapping("saveOrAccum")
  65. @ApiOperation("添加/累加")
  66. @Lock(value = "saveOrAccum.deviceId", prefix = "data_save_accum_")
  67. public R saveOrAccum(@RequestBody @Validated DeviceDataDto.SaveOrAccum saveOrAccum) {
  68. LocalDateTime now = LocalDateTime.now();
  69. String stringTime = DataTime.toString(now, "yyyyMMdd");
  70. //获取字典
  71. Map<String, SysDictRedis> map = SysDictUtils.get(DictEnum.DEVICE_DATA_TYPE.getKey());
  72. //查询已存在数据
  73. List<DeviceData> list = list(new LambdaQueryWrapper<DeviceData>()
  74. .eq(DeviceData::getDeviceId, saveOrAccum.getDeviceId())
  75. .in(DeviceData::getType, new JHashMap<>(map).toList().key())
  76. );
  77. JMap<String, DeviceData> typeMaps = new JArrayList<>(list).toMap(DeviceData::getType).cover();
  78. //新增或修改的多线程桶map集合
  79. JMap<String, DeviceData> dbMaps = new JConcurrentHashMap<>(map.size());
  80. //封装新增或修改对象
  81. map.forEach((type, sysDictRedis) -> {
  82. DeviceData deviceData = typeMaps.get(type);
  83. //添加
  84. if (deviceData == null) {
  85. Integer dateValue = Integer.valueOf(stringTime.substring(0, Integer.valueOf(sysDictRedis.getValue())));
  86. DeviceData saveOrUpdateInfo = copy(DeviceData.class, saveOrAccum)
  87. .setCreateTime(now)
  88. .setUpdateTime(now)
  89. .setType(type)
  90. .setDateValue(dateValue);
  91. dbMaps.put(type, saveOrUpdateInfo);
  92. return;
  93. }
  94. //累加
  95. DeviceData updateDeviceData = new DeviceData().setId(deviceData.getId()).setUpdateTime(now)
  96. .setSalesMoney(saveOrAccum.getSalesMoney() != null ? saveOrAccum.getSalesMoney() + deviceData.getSalesMoney() : null)
  97. .setSalesCount(saveOrAccum.getSalesCount() != null ? saveOrAccum.getSalesCount() + deviceData.getSalesCount() : null)
  98. .setGoodsCount(saveOrAccum.getGoodsCount() != null ? saveOrAccum.getGoodsCount() + deviceData.getGoodsCount() : null)
  99. .setRefundMoney(saveOrAccum.getRefundMoney() != null ? saveOrAccum.getRefundMoney() + deviceData.getRefundMoney() : null)
  100. .setRefundCount(saveOrAccum.getRefundCount() != null ? saveOrAccum.getRefundCount() + deviceData.getRefundCount() : null)
  101. .setZeroCount(saveOrAccum.getZeroCount() != null ? saveOrAccum.getZeroCount() + deviceData.getZeroCount() : null)
  102. .setDeviceFaultCount(saveOrAccum.getDeviceFaultCount() != null ? saveOrAccum.getDeviceFaultCount() + deviceData.getDeviceFaultCount() : null);
  103. dbMaps.put(type, updateDeviceData);
  104. });
  105. saveOrUpdateBatch(dbMaps.toList().value());
  106. return R.ok();
  107. }
  108. /**
  109. * 集合查询
  110. *
  111. * @param dto
  112. * @return
  113. */
  114. public List<DeviceDataDto.Vo> list(DeviceDataDto.ListDTO dto) {
  115. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, DeviceData.class).build();
  116. List<DeviceData> list = list(lambdaQueryWrapper);
  117. return copy(DeviceDataDto.Vo.class, list);
  118. }
  119. /**
  120. * 指定查询商户某设备某天数据
  121. *
  122. * @param deviceId
  123. * @param mercId
  124. * @param dateValue
  125. * @return
  126. */
  127. public DeviceDataDto.Vo getByDay(Long deviceId, Long mercId, String dateValue) {
  128. String type = SysDictUtils.getValue(DictEnum.DEVICE_DATA_TYPE.getKey(), DictSonEnum.DEVICE_DATA_TYPE_DAY.getKey(), String.class);
  129. return getOneData(type, deviceId, mercId, dateValue);
  130. }
  131. /**
  132. * 指定查询商户某设备某月数据
  133. *
  134. * @param deviceId
  135. * @param mercId
  136. * @param dateValue
  137. * @return
  138. */
  139. public DeviceDataDto.Vo getByMonth(Long deviceId, Long mercId, String dateValue) {
  140. String type = SysDictUtils.getValue(DictEnum.DEVICE_DATA_TYPE.getKey(), DictSonEnum.DEVICE_DATA_TYPE_MONTH.getKey(), String.class);
  141. return getOneData(type, deviceId, mercId, dateValue);
  142. }
  143. /**
  144. * 指定查询商户某设备某年数据
  145. *
  146. * @param deviceId
  147. * @param mercId
  148. * @param dateValue
  149. * @return
  150. */
  151. public DeviceDataDto.Vo getByYear(Long deviceId, Long mercId, String dateValue) {
  152. String type = SysDictUtils.getValue(DictEnum.DEVICE_DATA_TYPE.getKey(), DictSonEnum.DEVICE_DATA_TYPE_YEAR.getKey(), String.class);
  153. return getOneData(type, deviceId, mercId, dateValue);
  154. }
  155. /**
  156. * 指定查询商户某设备某年、月、日数据
  157. *
  158. * @param type
  159. * @param deviceId
  160. * @param mercId
  161. * @param dateValue
  162. * @return
  163. */
  164. public DeviceDataDto.Vo getOneData(String type, Long deviceId, Long mercId, String dateValue) {
  165. LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  166. lambdaQueryWrapper.eq(DeviceData::getType, type);
  167. lambdaQueryWrapper.eq(DeviceData::getDeviceId, deviceId);
  168. lambdaQueryWrapper.eq(DeviceData::getMercId, mercId);
  169. lambdaQueryWrapper.eq(DeviceData::getDateValue, Integer.valueOf(dateValue));
  170. DeviceData data = getOne(lambdaQueryWrapper);
  171. return copy(DeviceDataDto.Vo.class, data);
  172. }
  173. }