Browse Source

优化控制器代码,简化逻辑和参数验证

在 `MarketCustomerResourcesController.cs` 中,删除了客户资料表操作记录的相关代码,并简化了返回结果的处理逻辑。

在 `SystemController.cs` 中,移除了不必要的 `using` 语句,使用了 C# 9.0 的简化语法来初始化列表。

多个方法中简化了条件判断的返回逻辑,使用三元运算符减少代码行数。

参数验证中采用更简洁的返回方式,减少了代码的嵌套层级。

合并了消息处理相关方法中对未读消息计数的获取逻辑,减少冗余代码。

在数据类型操作相关方法中增加了对输入参数的有效性检查,确保传入参数符合预期。

移除了异常处理部分不必要的 `throw` 语句,简化了错误处理逻辑。
LEIYI 1 month ago
parent
commit
82ac39bff3

+ 0 - 9
OASystem/OASystem.Api/Controllers/MarketCustomerResourcesController.cs

@@ -667,16 +667,7 @@ namespace OASystem.API.Controllers
                 designer.Workbook.Save(AppSettingsHelper.Get("ExcelBasePath") + "NewClientDataExcelDownload/" + fileName);
                 string url = AppSettingsHelper.Get("ExcelBaseUrl") + "Office/Excel/NewClientDataExcelDownload/" + fileName;
 
-                #region 客户资料表操作记录
                 var respose = JsonView(true, "成功", url);
-                //var paramData = new
-                //{
-                //    APIName = @"MarketCustomerResources/NewClientDataExcelDownload",
-                //    RequestParam = dto,
-                //    ResposeParam = respose
-                //};
-                //await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Download, dto.OperationUserId, 0, JsonConvert.SerializeObject(paramData));
-                #endregion
 
                 return Ok(respose);
             }

+ 42 - 95
OASystem/OASystem.Api/Controllers/SystemController.cs

@@ -2,10 +2,8 @@ using OASystem.Domain.AesEncryption;
 using OASystem.Domain.Entities.Customer;
 using OASystem.Domain.Entities.Financial;
 using OASystem.Domain.Entities.Groups;
-using SqlSugar;
 using System.Collections;
 using System.Data;
-using static NPOI.HSSF.Util.HSSFColor;
 using static OASystem.API.OAMethodLib.GeneralMethod;
 using static OASystem.API.OAMethodLib.JWTHelper;
 
@@ -34,9 +32,8 @@ namespace OASystem.API.Controllers
         private readonly JobPostRepository _jobRep;
         private readonly SetDataTypeRepository _setDataTypeRep;
         private readonly UserAuthorityRepository _UserAuthorityRepository;
-
-        private readonly List<int> _operationTypeList = new List<int>() { 1, 2, 3, 4, 5 }; //操作通知所属类型
-        private readonly List<int> _taskTypeList = new List<int>() { 6 };//任务通知 TaskNotification
+        private readonly List<int> _operationTypeList = new() { 1, 2, 3, 4, 5 }; //操作通知所属类型
+        private readonly List<int> _taskTypeList = new() { 6 };//任务通知 TaskNotification
 
 
         public SystemController(
@@ -86,13 +83,7 @@ namespace OASystem.API.Controllers
         public async Task<IActionResult> PsotMsgPageList(MsgDto dto)
         {
             var msgData = await _messageRep.GetMsgList(dto);
-
-            if (msgData.Code != 0)
-            {
-                return Ok(JsonView(false, msgData.Msg));
-            }
-
-            return Ok(JsonView(msgData.Data));
+            return msgData.Code != 0 ? Ok(JsonView(false, msgData.Msg)) : Ok(JsonView(msgData.Data));
         }
 
         #region 消息列表 - 分开
@@ -107,25 +98,15 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public async Task<IActionResult> PotsMsgTypeData(MsgTypeDto dto)
         {
-            if (dto.PortType < 1 || dto.PortType > 3)
-            {
-                return Ok(JsonView(false, "请输入有效的PortType参数。1 Web 2 Android 3 IOS"));
-            }
+            if (dto.PortType < 1 || dto.PortType > 3) 
+                return Ok(JsonView(false, MsgTips.Port));
 
-            if (dto.UserId < 1)
-            {
+            if (dto.UserId < 1) 
                 return Ok(JsonView(false, "请输入有效的UserId参数。"));
-            }
 
             var msgData = await _messageRep.PotsMsgTypeData(dto);
 
-            if (msgData.Code != 0)
-            {
-
-                return Ok(JsonView(400, msgData.Msg, new string[] { }));
-            }
-
-            return Ok(JsonView(true, msgData.Msg, msgData.Data));
+            return msgData.Code != 0 ? Ok(JsonView(400, msgData.Msg, new string[] { })) : Ok(JsonView(true, msgData.Msg, msgData.Data));
         }
 
         /// <summary>
@@ -141,30 +122,19 @@ namespace OASystem.API.Controllers
             #region 参数验证
 
             if (dto.PortType < 1 || dto.PortType > 3)
-            {
-                return Ok(JsonView(false, "请输入有效的PortType参数。1 Web 2 Android 3 IOS"));
-            }
+                return Ok(JsonView(false, MsgTips.Port));
 
             var typeData = await _sqlSugar.Queryable<Sys_SetData>().Where(it => it.STid == 77).Select(it => it.Id).ToListAsync();
-            if (typeData.Count < 0)
-            {
+            if (typeData.Count < 0) 
                 return Ok(JsonView(false, "消息类型不存在"));
-            }
 
-            if (!typeData.Contains(dto.Type))
-            {
+            if (!typeData.Contains(dto.Type)) 
                 return Ok(JsonView(false, "请输入有效的Type参数。1021 团组操作通知 1020 任务操作通知 "));
-            }
-            if (dto.UserId < 1)
-            {
+            if (dto.UserId < 1) 
                 return Ok(JsonView(false, "请输入有效的UserId参数。"));
-            }
 
-            if (dto.ReadStatus < 1 || dto.ReadStatus > 3)
-            {
+            if (dto.ReadStatus < 1 || dto.ReadStatus > 3) 
                 return Ok(JsonView(false, "请输入有效的ReadStatus参数。1 全部(包含已读/未读) 2 未读 3 已读"));
-            }
-
             #endregion
 
             //userId
@@ -192,10 +162,13 @@ namespace OASystem.API.Controllers
             }
 
             //是否已读处理 1 全部(包含已读/未读) 2 未读 3 已读
