Pārlūkot izejas kodu

GPT镜像账号展示;处理GPT镜像车队账号

zoujiajian 2 gadi atpakaļ
vecāks
revīzija
f0457c0df3

+ 5 - 0
netflix-common/src/main/java/com/cyksj/common/constant/Constant.java

@@ -270,4 +270,9 @@ public interface Constant {
 	BigDecimal TWO_THOUSAND = BigDecimal.valueOf(200000);
 
 	Long CALUDE_GOODS_ID = 73l;
+
+	/**
+	 * GPT 镜像车队唯一账号
+	 */
+	String GPT_CAR_ONLY_ACCOUNT = "gptcaroo21213";
 }

+ 5 - 0
netflix-dao/src/main/java/com/cyksj/mapper/ChatgptUserMapper.java

@@ -2,10 +2,15 @@ package com.cyksj.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.cyksj.model.entity.ChatgptUser;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Update;
 
 /**
  * @author chan
  * @date 2024/3/19 10:44
  */
 public interface ChatgptUserMapper extends BaseMapper<ChatgptUser> {
+
+	@Update("update chatgpt_user cu set limit_num = #{gptLimitNum}, limit_time = #{gptLimitTime} where exists(select 1 from (select id from groups_trips where sku_id = #{skuId}) gt inner join groups_relation gr on gr.groups_id = gt.id where gr.id = cu.relationId)")
+	void updateChatGptCarUserNumAndLimitTime(@Param("skuId") Long skuId, @Param("gptLimitNum") Integer gptLimitNum, @Param("gptLimitTime") Long gptLimitTime);
 }

+ 38 - 0
netflix-dao/src/main/java/com/cyksj/model/views/ChatgptCarSessionView.java

@@ -0,0 +1,38 @@
+package com.cyksj.model.views;
+
+import com.ejlchina.searcher.bean.DbField;
+import com.ejlchina.searcher.bean.SearchBean;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Date;
+
+/**
+ * 项目名: yhlxj
+ * 文件名: ChatgptCarSessionView
+ * 创建者: JavaZou
+ * 创建时间:2024/4/12 10:28
+ */
+@Getter
+@Setter
+@SearchBean(tables = "chatgpt_session cs",
+		where = "cs.isCar = 1")
+public class ChatgptCarSessionView {
+	@DbField("cs.id")
+	private Long id;
+
+	/**
+	 * 账号
+	 */
+	@DbField("cs.email")
+	private String email;
+
+	@DbField("cs.password")
+	private String password;
+
+	@DbField("cs.carID")
+	private String carID;
+
+	@DbField("cs.created_time")
+	private Date createdTime;
+}

+ 5 - 0
netflix-service/src/main/java/com/cyksj/service/chatgpt/ChatGptAccountService.java

@@ -60,4 +60,9 @@ public interface ChatGptAccountService {
      * oai 触发车队限制
      */
     void carLimited(String carId, Integer expTime);
+
+    /**
+     * 获取GPT session
+     */
+   String getGptSession(String account, String password);
 }

+ 18 - 1
netflix-service/src/main/java/com/cyksj/service/chatgpt/impl/ChatGptAccountServiceImpl.java

@@ -6,7 +6,6 @@ import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.http.Method;
-import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -113,6 +112,24 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
         return refreshToken;
     }
 
