瀏覽代碼

添加操作时间计时功能及逻辑优化

在代码中引入 `Stopwatch` 类以记录方法执行时间。初始化 `ffrPrice` 和 `visitDt`,简化 `groupInfo` 的获取逻辑,移除重复代码。成功操作的返回信息中包含执行时间,并在异常处理时记录执行时间。
LEIYI 4 月之前
父節點
當前提交
6bdbdde5ea
共有 1 個文件被更改,包括 42 次插入23 次删除
  1. 42 23
      OASystem/OASystem.Api/Controllers/GroupsController.cs

+ 42 - 23
OASystem/OASystem.Api/Controllers/GroupsController.cs

@@ -692,27 +692,31 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public async Task<IActionResult> GroupOperation(GroupOperationDto dto)
         {
-            try
+            // 创建 Stopwatch 实例
+            Stopwatch stopwatch = Stopwatch.StartNew();
+
+            decimal ffrPrice = 0.00M;
+            DateTime? visitDt = null;
+            if (dto.Status == 2)
             {
-                decimal ffrPrice = 0.00M;
-                DateTime? visitDt = null;
-                if (dto.Status == 2)
+                var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(x => x.Id == dto.Id && x.IsDel == 0);
+                if (groupInfo != null)
                 {
-                    var groupInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(x => x.Id == dto.Id && x.IsDel == 0);
-                    if (groupInfo != null)
-                    {
-                        ffrPrice = groupInfo.PaymentMoney;
-                        visitDt = groupInfo.VisitDate.AddDays(-groupInfo.PayDay);
-                    }
+                    ffrPrice = groupInfo.PaymentMoney;
+                    visitDt = groupInfo.VisitDate.AddDays(-groupInfo.PayDay);
                 }
+            }
 
-                var groupData = await _groupRepository.GroupOperation(dto);
-                if (groupData.Code != 0)
-                {
-                    return Ok(JsonView(false, groupData.Msg));
-                }
+            var groupData = await _groupRepository.GroupOperation(dto);
+            if (groupData.Code != 0)
+            {
+                return Ok(JsonView(false, groupData.Msg));
+            }
+            int diId = 0;
+            try
+            {
 
-                int diId = 0;
+                
                 //添加时 默认加入团组汇率
                 if (dto.Status == 1) //添加
                 {
@@ -763,10 +767,7 @@ namespace OASystem.API.Controllers
                 }
                 #endregion
 
-                if (diId > 0)
-                {
-                    await AppNoticeLibrary.SendUserMsg_GroupTimeInfo_ToVisaUser(diId, startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd"));
-                }
+
 
                 #endregion
 
@@ -820,15 +821,33 @@ namespace OASystem.API.Controllers
 
                 #endregion
 
-                return Ok(JsonView(true, "操作成功!", diId));
+
+
+
+                if (diId > 0)
+                {
+                    await AppNoticeLibrary.SendUserMsg_GroupTimeInfo_ToVisaUser(diId, startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd"));
+                }
+
+                // 停止计时
+                stopwatch.Stop();
+
+                return Ok(JsonView(true, $"操作成功!{stopwatch.ElapsedMilliseconds}ms", diId));
             }
             catch (Exception ex)
             {
-
+                // 停止计时
+                stopwatch.Stop();
                 Logs("[response]" + JsonConvert.SerializeObject(dto));
                 Logs(ex.Message);
-                return Ok(JsonView(false, ex.Message));
+                //return Ok(JsonView(false, $"{ex.Message}[{stopwatch.ElapsedMilliseconds}ms]"));
             }
+
+
+            // 停止计时
+            stopwatch.Stop();
+
+            return Ok(JsonView(true, $"操作成功!{stopwatch.ElapsedMilliseconds}ms", diId));
         }
 
         /// <summary>