123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package com.xy.service;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.xy.annotation.Lock;
- import com.xy.collections.list.JArrayList;
- import com.xy.collections.map.JConcurrentHashMap;
- import com.xy.collections.map.JHashMap;
- import com.xy.collections.map.JMap;
- import com.xy.dto.DeviceDataDto;
- import com.xy.entity.DeviceData;
- import com.xy.entity.SysDictRedis;
- import com.xy.mapper.DeviceDataMapper;
- import com.xy.utils.*;
- import com.xy.utils.enums.DictEnum;
- import com.xy.utils.enums.DictSonEnum;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import org.springframework.stereotype.Service;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.Map;
- import static com.xy.utils.Beans.copy;
- import static com.xy.utils.PlusBeans.toIPage;
- import static com.xy.utils.PlusBeans.toPageBean;
- /**
- * <p>
- * 设备统计数据 服务实现类
- * </p>
- *
- * @author lijin
- * @since 2023-01-11
- */
- @Service
- @AllArgsConstructor
- @Api(tags = "设备统计数据")
- public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements DeviceDataService {
- @PostMapping("obj")
- @ApiOperation("对象查询")
- public R<DeviceDataDto.Vo> obj(@RequestBody DeviceDataDto.Vo vo) {
- LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(vo, DeviceData.class).build();
- List<DeviceData> list = list(lambdaQueryWrapper);
- if (Emptys.check(list)) {
- return R.ok(copy(DeviceDataDto.Vo.class, list).get(0));
- }
- return R.ok();
- }
- @PostMapping("page")
- @ApiOperation("分页查询")
- public R<PageBean<DeviceDataDto.Vo>> page(@RequestBody DeviceDataDto.Page page) {
- PageBean pageBean = page.getPage();
- LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceData.class)
- .ge(DeviceData::getCreateTime, page.getBeginCreateTime())
- .le(DeviceData::getCreateTime, page.getEndCreateTime())
- .build()
- .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceData::getCreateTime);
- IPage<DeviceData> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
- return R.ok(toPageBean(DeviceDataDto.Vo.class, iPage));
- }
- @PostMapping("saveOrAccum")
- @ApiOperation("添加/累加")
- @Lock(value = "saveOrAccum.deviceId", prefix = "data_save_accum_")
- public R saveOrAccum(@RequestBody @Validated DeviceDataDto.SaveOrAccum saveOrAccum) {
- LocalDateTime now = LocalDateTime.now();
- String stringTime = DataTime.toString(now, "yyyyMMdd");
- //获取字典
- Map<String, SysDictRedis> map = SysDictUtils.get(DictEnum.DEVICE_DATA_TYPE.getKey());
- //查询已存在数据
- List<DeviceData> list = list(new LambdaQueryWrapper<DeviceData>()
- .eq(DeviceData::getDeviceId, saveOrAccum.getDeviceId())
- .in(DeviceData::getType, new JHashMap<>(map).toList().key())
- );
- JMap<String, DeviceData> typeMaps = new JArrayList<>(list).toMap(DeviceData::getType).cover();
- //新增或修改的多线程桶map集合
- JMap<String, DeviceData> dbMaps = new JConcurrentHashMap<>(map.size());
- //封装新增或修改对象
- map.forEach((type, sysDictRedis) -> {
- DeviceData deviceData = typeMaps.get(type);
- //添加
- if (deviceData == null) {
- Integer dateValue = Integer.valueOf(stringTime.substring(0, Integer.valueOf(sysDictRedis.getValue())));
- DeviceData saveOrUpdateInfo = copy(DeviceData.class, saveOrAccum)
- .setCreateTime(now)
- .setUpdateTime(now)
- .setType(type)
- .setDateValue(dateValue);
- dbMaps.put(type, saveOrUpdateInfo);
- return;
- }
- //累加
- DeviceData updateDeviceData = new DeviceData().setId(deviceData.getId()).setUpdateTime(now)
- .setSalesMoney(saveOrAccum.getSalesMoney() != null ? saveOrAccum.getSalesMoney() + deviceData.getSalesMoney() : null)
- .setSalesCount(saveOrAccum.getSalesCount() != null ? saveOrAccum.getSalesCount() + deviceData.getSalesCount() : null)
- .setGoodsCount(saveOrAccum.getGoodsCount() != null ? saveOrAccum.getGoodsCount() + deviceData.getGoodsCount() : null)
- .setRefundMoney(saveOrAccum.getRefundMoney() != null ? saveOrAccum.getRefundMoney() + deviceData.getRefundMoney() : null)
- .setRefundCount(saveOrAccum.getRefundCount() != null ? saveOrAccum.getRefundCount() + deviceData.getRefundCount() : null)
- .setZeroCount(saveOrAccum.getZeroCount() != null ? saveOrAccum.getZeroCount() + deviceData.getZeroCount() : null)
- .setDeviceFaultCount(saveOrAccum.getDeviceFaultCount() != null ? saveOrAccum.getDeviceFaultCount() + deviceData.getDeviceFaultCount() : null);
- dbMaps.put(type, updateDeviceData);
- });
- saveOrUpdateBatch(dbMaps.toList().value());
- return R.ok();
- }
- /**
- * 集合查询
- *
- * @param dto
- * @return
- */
- public List<DeviceDataDto.Vo> list(DeviceDataDto.ListDTO dto) {
- LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(dto, DeviceData.class).build();
- List<DeviceData> list = list(lambdaQueryWrapper);
- return copy(DeviceDataDto.Vo.class, list);
- }
- /**
- * 指定查询商户某设备某天数据
- *
- * @param deviceId
- * @param mercId
- * @param dateValue
- * @return
- */
- public DeviceDataDto.Vo getByDay(Long deviceId, Long mercId, String dateValue) {
- String type = SysDictUtils.getValue(DictEnum.DEVICE_DATA_TYPE.getKey(), DictSonEnum.DEVICE_DATA_TYPE_DAY.getKey(), String.class);
- return getOneData(type, deviceId, mercId, dateValue);
- }
- /**
- * 指定查询商户某设备某月数据
- *
- * @param deviceId
- * @param mercId
- * @param dateValue
- * @return
- */
- public DeviceDataDto.Vo getByMonth(Long deviceId, Long mercId, String dateValue) {
- String type = SysDictUtils.getValue(DictEnum.DEVICE_DATA_TYPE.getKey(), DictSonEnum.DEVICE_DATA_TYPE_MONTH.getKey(), String.class);
- return getOneData(type, deviceId, mercId, dateValue);
- }
- /**
- * 指定查询商户某设备某年数据
- *
- * @param deviceId
- * @param mercId
- * @param dateValue
- * @return
- */
- public DeviceDataDto.Vo getByYear(Long deviceId, Long mercId, String dateValue) {
- String type = SysDictUtils.getValue(DictEnum.DEVICE_DATA_TYPE.getKey(), DictSonEnum.DEVICE_DATA_TYPE_YEAR.getKey(), String.class);
- return getOneData(type, deviceId, mercId, dateValue);
- }
- /**
- * 指定查询商户某设备某年、月、日数据
- *
- * @param type
- * @param deviceId
- * @param mercId
- * @param dateValue
- * @return
- */
- public DeviceDataDto.Vo getOneData(String type, Long deviceId, Long mercId, String dateValue) {
- LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
- lambdaQueryWrapper.eq(DeviceData::getType, type);
- lambdaQueryWrapper.eq(DeviceData::getDeviceId, deviceId);
- lambdaQueryWrapper.eq(DeviceData::getMercId, mercId);
- lambdaQueryWrapper.eq(DeviceData::getDateValue, Integer.valueOf(dateValue));
- DeviceData data = getOne(lambdaQueryWrapper);
- return copy(DeviceDataDto.Vo.class, data);
- }
- }
|