123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- package com.xy.service;
- import cn.hutool.core.bean.BeanUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- 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.dto.DeviceInfoDto;
- 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 lombok.SneakyThrows;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import javax.validation.Valid;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- 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(onConstructor_ = @Lazy)
- @Api(tags = "设备统计数据")
- public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements DeviceDataService {
- private final DeviceInfoServiceImpl deviceInfoService;
- @PostMapping("sumPage")
- @ApiOperation("设备销售统计")
- public R<PageBean<DeviceDataDto.SumPageVo>> sumPage(@RequestBody @Valid DeviceDataDto.SumPageDto dto) {
- PageBean pageBean = dto.getPage();
- String salesCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesCount));
- String salesMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesMoney));
- String goodsCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getGoodsCount));
- String refundMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundMoney));
- String refundCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundCount));
- String riskCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRiskCount));
- String zeroCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getZeroCount));
- String deviceId = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getDeviceId));
- LambdaQueryWrapper<DeviceData> lqw = new QueryWrapper<DeviceData>()
- .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"
- , deviceId, salesCount, salesCount, salesMoney, salesMoney, goodsCount, goodsCount
- , refundMoney, refundMoney, refundCount, refundCount, riskCount, riskCount, zeroCount, zeroCount
- ))
- .orderByAsc("asc".equals(dto.getOrderBy()), dto.getOrderByKey())
- .orderByDesc("desc".equals(dto.getOrderBy()), dto.getOrderByKey())
- .lambda()
- .eq(Emptys.check(dto.getMercId()), DeviceData::getMercId, dto.getMercId())
- .eq(DeviceData::getType, dto.getType())
- .ge(Emptys.check(dto.getBeginDate()), DeviceData::getDateValue, dto.getBeginDate())
- .le(Emptys.check(dto.getEndDate()), DeviceData::getDateValue, dto.getEndDate())
- .groupBy(DeviceData::getDeviceId);
- IPage<DeviceData> ipage = page(toIPage(pageBean), lqw);
- if (Emptys.check(ipage.getRecords())) {
- List<Long> deviceIdList = ipage.getRecords().stream().map(DeviceData::getDeviceId).collect(Collectors.toList());
- Map<Long, String> deviceMap = deviceInfoService.getDeviceNameList(new DeviceInfoDto.DeviceIdDto().setDeviceId(deviceIdList)).getData();
- PageBean<DeviceDataDto.SumPageVo> sumPageVoPageBean = toPageBean(DeviceDataDto.SumPageVo.class, ipage);
- sumPageVoPageBean.getRecords().forEach(i -> {
- i.setDeviceName(deviceMap.get(i.getDeviceId()));
- });
- return R.ok(sumPageVoPageBean);
- }
- return R.ok();
- }
- @PostMapping("sumCount")
- @ApiOperation("设备销售统计总计")
- public R<DeviceDataDto.SumCountVo> sumCount(@RequestBody @Valid DeviceDataDto.SumCountDto dto) {
- String salesCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesCount));
- String salesMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getSalesMoney));
- String goodsCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getGoodsCount));
- String refundMoney = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundMoney));
- String refundCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRefundCount));
- String riskCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getRiskCount));
- String zeroCount = StringTools.humpToLine(LambdaUtils.getProperty(DeviceData::getZeroCount));
- //查询总数
- LambdaQueryWrapper<DeviceData> lqw = new QueryWrapper<DeviceData>()
- .select(String.format("sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s,sum(%s) %s"
- , salesCount, salesCount, salesMoney, salesMoney, goodsCount, goodsCount
- , refundMoney, refundMoney, refundCount, refundCount, riskCount, riskCount, zeroCount, zeroCount
- ))
- .lambda()
- .eq(Emptys.check(dto.getMercId()), DeviceData::getMercId, dto.getMercId())
- .eq(DeviceData::getType, dto.getType())
- .ge(Emptys.check(dto.getBeginDate()), DeviceData::getDateValue, dto.getBeginDate())
- .le(Emptys.check(dto.getEndDate()), DeviceData::getDateValue, dto.getEndDate());
- return R.ok(copy(DeviceDataDto.SumCountVo.class, getOne(lqw)));
- }
- @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));
- }
- @ApiOperation("添加/累加")
- @PostMapping("saveOrAccum")
- public R saveOrAccum(@RequestBody DeviceDataDto.SaveOrAccum saveOrAccum) {
- return new SaveOrAccum().saveOrAccum(this, saveOrAccum);
- }
- /**
- * 集合查询
- *
- * @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) {
- return getOneData(DictSonEnum.DEVICE_DATA_TYPE_DAY.getKey(), deviceId, mercId, dateValue);
- }
- /**
- * 指定查询商户所有设备某天数据
- *
- * @param mercId
- * @param dateValue
- * @return
- */
- public List<DeviceDataDto.Vo> getMercDataOneDay(Long mercId, String dateValue) {
- return getMercListData(DictSonEnum.DEVICE_DATA_TYPE_DAY.getKey(), mercId, dateValue);
- }
- /**
- * 指定查询商户所有设备某月数据
- *
- * @param mercId
- * @param dateValue
- * @return
- */
- public List<DeviceDataDto.Vo> getMercDataOneMonth(Long mercId, String dateValue) {
- return getMercListData(DictSonEnum.DEVICE_DATA_TYPE_MONTH.getKey(), mercId, dateValue);
- }
- /**
- * 指定查询商户某设备某月数据
- *
- * @param deviceId
- * @param mercId
- * @param dateValue
- * @return
- */
- public DeviceDataDto.Vo getByMonth(Long deviceId, Long mercId, String dateValue) {
- return getOneData(DictSonEnum.DEVICE_DATA_TYPE_MONTH.getKey(), deviceId, mercId, dateValue);
- }
- /**
- * 指定查询商户某设备某年数据
- *
- * @param deviceId
- * @param mercId
- * @param dateValue
- * @return
- */
- public DeviceDataDto.Vo getByYear(Long deviceId, Long mercId, String dateValue) {
- return getOneData(DictSonEnum.DEVICE_DATA_TYPE_YEAR.getKey(), 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);
- }
- /**
- * 指定查询商户所有设备某年、月、日数据
- *
- * @param type
- * @param mercId
- * @param dateValue
- * @return
- */
- public List<DeviceDataDto.Vo> getMercListData(String type, Long mercId, String dateValue) {
- LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
- lambdaQueryWrapper.eq(DeviceData::getType, type);
- lambdaQueryWrapper.eq(DeviceData::getMercId, mercId);
- lambdaQueryWrapper.eq(DeviceData::getDateValue, Integer.valueOf(dateValue));
- List<DeviceData> list = list(lambdaQueryWrapper);
- return BeanUtil.copyToList(list, DeviceDataDto.Vo.class);
- }
- /**
- * 日范围内每天数据
- *
- * @param deviceId
- * @param startDateValue
- * @param endDateValue
- * @return
- */
- public List<DeviceDataDto.Vo> getListByDay(Long deviceId, Integer startDateValue, Integer endDateValue) {
- return getListData(DictSonEnum.DEVICE_DATA_TYPE_DAY.getKey(), deviceId, startDateValue, endDateValue);
- }
- /**
- * 日期范围查询
- *
- * @param type
- * @param deviceId
- * @param startDateValue
- * @param endDateValue
- * @return
- */
- public List<DeviceDataDto.Vo> getListData(String type, Long deviceId, Integer startDateValue, Integer endDateValue) {
- LambdaQueryWrapper<DeviceData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
- lambdaQueryWrapper.eq(DeviceData::getType, type);
- lambdaQueryWrapper.eq(DeviceData::getDeviceId, deviceId);
- lambdaQueryWrapper.between(DeviceData::getDateValue, startDateValue, endDateValue);
- lambdaQueryWrapper.orderBy(true, true, DeviceData::getDateValue);
- List<DeviceData> list = list(lambdaQueryWrapper);
- return copy(DeviceDataDto.Vo.class, list);
- }
- public static class SaveOrAccum {
- public R saveOrAccum(DeviceDataServiceImpl deviceDataService, DeviceDataDto.SaveOrAccum saveOrAccum) {
- return new LockUtils()
- .name("saveOrAccum.deviceId")
- .prefix("data_save_accum_")
- .build(SaveOrAccum.class)
- .exec(deviceDataService, saveOrAccum);
- }
- @SneakyThrows
- public R exec(DeviceDataServiceImpl deviceDataService, DeviceDataDto.SaveOrAccum saveOrAccum) {
- LocalDateTime now = LocalDateTime.now();
- String yyyyMMdd = DataTime.toString(now, "yyyyMMdd");
- //获取字典
- Map<String, SysDictRedis> map = SysDictUtils.get(DictEnum.DEVICE_DATA_TYPE.getKey());
- JMap<String, Integer> jMap = new JHashMap<>(map.size());
- map.forEach((type, sysDictRedis) -> {
- Integer dateValue = Integer.valueOf(yyyyMMdd.substring(0, Integer.valueOf(sysDictRedis.getValue())));
- jMap.put(type, dateValue);
- });
- //查询已存在数据
- List<DeviceData> list = deviceDataService.list(new LambdaQueryWrapper<DeviceData>()
- .eq(DeviceData::getDeviceId, saveOrAccum.getDeviceId())
- .in(DeviceData::getDateValue, jMap.toList().value())
- );
- JMap<String, DeviceData> typeMaps = new JArrayList<>(list).toMap(DeviceData::getType).cover();
- //新增或修改的多线程桶map集合
- JMap<String, DeviceData> dbMaps = new JConcurrentHashMap<>(map.size());
- //封装新增或修改对象
- jMap.forEach((type, dateValue) -> {
- DeviceData deviceData = typeMaps.get(type);
- //添加
- if (deviceData == null) {
- 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);
- });
- deviceDataService.saveOrUpdateBatch(dbMaps.toList().value());
- return R.ok();
- }
- }
- }
|