-            if (dto.ReadStatus == 1) msgSqlWhere += "";
-            else if (dto.ReadStatus == 2) msgSqlWhere += $" And smra.IsRead = {0}";
-            else if (dto.ReadStatus == 3) msgSqlWhere += $" And smra.IsRead = {1}";
 
+            msgSqlWhere += dto.ReadStatus switch
+            {
+                2 => " And smra.IsRead = 0",
+                3 => " And smra.IsRead = 1",
+                _ => ""
+            };
 
             string msgSql = string.Format(@"Select * From(
                                                 Select row_number() over(order by sm.ReleaseTime Desc) as RowNumber,
@@ -216,18 +189,15 @@ namespace OASystem.API.Controllers
                 RefAsync<int> totalCount = 0;
                 var data = await _sqlSugar.SqlQueryable<MessageListView>(msgSql).ToPageListAsync(dto.PageIndex, dto.PageSize, totalCount);
 
-
                 return Ok(JsonView(true, "操作成功!", data, totalCount));
             }
             catch (Exception ex)
             {
-
                 return Ok(JsonView(false, ex.Message));
             }
 
         }
 
-
         /// <summary>
         /// 系统消息
         /// 获取消息未读条数
@@ -241,9 +211,7 @@ namespace OASystem.API.Controllers
             #region 参数验证
 
             if (dto.UserId < 1)
-            {
                 return Ok(JsonView(false, "请输入有效的UserId参数。"));
-            }
 
             #endregion
 
@@ -273,20 +241,9 @@ namespace OASystem.API.Controllers
                 //if (currentDateTime < startTime && currentDateTime > endTime) return Ok(JsonView(204, "NO ACCESS!!", ""));
                 #endregion
 
-                int messageUnReadCount = 0;
-                int announcementUnReadCount = 0;
-                var data = await _messageRep.GetUnReadCount(dto.UserId);
-                if (data > 0)
-                {
-                    messageUnReadCount = data;
-
-                }
-                var data1 = await _messageRep.GetAnnouncementUnReadCount(dto.UserId);
-                if (data1 > 0)
-                {
-                    announcementUnReadCount = data1;
-                }
-
+                int messageUnReadCount = await _messageRep.GetUnReadCount(dto.UserId);
+                int announcementUnReadCount = await _messageRep.GetAnnouncementUnReadCount(dto.UserId);
+                
                 return Ok(JsonView(true, "操作成功!", new { messageUnReadCount = messageUnReadCount, announcementUnReadCount = announcementUnReadCount }));
             }
             catch (Exception ex)
