|
@@ -1,7 +1,9 @@
|
|
|
package com.cyksj.service.mange.gpt.impl;
|
|
package com.cyksj.service.mange.gpt.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
import cn.hutool.http.HttpResponse;
|
|
@@ -16,8 +18,10 @@ import com.cyksj.mapper.*;
|
|
|
import com.cyksj.mapper.manage.cms.CmsUserMapper;
|
|
import com.cyksj.mapper.manage.cms.CmsUserMapper;
|
|
|
import com.cyksj.model.dto.GptCardTokenDto;
|
|
import com.cyksj.model.dto.GptCardTokenDto;
|
|
|
import com.cyksj.model.entity.*;
|
|
import com.cyksj.model.entity.*;
|
|
|
|
|
+import com.cyksj.model.request.BatchRechargeRemainNumReq;
|
|
|
import com.cyksj.model.request.GptIndependentExportRechargeReq;
|
|
import com.cyksj.model.request.GptIndependentExportRechargeReq;
|
|
|
import com.cyksj.model.request.GroupsRelationRechargeReq;
|
|
import com.cyksj.model.request.GroupsRelationRechargeReq;
|
|
|
|
|
+import com.cyksj.model.response.BatchRechargeRemainNumResp;
|
|
|
import com.cyksj.redis.RedisService;
|
|
import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.server.recharge.dto.CardKeyValidationResult;
|
|
import com.cyksj.server.recharge.dto.CardKeyValidationResult;
|
|
|
import com.cyksj.server.recharge.dto.TaskResult;
|
|
import com.cyksj.server.recharge.dto.TaskResult;
|
|
@@ -31,6 +35,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import org.springframework.transaction.support.TransactionTemplate;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -61,6 +66,8 @@ public class GptRechargeServiceImpl implements GptRechargeService {
|
|
|
|
|
|
|
|
private final GptUserRechargeRecordService gptUserRechargeRecordService;
|
|
private final GptUserRechargeRecordService gptUserRechargeRecordService;
|
|
|
|
|
|
|
|
|
|
+ private final TransactionTemplate transactionTemplate;
|
|
|
|
|
+
|
|
|
private final RedisService redisService;
|
|
private final RedisService redisService;
|
|
|
|
|
|
|
|
private final UserBindRelationService userBindRelationService;
|
|
private final UserBindRelationService userBindRelationService;
|
|
@@ -465,6 +472,184 @@ public class GptRechargeServiceImpl implements GptRechargeService {
|
|
|
return relation;
|
|
return relation;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<BatchRechargeRemainNumResp> batchUpdateRechargeRemainNum(long adminId, BatchRechargeRemainNumReq req) {
|
|
|
|
|
+ if (CollUtil.isEmpty(req.getOrderNos())) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("订单号不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ CmsUser cmsUser = cmsUserMapper.selectById(adminId);
|
|
|
|
|
+ String operator = cmsUser != null ? cmsUser.getNickname() : null;
|
|
|
|
|
+ return req.getOrderNos().stream()
|
|
|
|
|
+ .filter(StrUtil::isNotBlank)
|
|
|
|
|
+ .map(String::trim)
|
|
|
|
|
+ .distinct()
|
|
|
|
|
+ .map(orderNo -> batchHandleRechargeRemainNum(orderNo, req, operator))
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private BatchRechargeRemainNumResp batchHandleRechargeRemainNum(String orderNo, BatchRechargeRemainNumReq req, String operator) {
|
|
|
|
|
+ BatchRechargeRemainNumResp resp = new BatchRechargeRemainNumResp();
|
|
|
|
|
+ resp.setOrderNo(orderNo);
|
|
|
|
|
+ try {
|
|
|
|
|
+ transactionTemplate.execute(status -> {
|
|
|
|
|
+ OrderDon orderDon = getRechargeOrderDon(orderNo);
|
|
|
|
|
+ GroupsRelation relation = relationMapper.selectById(orderDon.getRelationId());
|
|
|
|
|
+ Assert.notNull(relation, "代充车票不存在");
|
|
|
|
|
+ resp.setRelationId(relation.getId());
|
|
|
|
|
+ if (relation.getRechargeStatus() == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("非代充类型车票");
|
|
|
|
|
+ }
|
|
|
|
|
+ GoodsDonSku sku = skuMapper.selectById(orderDon.getSkuId());
|
|
|
|
|
+ Assert.notNull(sku, "车票规格不存在");
|
|
|
|
|
+ if (relation.getRechargeNum() == null || relation.getRechargeRemainNum() == null) {
|
|
|
|
|
+ batchSyncRechargeOrder(orderDon, relation, sku, req, operator, resp);
|
|
|
|
|
+ resp.setSuccess(Boolean.TRUE);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ //不处理gpt代充
|
|
|
|
|
+ if (true) {
|
|
|
|
|
+ resp.setSuccess(Boolean.TRUE);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ boolean needShip = relation.getStartTime() == null
|
|
|
|
|
+ && (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.waiting
|
|
|
|
|
+ || relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharge_error);
|
|
|
|
|
+ if (needShip) {
|
|
|
|
|
+ batchShipRechargeOrder(orderDon, relation, sku, req, operator, resp);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ batchUpdateRechargeNum(relation, req, operator, resp);
|
|
|
|
|
+ }
|
|
|
|
|
+ resp.setSuccess(Boolean.TRUE);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ resp.setSuccess(Boolean.FALSE);
|
|
|
|
|
+ resp.setMessage(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return resp;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private OrderDon getRechargeOrderDon(String orderNo) {
|
|
|
|
|
+ OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
|
|
+ .eq(OrderDon::getOrderNo, orderNo)
|
|
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
|
|
|
|
|
+ .orderByDesc(OrderDon::getId)
|
|
|
|
|
+ .last("limit 1"));
|
|
|
|
|
+ if (orderDon == null) {
|
|
|
|
|
+ orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
|
|
+ .eq(OrderDon::getOrderNo, orderNo)
|
|
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderStatus)
|
|
|
|
|
+ .orderByDesc(OrderDon::getId)
|
|
|
|
|
+ .last("limit 1"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (orderDon == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("订单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (orderDon.getStatus() == OrderDon.Status.refund) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("该代充订单已退款");
|
|
|
|
|
+ }
|
|
|
|
|
+ return orderDon;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void batchSyncRechargeOrder(OrderDon orderDon, GroupsRelation relation, GoodsDonSku sku, BatchRechargeRemainNumReq req, String operator, BatchRechargeRemainNumResp resp) {
|
|
|
|
|
+ resp.setHandleType("sync");
|
|
|
|
|
+ DateTime now = DateTime.now();
|
|
|
|
|
+ orderDon.setStatus(OrderDon.Status.complete);
|
|
|
|
|
+ orderDonMapper.updateById(orderDon);
|
|
|
|
|
+
|
|
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.complete);
|
|
|
|
|
+ relation.setOperator(operator);
|
|
|
|
|
+ relation.setSubmitTime(relation.getSubmitTime() != null ? relation.getSubmitTime() : now);
|
|
|
|
|
+ relation.setSendTime(now);
|
|
|
|
|
+ if (relation.getStartTime() == null) {
|
|
|
|
|
+ relation.setStartTime(now);
|
|
|
|
|
+ DateTime expiryTime;
|
|
|
|
|
+ if (sku.getDays() != null && sku.getDays() > 0) {
|
|
|
|
|
+ expiryTime = DateUtil.offsetDay(relation.getStartTime(), sku.getDays());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ expiryTime = DateUtil.offsetMonth(relation.getStartTime(), sku.getMonths());
|
|
|
|
|
+ }
|
|
|
|
|
+ relation.setExpiryTime(expiryTime);
|
|
|
|
|
+ }
|
|
|
|
|
+ relationMapper.updateById(relation);
|
|
|
|
|
+ resp.setMessage("同步成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void batchShipRechargeOrder(OrderDon orderDon, GroupsRelation relation, GoodsDonSku sku, BatchRechargeRemainNumReq req, String operator, BatchRechargeRemainNumResp resp) {
|
|
|
|
|
+ Integer subRechargeNum = getRechargeSubNum(sku);
|
|
|
|
|
+ Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
|
|
+ if (rechargeRemainNum < subRechargeNum) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("剩余可代充次数不足");
|
|
|
|
|
+ }
|
|
|
|
|
+ resp.setHandleType("ship");
|
|
|
|
|
+ resp.setBeforeRemainNum(rechargeRemainNum);
|
|
|
|
|
+ DateTime now = DateTime.now();
|
|
|
|
|
+ orderDon.setStatus(OrderDon.Status.complete);
|
|
|
|
|
+ orderDonMapper.updateById(orderDon);
|
|
|
|
|
+
|
|
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.complete);
|
|
|
|
|
+ relation.setOperator(operator);
|
|
|
|
|
+ relation.setSubmitTime(relation.getSubmitTime() != null ? relation.getSubmitTime() : now);
|
|
|
|
|
+ relation.setSendTime(now);
|
|
|
|
|
+ if (relation.getStartTime() == null) {
|
|
|
|
|
+ relation.setStartTime(now);
|
|
|
|
|
+ DateTime expiryTime;
|
|
|
|
|
+ if (sku.getDays() != null && sku.getDays() > 0) {
|
|
|
|
|
+ expiryTime = DateUtil.offsetDay(relation.getStartTime(), sku.getDays());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ expiryTime = DateUtil.offsetMonth(relation.getStartTime(), sku.getMonths());
|
|
|
|
|
+ }
|
|
|
|
|
+ relation.setExpiryTime(expiryTime);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StrUtil.isNotBlank(req.getCardKey())) {
|
|
|
|
|
+ relation.setCardKey(req.getCardKey());
|
|
|
|
|
+ }
|
|
|
|
|
+ relationMapper.updateById(relation);
|
|
|
|
|
+
|
|
|
|
|
+ int update = relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
|
|
+ .set(GroupsRelation::getRechargeRemainNum, rechargeRemainNum - subRechargeNum)
|
|
|
|
|
+ .eq(GroupsRelation::getId, relation.getId())
|
|
|
|
|
+ .eq(GroupsRelation::getRechargeRemainNum, rechargeRemainNum));
|
|
|
|
|
+ if (update == 0) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("更新用户代充剩余次数失败,请重试");
|
|
|
|
|
+ }
|
|
|
|
|
+ resp.setAfterRemainNum(rechargeRemainNum - subRechargeNum);
|
|
|
|
|
+ gptUserRechargeRecordService.recordGptUserRechargeNum(req.getCardKey(), relation.getUserId(), relation.getId(), relation.getAccount(), -subRechargeNum, operator, Optional.ofNullable(req.getRemark()).orElse("客服批量发货"));
|
|
|
|
|
+ resp.setMessage("发货成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void batchUpdateRechargeNum(GroupsRelation relation, BatchRechargeRemainNumReq req, String operator, BatchRechargeRemainNumResp resp) {
|
|
|
|
|
+ if (req.getNum() == null || req.getNum() == 0) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("变更次数不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
|
|
+ Integer afterRemainNum = rechargeRemainNum + req.getNum();
|
|
|
|
|
+ if (afterRemainNum < 0) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("扣除次数不能为负数");
|
|
|
|
|
+ }
|
|
|
|
|
+ resp.setHandleType("update");
|
|
|
|
|
+ resp.setBeforeRemainNum(rechargeRemainNum);
|
|
|
|
|
+ int update = relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
|
|
+ .set(GroupsRelation::getRechargeNum, relation.getRechargeNum() + req.getNum())
|
|
|
|
|
+ .set(GroupsRelation::getRechargeRemainNum, afterRemainNum)
|
|
|
|
|
+ .eq(GroupsRelation::getId, relation.getId())
|
|
|
|
|
+ .eq(GroupsRelation::getRechargeRemainNum, rechargeRemainNum));
|
|
|
|
|
+ if (update == 0) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("修改用户代充次数错误,请重试");
|
|
|
|
|
+ }
|
|
|
|
|
+ resp.setAfterRemainNum(afterRemainNum);
|
|
|
|
|
+ gptUserRechargeRecordService.recordGptUserRechargeNum(req.getCardKey(), relation.getUserId(), relation.getId(), relation.getAccount(), req.getNum(), operator, Optional.ofNullable(req.getRemark()).orElse("批量修改代充次数"));
|
|
|
|
|
+ resp.setMessage("修改成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Integer getRechargeSubNum(GoodsDonSku goodsDonSku) {
|
|
|
|
|
+ if (goodsDonSku.getRechargeType() != null && goodsDonSku.getRechargeType() == 2) {
|
|
|
|
|
+ return 12;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public CardKeyValidationResult validateCardKey(String cardKey) {
|
|
public CardKeyValidationResult validateCardKey(String cardKey) {
|
|
|
log.info("开始验证卡密: {}", cardKey);
|
|
log.info("开始验证卡密: {}", cardKey);
|
|
|
try {
|
|
try {
|