From df19a6d58ef1d94e11c4dbfd76074e0e54d3d32c Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期五, 05 九月 2025 10:59:32 +0800
Subject: [PATCH] feat:开发

---
 FlexJobApi.CommonServer.Application/CodeUrls/Commands/CodeUrlCommandHandler.cs |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/FlexJobApi.CommonServer.Application/CodeUrls/Commands/CodeUrlCommandHandler.cs b/FlexJobApi.CommonServer.Application/CodeUrls/Commands/CodeUrlCommandHandler.cs
new file mode 100644
index 0000000..a3c2a4a
--- /dev/null
+++ b/FlexJobApi.CommonServer.Application/CodeUrls/Commands/CodeUrlCommandHandler.cs
@@ -0,0 +1,58 @@
+锘縰sing FlexJobApi.Core;
+using Furion.DatabaseAccessor;
+using MediatR;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlexJobApi.CommonServer.Application
+{
+    /// <summary>
+    /// 淇濆瓨缂栧彿鍦板潃
+    /// </summary>
+    /// <param name="rep"></param>
+    public class CodeUrlCommandHandler(
+            IRepository<CodeUrl> rep
+        ) :
+        IRequestHandler<SaveCodeUrlCommand, string>
+    {
+        private readonly IRepository<CodeUrl> rep = rep;
+
+        /// <summary>
+        /// 淇濆瓨缂栧彿鍦板潃
+        /// </summary>
+        /// <param name="request"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        public async Task<string> Handle(SaveCodeUrlCommand request, CancellationToken cancellationToken)
+        {
+            var entity = new CodeUrl
+            {
+                Scene = request.Scene,
+                Url = request.Url,
+                ExpiredTime = request.ExpiredTime
+            };
+            await SetCode(entity);
+            await rep.InsertAsync(entity);
+            return entity.Code;
+        }
+
+        private async Task<string> SetCode(CodeUrl entity)
+        {
+            var now = DateTime.Now;
+            entity.Code = $"{StringUtils.GenerateRandomString(5)}";
+            var exist = await rep.AnyAsync(it =>
+                it.Scene == entity.Scene
+                && it.Code == entity.Code
+                && (it.ExpiredTime == null
+                || it.ExpiredTime > now));
+            if (exist)
+            {
+                await SetCode(entity);
+            }
+            return entity.Code;
+        }
+    }
+}

--
Gitblit v1.9.1