@@ -308,18 +265,11 @@ namespace OASystem.API.Controllers
         public async Task<IActionResult> PostMsgInfo(MsgInfoDto dto)
         {
             if (dto.PortType < 1 || dto.PortType > 3)
-            {
-                return Ok(JsonView(false, "请输入有效的PortType参数。1 Web 2 Android 3 IOS"));
-            }
+                return Ok(JsonView(false, MsgTips.Port));
 
             var msgData = await _messageRep.GetMsgInfo(dto);
 
-            if (msgData.Code != 0)
-            {
-                return Ok(JsonView(false, msgData.Msg));
-            }
-
-            return Ok(JsonView(true, "操作成功!", msgData.Data));
+            return msgData.Code != 0 ? Ok(JsonView(false, msgData.Msg)) : Ok(JsonView(true, "操作成功!", msgData.Data));
         }
 
         /// <summary>
@@ -332,13 +282,7 @@ namespace OASystem.API.Controllers
         public async Task<IActionResult> SetMessageRead(MsgSetReadDto dto)
         {
             var msgData = await _messageRep.SetMsgRead(dto);
-
-            if (msgData.Code != 0)
-            {
-                return Ok(JsonView(false, msgData.Msg));
-            }
-
-            return Ok(JsonView(true));
+            return msgData.Code != 0 ? Ok(JsonView(false, msgData.Msg)) : Ok(JsonView(true));
         }
 
         /// <summary>
@@ -351,13 +295,7 @@ namespace OASystem.API.Controllers
         public async Task<IActionResult> DeleMsg(MsgDeleteDto dto)
         {
             var msgData = await _messageRep.DelMsg(dto);
-
-            if (msgData.Code != 0)
-            {
-                return Ok(JsonView(false, msgData.Msg));
-            }
-
-            return Ok(JsonView(true));
+            return msgData.Code != 0 ? Ok(JsonView(false, msgData.Msg)) : Ok(JsonView(true));
         }
         #endregion
 
@@ -387,7 +325,6 @@ namespace OASystem.API.Controllers
             catch (Exception ex)
             {
                 return Ok(JsonView(false, "程序错误!"));
-                throw;
             }
         }
 
@@ -400,6 +337,11 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public IActionResult QuerySetDataInitByArr(QuerySetDataInitByArr Dto)
         {
+            if (Dto.DataTypeArr == null || !Dto.DataTypeArr.Any())
+            {
+                return Ok(JsonView(false, "请传入有效的类型数组!"));
+            }
+
             var DbQuery = _setDataRepository.QueryDto<Sys_SetData, SetDataView>(s => Dto.DataTypeArr.Contains(s.STid)).ToList();
             var GroupResult = DbQuery.GroupBy(x => x.STid).Select(x => new
             {
@@ -433,7 +375,6 @@ namespace OASystem.API.Controllers
             catch (Exception)
             {
                 return Ok(JsonView(false, "程序错误!"));
-                throw;
             }
         }
 
@@ -446,12 +387,13 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public async Task<IActionResult> OperationSetDataType(OperationSetDataTypeDto dto)
         {
+            if (dto == null || string.IsNullOrEmpty(dto.Name))
+            {
+                return Ok(JsonView(false, "请检查类型名称是否填写!"));
+            }
+
             try
             {
-                if (dto.Name == "")
-                {
-                    return Ok(JsonView(false, "请检查类型名称是否填写!"));
-                }
                 Result result = await _setDataTypeRep.OperationSetDataType(dto);
                 if (result.Code != 0)
                 {
@@ -462,7 +404,6 @@ namespace OASystem.API.Controllers
             catch (Exception ex)
             {
                 return Ok(JsonView(false, "程序错误!"));
-                throw;
             }
         }
 
@@ -475,6 +416,12 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public async Task<IActionResult> DelSetDataType(DelSetDataTypeDto dto)
         {
+            if (dto == null || dto.Id <= 0)
+                return Ok(JsonView(false, "请传入有效的Id参数!"));
+
+            if (dto == null || dto.DeleteUserId <= 0)
+                return Ok(JsonView(false, "请传入有效的DeleteUserId参数!"));
+
             try
             {
                 var res = await _setDataTypeRep.SoftDeleteByIdAsync<Sys_SetDataType>(dto.Id.ToString(), dto.DeleteUserId);
@@ -487,7 +434,6 @@ namespace OASystem.API.Controllers
             catch (Exception ex)
             {
                 return Ok(JsonView(false, "程序错误!"));
-                throw;
             }
         }
         #endregion
@@ -3106,5 +3052,6 @@ And u.UId = {0} And u.FId = 1 ", dto.UserId);
         }
 
         #endregion
+
     }
 }