username@email.com
2024-10-29 3f91a6737fc06b45461ce11eae5660cbbf766f7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Caching.Manual;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.Model.ViewModels.UI;
using Microsoft.AspNetCore.Mvc;
 
namespace CoreCms.Net.Repository
{
    /// <summary>
    /// 菜单表 接口实现
    /// </summary>
    public class SysMenuRepository : BaseRepository<SysMenu>, ISysMenuRepository
    {
        public SysMenuRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
 
        #region 实现重写增删改查操作==========================================================
 
        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> InsertAsync(SysMenu entity)
        {
            var jm = new AdminUiCallBack();
 
            var bl = await DbClient.Insertable(entity).ExecuteReturnIdentityAsync() > 0;
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.CreateSuccess : GlobalConstVars.CreateFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(SysMenu entity)
        {
            var jm = new AdminUiCallBack();
 
            var oldModel = await DbClient.Queryable<SysMenu>().In(entity.id).SingleAsync();
            if (oldModel == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            //事物处理过程开始
            //oldModel.id = entity.id;
            oldModel.parentId = entity.parentId;
            oldModel.menuName = entity.menuName;
            oldModel.menuIcon = entity.menuIcon;
            oldModel.path = entity.path;
            oldModel.component = entity.component;
            oldModel.menuType = entity.menuType;
            oldModel.sortNumber = entity.sortNumber;
            oldModel.authority = entity.authority;
            oldModel.target = entity.target;
            oldModel.iconColor = entity.iconColor;
            oldModel.hide = entity.hide;
            //oldModel.deleted = entity.deleted;
            oldModel.createTime = entity.createTime;
            oldModel.updateTime = entity.updateTime;
            oldModel.identificationCode = entity.identificationCode;
 
            //事物处理过程结束
            var bl = await DbClient.Updateable(oldModel).ExecuteCommandHasChangeAsync();
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(List<SysMenu> entity)
        {
            var jm = new AdminUiCallBack();
 
            var bl = await DbClient.Updateable(entity).ExecuteCommandHasChangeAsync();
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdAsync(int id)
        {
            var jm = new AdminUiCallBack();
 
            var all = await DbClient.Queryable<SysMenu>().ToListAsync();
            var model = all.Find(p => p.id == id);
            if (model == null)
            {
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
 
            var ids = new List<int>() { id };
            GetIds(all, id, ids);
 
            var bl = await DbClient.Deleteable<SysMenu>().In(ids).ExecuteCommandHasChangeAsync();
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
 
            return jm;
        }
 
        #endregion
 
 
        /// <summary>
        ///获取下级所有数据序列
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parentId"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        private List<int> GetIds(List<SysMenu> data, int parentId, List<int> ids)
        {
            var childs = data.Where(p => p.parentId == parentId).ToList();
            foreach (var item in childs)
            {
                ids.Add(item.id);
                if (data.Exists(p => p.parentId == item.id))
                {
                    ids = GetIds(data, item.id, ids);
                }
            }
            return ids;
        }
 
    }
}