schangxiang@126.com
2025-09-17 f1859bb8b72998c4852aa23a605f6f7628599a2c
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
using iWareCommon.Common.EnumType;
using iWareCommon.Common.Service;
using iWareCommon.Utils;
using iWareDataCore.ORM;
using iWareDataCore.Properties;
using iWareDataCore.RBAC.Dao;
using iWareDataCore.RBAC.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareDataCore.RBAC.Service
{
  public  class RoleContentService : CommonService<RoleContentEntity, RBACRoleContent, DbModelCore>
    {
        private static object Lock = new object();
 
        private RoleContentService() : base(RoleContentDao.GetInstance()) { }
 
        private static RoleContentService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色-菜单服务的单例实体</returns>
        public static RoleContentService GetInstance()
        {
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new RoleContentService();
                    }
                }
            }
            return Instance;
        }
 
 
 
        /// <summary>
        /// 批量保存角色-菜单,将删除传入roleId的所有绑定后批量添加
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="roleContents">角色菜单列表</param>
        /// <param name="msg">异常错误消息</param>
        /// <returns>生成的角色菜单列表</returns>
        public List<RoleContentEntity> Save(int roleId, List<RoleContentEntity> roleContents, out string msg)
        {
            msg = "";
            using (var dbModel = new DbModelCore())
            {
                try
                {
 
                    var ones = dbModel.RBACRoleContents.Where(x => x.roleid == roleId).ToList();
 
                    foreach (var one in ones)
                    {
                        dbModel.RBACRoleContents.Remove(one);
                    }
 
                    var rbacRoleContents = new List<RBACRoleContent>();
 
                    foreach (var roleUser in roleContents)
                    {
                        rbacRoleContents.Add(roleUser.ToOrm());
                    }
 
                    foreach (var one in rbacRoleContents)
                    {
                        dbModel.RBACRoleContents.Add(one);
                    }
 
                    dbModel.SaveChanges();
 
                    var res = new List<RoleContentEntity>();
 
                    foreach (var one in rbacRoleContents)
                    {
                        res.Add(new RoleContentEntity(one));
                    }
 
                    return res;
                }
                catch (Exception ex)
                {
                    msg = ex.HResult == (int)EDbError.记录已存在 ? EDbError.记录已存在.ToString() : ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Save", msg);
                    return new List<RoleContentEntity>();
                }
            }
        }
    }
}