Browse Source

新增餐厅项视图类并优化查询逻辑

在 `RestaurantView.cs` 中新增 `RestaurantItemView` 类,包含餐厅项的详细信息属性。
在 `RestaurantRepository.cs` 中修改 `ItemAsync` 方法,使用原始 SQL 查询替代原有逻辑,以映射到新的视图类,提升查询灵活性并获取更多信息。
LEIYI 4 days ago
parent
commit
6ae3b99b77

+ 54 - 0
OASystem/OASystem.Domain/ViewModels/Groups/RestaurantView.cs

@@ -59,4 +59,58 @@ namespace OASystem.Domain.ViewModels.Groups
         /// </summary>
         public string Remark { get; set; }
     }
+
+    public class RestaurantItemView
+    {
+        public int Id { get; set; }
+
+        /// <summary>
+        /// 团组Id
+        /// </summary>
+        public int GroupId { get; set; }
+
+        /// <summary>
+        /// 用餐日期(2024-12-31)
+        /// </summary>
+        public string Date { get; set; }
+
+        /// <summary>
+        ///  用餐开始时间(08:00)
+        /// </summary>
+        public string StartTime { get; set; }
+
+        /// <summary>
+        ///  用餐结束时间(10:00)
+        /// </summary>
+        public string EndTime { get; set; }
+
+        /// <summary>
+        /// 用餐类型(1:早餐; 2:午餐; 3:晚餐;)
+        /// </summary>
+        public string Type { get; set; }
+
+        /// <summary>
+        /// 餐厅名称
+        /// </summary>
+        public string Name { get; set; }
+
+        /// <summary>
+        /// 餐厅电话
+        /// </summary>
+        public string Tel { get; set; }
+
+        /// <summary>
+        /// 餐厅地址
+        /// </summary>
+        public string Address { get; set; }
+
+        /// <summary>
+        /// 其他信息
+        /// </summary>
+        public string Remark { get; set; }
+
+        public DateTime CreateTime { get; set; }
+
+        public string CreateUserName { get; set; }
+    }
 }

+ 28 - 19
OASystem/OASystem.Infrastructure/Repositories/Groups/RestaurantRepository.cs

@@ -78,25 +78,34 @@ namespace OASystem.Infrastructure.Repositories.Groups
             if (!SharingStaticData.PortTypes.Contains(portType)) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "端口类型错误!" };
             if (groupId < 1) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = MsgTips.DiId };
 
-            var infos = await _sqlSugar
-                .Queryable<Grp_RestaurantInfo, Sys_Users>((pi, u) => new JoinQueryInfos(JoinType.Left, pi.CreateUserId == u.Id))
-                .Where((pi, u) => pi.IsDel == 0 && pi.GroupId == groupId)
-                
-                .Select((pi, u) => new
-                {
-                    pi.Id,
-                    //pi.GroupId,
-                    pi.Date,
-                    pi.StartTime,
-                    pi.EndTime,
-                    Type = pi.Type == 1 ? "早餐" : pi.Type == 2 ? "午餐" : pi.Type == 3 ? "晚餐" : "其他",
-                    pi.Name,
-                    pi.Address,
-                    pi.Tel,
-                    pi.Remark
-                })
-                .OrderBy(pi => pi.Date)
-                .ToListAsync();
+
+            var sql = string.Format(@" SELECT
+  ri.Id,
+  ri.GroupId,
+  ri.Date,
+  ri.StartTime,
+  ri.EndTime,
+  CASE WHEN ri.Type = 1 THEN '早餐'
+  WHEN ri.Type = 2 THEN '午餐'
+  WHEN ri.Type = 3 THEN '晚餐'
+  ELSE '其他'
+  END AS 'Type',
+  ri.Name,
+  ri.Address,
+  ri.Tel,
+  ri.Remark,
+  ri.CreateTime,
+  u.CnName AS CreateUserName
+FROM
+  Grp_RestaurantInfo ri
+  LEFT JOIN Sys_Users u ON ri.CreateUserId = u.Id
+WHERE
+  ri.IsDel = 0
+  AND ri.GroupId = {0}
+", groupId);
+
+            var infos = await _sqlSugar.SqlQueryable<RestaurantItemView>(sql).ToListAsync();
+
             if (!infos.Any()) return new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "数据未填写!",Data = Array.Empty<dynamic>() };
 
             return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = infos };