|
@@ -1,16 +1,21 @@
|
|
|
package com.cyksj.service.quota.impl;
|
|
package com.cyksj.service.quota.impl;
|
|
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
|
|
+import cn.hutool.core.lang.UUID;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import com.cyksj.common.constant.Constant;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
-import com.cyksj.mapper.NanoUserQuotaMapper;
|
|
|
|
|
-import com.cyksj.model.entity.NanoUserQuota;
|
|
|
|
|
|
|
+import com.cyksj.mapper.*;
|
|
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.service.quota.NanoUserQuotaService;
|
|
import com.cyksj.service.quota.NanoUserQuotaService;
|
|
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import java.util.Date;
|
|
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 用户固定配额管理服务实现类
|
|
* 用户固定配额管理服务实现类
|
|
@@ -19,49 +24,85 @@ import java.util.Date;
|
|
|
@Service
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
@RequiredArgsConstructor
|
|
|
public class NanoUserQuotaServiceImpl extends ServiceImpl<NanoUserQuotaMapper, NanoUserQuota> implements NanoUserQuotaService {
|
|
public class NanoUserQuotaServiceImpl extends ServiceImpl<NanoUserQuotaMapper, NanoUserQuota> implements NanoUserQuotaService {
|
|
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
|
|
+
|
|
|
|
|
+ private final GroupsRelationMapper groupsRelationMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final GroupsMapper groupsMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final UserMapper userMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public NanoUserQuota getByUserToken(String userToken) {
|
|
|
|
|
- if (userToken == null || userToken.trim().isEmpty()) {
|
|
|
|
|
- throw new BusinessRuntimeException("用户token不能为空");
|
|
|
|
|
|
|
+ public NanoUserQuota getNanoUser(Long userId, Long relationId) {
|
|
|
|
|
+ GroupsRelation groupsRelation = getGroupsRelation(userId, relationId);
|
|
|
|
|
+ GroupsTrips groupsTrips = getGroupsTrips(groupsRelation);
|
|
|
|
|
+ GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
|
|
+ if (goodsDonSku != null && goodsDonSku.getGoodsId() == Constant.NANO_GOODS_ID) {
|
|
|
|
|
+ return getNanoUser(userId, relationId, groupsRelation, goodsDonSku);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("服务器出了点问题");
|
|
|
}
|
|
}
|
|
|
- return this.getOne(new LambdaQueryWrapper<NanoUserQuota>()
|
|
|
|
|
- .eq(NanoUserQuota::getUserToken, userToken)
|
|
|
|
|
- .last("LIMIT 1"));
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public NanoUserQuota getByUid(Long uid) {
|
|
|
|
|
- if (uid == null) {
|
|
|
|
|
- throw new BusinessRuntimeException("用户ID不能为空");
|
|
|
|
|
|
|
+ private GroupsRelation getGroupsRelation(Long userId, Long relationId) {
|
|
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
|
|
+ GroupsRelation groupsRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).in(GroupsRelation::getUserId, userIdList).eq(GroupsRelation::getId, relationId));
|
|
|
|
|
+ if (groupsRelation == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("车票不存在");
|
|
|
}
|
|
}
|
|
|
- return this.getOne(new LambdaQueryWrapper<NanoUserQuota>()
|
|
|
|
|
- .eq(NanoUserQuota::getUid, uid)
|
|
|
|
|
- .last("LIMIT 1"));
|
|
|
|
|
|
|
+ return groupsRelation;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public void updateUsage(Long id, Integer usedQuota) {
|
|
|
|
|
- if (id == null) {
|
|
|
|
|
- throw new BusinessRuntimeException("配额ID不能为空");
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取车队信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param groupsRelation
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private GroupsTrips getGroupsTrips(GroupsRelation groupsRelation) {
|
|
|
|
|
+ GroupsTrips groupsTrips = groupsMapper.selectById(groupsRelation.getGroupsId());
|
|
|
|
|
+ if (groupsTrips == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("车队异常");
|
|
|
}
|
|
}
|
|
|
- if (usedQuota == null || usedQuota < 0) {
|
|
|
|
|
- throw new BusinessRuntimeException("使用配额数量不能为空且必须大于等于0");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- NanoUserQuota quota = this.getById(id);
|
|
|
|
|
- if (quota == null) {
|
|
|
|
|
- throw new BusinessRuntimeException("配额记录不存在");
|
|
|
|
|
|
|
+ return groupsTrips;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private NanoUserQuota getNanoUser(Long userId, Long relationId, GroupsRelation groupsRelation, GoodsDonSku goodsDonSku) {
|
|
|
|
|
+ NanoUserQuota nanoUser = getOne(Wrappers.lambdaQuery(NanoUserQuota.class).eq(NanoUserQuota::getRelationId, relationId));
|
|
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
|
|
+ if (nanoUser == null) {
|
|
|
|
|
+ nanoUser = createNanoUser(groupsRelation, user, goodsDonSku);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ updateLumaUser(user, nanoUser);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- if (usedQuota > quota.getTotalQuota()) {
|
|
|
|
|
- throw new BusinessRuntimeException("使用配额不能超过总配额");
|
|
|
|
|
|
|
+ return nanoUser;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private NanoUserQuota createNanoUser(GroupsRelation groupsRelation, User user, GoodsDonSku sku) {
|
|
|
|
|
+ NanoUserQuota nanoUserQuota = new NanoUserQuota();
|
|
|
|
|
+ nanoUserQuota.setUid(user.getId());
|
|
|
|
|
+ nanoUserQuota.setRelationId(groupsRelation.getId());
|
|
|
|
|
+ nanoUserQuota.setUserToken(UUID.randomUUID().toString());
|
|
|
|
|
+ nanoUserQuota.setNickname(user.getNickname());
|
|
|
|
|
+ nanoUserQuota.setAvatar(user.getHeadimgurl());
|
|
|
|
|
+ nanoUserQuota.setPurchaseDate(DateTime.now());
|
|
|
|
|
+ nanoUserQuota.setTotalQuota(sku.getNanoQuota());
|
|
|
|
|
+ nanoUserQuota.setRemainingQuota(nanoUserQuota.getRemainingQuota());
|
|
|
|
|
+ try {
|
|
|
|
|
+ save(nanoUserQuota);
|
|
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- quota.setUsedQuota(usedQuota);
|
|
|
|
|
- quota.setRemainingQuota(quota.getTotalQuota() - usedQuota);
|
|
|
|
|
- quota.setLastUsedDate(new Date());
|
|
|
|
|
-
|
|
|
|
|
- this.updateById(quota);
|
|
|
|
|
|
|
+ return nanoUserQuota;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void updateLumaUser(User user, NanoUserQuota nanoUserQuota) {
|
|
|
|
|
+ nanoUserQuota.setNickname(user.getNickname());
|
|
|
|
|
+ nanoUserQuota.setAvatar(user.getHeadimgurl());
|
|
|
|
|
+ updateById(nanoUserQuota);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|