+    @Override
+    public String getGptSession(String account, String password) {
+        String officialSession = "";
+        try {
+            Account at = new Account();
+            at.setAccount(account);
+            at.setPassword(password);
+            JSONObject loginResult = getLoginResult(at);
+            if (StringUtils.isBlank(loginResult.getStr("accessToken"))) {
+                throw BusinessRuntimeException.getInstance(loginResult.getStr("detail"));
+            }
+            officialSession = loginResult.toString();
+        } catch (Exception e) {
+            throw BusinessRuntimeException.getInstance("登录获取token错误 error:" + e.getMessage());
+        }
+        return officialSession;
+    }
+
     /**
      * 判断账号是否存在
      *

+ 14 - 0
netflix-service/src/main/java/com/cyksj/service/groups/impl/GroupFunServiceImpl.java

@@ -1,5 +1,6 @@
 package com.cyksj.service.groups.impl;
 
+import com.cyksj.common.constant.Constant;
 import com.cyksj.mapper.GroupsMapper;
 import com.cyksj.mapper.GroupsRelationMapper;
 import com.cyksj.model.entity.GoodsDonSku;
@@ -61,6 +62,19 @@ public class GroupFunServiceImpl implements GroupsFuncService {
 		if (sku.getId() == 178l || sku.getId() == 423l) {
 			availableParkingNum = 4;
 		}
+		//如果是GPT镜像车队
+		//设置唯一账号
+		if (sku.getIsCar()) {
+			AccountView accountView = beanSearcher.searchFirst(AccountView.class, MapUtils.builder()
+					.field(AccountView::getGoodsId, sku.getGoodsId())
+					.field(AccountView::getAccount, Constant.GPT_CAR_ONLY_ACCOUNT).
+					onlySelect(AccountView::getId).build());
+			if (accountView != null) {
+				groups.setAccountId(accountView.getId());
+				groups.setStatus(GroupsTrips.Status.validity);
+				groups.setRemark("已设置镜像车队唯一账号");
+			}
+		}
 		groups.setAvailableParkingNum(availableParkingNum);
 
 		groupsMapper.insert(groups);

+ 2 - 0
netflix-service/src/main/java/com/cyksj/service/mange/CmsGoodsDonSkuService.java

@@ -12,4 +12,6 @@ import java.util.List;
  */
 public interface CmsGoodsDonSkuService extends IService<GoodsDonSku> {
 	void handleDiscountPurchaseGoods(GoodsDonSku sku, List<GoodsDiscountPlusPurchaseRelation> discountPlusPurchaseGoods);
+
+	void updateMirrorCarUserNum(GoodsDonSku sku);
 }

+ 30 - 0
netflix-service/src/main/java/com/cyksj/service/mange/goods/CmsGoodsDonSkuServiceImpl.java

@@ -1,9 +1,11 @@
 package com.cyksj.service.mange.goods;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.cyksj.common.exception.BusinessRuntimeException;
+import com.cyksj.mapper.ChatgptUserMapper;
 import com.cyksj.mapper.GoodsDiscountPlusPurchaseRelationMapper;
 import com.cyksj.mapper.GoodsDonSkuMapper;
 import com.cyksj.model.entity.GoodsDiscountPlusPurchaseRelation;
@@ -32,6 +34,8 @@ public class CmsGoodsDonSkuServiceImpl extends ServiceImpl<GoodsDonSkuMapper,Goo
 
 	private final GoodsDonSkuMapper skuMapper;
 
+	private final ChatgptUserMapper chatgptUserMapper;
+
 	@Override
 	public void handleDiscountPurchaseGoods(GoodsDonSku sku, List<GoodsDiscountPlusPurchaseRelation> discountPlusPurchaseGoods) {
 		Long skuId = sku.getId();
@@ -85,6 +89,32 @@ public class CmsGoodsDonSkuServiceImpl extends ServiceImpl<GoodsDonSkuMapper,Goo
 		});
 	}
 
+	@Override
+	public void updateMirrorCarUserNum(GoodsDonSku sku) {
+		if (sku.getIsCar()) {
+			Long id = sku.getId();
+			GoodsDonSku dbSku = skuMapper.selectById(id);
+			Integer gptLimitNum = sku.getGptLimitNum();
+			Long gptLimitTime = sku.getGptLimitTime();
+
+			Integer dbGptLimitNum = dbSku.getGptLimitNum();
+			Long dbGptLimitTime = dbSku.getGptLimitTime();
+
+			if (gptLimitNum == null || gptLimitNum < 0) {
+				throw BusinessRuntimeException.getInstance("请输入正确的限制次数");
+			}
+
+			if (gptLimitTime == null || gptLimitTime < 0) {
+				throw BusinessRuntimeException.getInstance("请输入正确的限制时间");
+			}
+
+			if (!ObjectUtil.equal(gptLimitNum, dbGptLimitNum) || !ObjectUtil.equal(gptLimitTime, dbGptLimitTime)) {
+				//更新该规格镜像车队的用户次数 时间
+				chatgptUserMapper.updateChatGptCarUserNumAndLimitTime(sku.getId(), gptLimitNum, gptLimitTime);
+			}
+		}
+	}
+
 	public void checkPurchaseParamsAndSaveOrUpdate(Long goodsId, Long skuId, GoodsDiscountPlusPurchaseRelation discountGoods) {
 		Long disSid = discountGoods.getDisSid();
 		if (disSid == null) {

+ 73 - 0
netflix-web/src/main/java/com/cyksj/web/controller/manage/account/CmsAccountController.java

@@ -5,6 +5,7 @@ import cn.dev33.satoken.stp.StpUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.servlet.ServletUtil;
 import com.alibaba.excel.EasyExcel;
@@ -115,6 +116,8 @@ public class CmsAccountController {
 
 	private final ChatGptAccountService chatGptAccountService;
 
+	private final ChatgptSessionMapper chatgptSessionMapper;
+
 	private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
 
 	@GetMapping("/get")
@@ -1256,4 +1259,74 @@ public class CmsAccountController {
 				.build());
 		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
 	}
+
+	/**
+	 * 获取镜像车队账号列表
+	 */
+	@GetMapping("/get/mirror/car/session")
+	public Result<SearchResult<ChatgptCarSessionView>> getMirrorCarSession() {
+		SearchResult<ChatgptCarSessionView> search = beanSearcher.search(ChatgptCarSessionView.class, MapUtils.flatBuilder(request.getParameterMap())
+				.orderBy(ChatgptCarSessionView::getId).desc()
+				.build());
+		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
+	}
+
+	/**
+	 * 编辑镜像车队账号列表
+	 */
+	@PutMapping("/update/mirror/car/session")
+	public Result<String> updateMirrorCarSession(@RequestBody ChatgptSession chatgptSession) {
+		Long id = chatgptSession.getId();
+		ChatgptSession dbChatSession = chatgptSessionMapper.selectById(id);
+		if (!ObjectUtil.equal(dbChatSession.getEmail(), chatgptSession.getEmail()) || !ObjectUtil.equal(dbChatSession.getPassword(), chatgptSession.getPassword())) {
+			String gptSession = chatGptAccountService.getGptSession(dbChatSession.getEmail(), dbChatSession.getPassword());
+			dbChatSession.setOfficialSession(gptSession);
+			dbChatSession.setEmail(chatgptSession.getEmail());
+			dbChatSession.setPassword(chatgptSession.getPassword());
+		}
+		chatgptSessionMapper.updateById(dbChatSession);
+		return GatewayResponse.SUCCESS.newBuilder().toResult();
+	}
+
+	/**
+	 * 删除镜像车队账号
+	 */
+	@DeleteMapping("/delete/mirror/car/session/{id}")
+	public Result<String> deleteMirrorCarSession(@PathVariable Long id) {
+		Optional.ofNullable(chatgptSessionMapper.selectById(id))
+				.ifPresent(e -> {
+					String carId = e.getCarId();
+					if (StrUtil.isNotBlank(carId)) {
+						chatgptSessionMapper.deleteById(e.getId());
+
+						String carConversationKey = RedisService.key.CHATGPT_CAR_CONVERSATION_LIMIT.getName() + ":" + carId;
+						String highCarChatKey = RedisService.key.CHATGPT_CAR_HIGH_CHAT.getName() + carId;
+						String lowCarChatKey = RedisService.key.CHATGPT_CAR_LOW_CHAT.getName() + carId;
+						//从zSet中删除车队
+						String carScoresKey = RedisService.key.CHATGPT_CAR_SCORES.getName();
+						redisService.zRemove(carScoresKey, carId);
+						redisService.del(highCarChatKey);
+						redisService.del(lowCarChatKey);
+						redisService.del(carConversationKey);
+					}
+				});
+		return GatewayResponse.SUCCESS.newBuilder().toResult();
+	}
+
+	/**
+	 * 新增镜像车队账号
+	 */
+	@PostMapping("/post/mirror/car/session")
+	public Result<String> postMirrorCarSession(@RequestBody ChatgptSession chatgptSession) {
+		Integer selectCount = chatgptSessionMapper.selectCount(Wrappers.lambdaQuery(ChatgptSession.class)
+				.eq(ChatgptSession::getCarId, chatgptSession.getCarId()));
+		if (selectCount > 0) {
+			throw BusinessRuntimeException.getInstance("输入的车队id已存在");
+		}
+		String gptSession = chatGptAccountService.getGptSession(chatgptSession.getEmail(), chatgptSession.getPassword());
+		chatgptSession.setIsCar(true);
+		chatgptSession.setOfficialSession(gptSession);
+		chatgptSessionMapper.insert(chatgptSession);
+		return GatewayResponse.SUCCESS.newBuilder().toResult();
+	}
 }

+ 1 - 0
netflix-web/src/main/java/com/cyksj/web/controller/manage/goods/CmsGoodsDonSkuController.java

@@ -161,6 +161,7 @@ public class CmsGoodsDonSkuController {
             Set<GoodsDonSku> benefitsSkuIds = Jsons.parseSet(benefitsList, GoodsDonSku.class);
             sku.setBenefitsList(Jsons.toJson(benefitsSkuIds));
         }
+        goodsDonSkuService.updateMirrorCarUserNum(sku);
         goodsDonSkuService.saveOrUpdate(sku);
         //给平台更新额外属性
         goodsDonService.updateGoodsSkuPrice(sku.getGoodsId(), null);