hutongqing
2024-12-10 8d341db9d2d5699d527c88c935f0c4ce255a57a4
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_SystemService/Sys_RoleService.cs
@@ -15,6 +15,7 @@
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.System;
using WIDESEA_SystemRepository;
using WIDESEA_Core.Helper;
namespace WIDESEA_SystemService
{
@@ -35,6 +36,89 @@
            _RoleAuthRepository = roleAuthRepository;
        }
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            string authorityScope = saveModel.MainData["authorityScope"].ToString();
            Sys_Role role = saveModel.MainData.DicToModel<Sys_Role>();
            if (BaseDal.QueryFirst(x => x.RoleName == role.RoleName) != null)
            {
                return WebResponseContent.Instance.Error($"角色名重复");
            }
            List<Dt_Warehouse> warehouses = BaseDal.Db.Queryable<Dt_Warehouse>().Where(x => true).ToList();
            List<Sys_RoleDataPermission> roleDataPermissions = new List<Sys_RoleDataPermission>();
            if (!string.IsNullOrEmpty(authorityScope))
            {
                string[] scopes = authorityScope.Split(',');
                foreach (string scope in scopes)
                {
                    Dt_Warehouse? warehouse = warehouses.FirstOrDefault(x => x.WarehouseId == Convert.ToInt32(scope));
                    if (warehouse != null)
                    {
                        roleDataPermissions.Add(new Sys_RoleDataPermission
                        {
                            WarehouseId = warehouse.WarehouseId,
                            WarehouseName = warehouse.WarehouseName,
                            RoleName = role.RoleName
                        });
                    }
                }
            }
            int roleId = BaseDal.AddData(role);
            if (roleDataPermissions.Count > 0)
            {
                roleDataPermissions.ForEach(x => { x.RoleId = roleId; });
                BaseDal.Db.Insertable(roleDataPermissions).ExecuteCommand();
            }
            return WebResponseContent.Instance.OK();
        }
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            string authorityScope = saveModel.MainData["authorityScope"].ToString();
            Sys_Role role = saveModel.MainData.DicToModel<Sys_Role>();
            if (BaseDal.QueryFirst(x => x.RoleId == role.RoleId) == null)
            {
                return WebResponseContent.Instance.Error($"未找到该数据");
            }
            List<Dt_Warehouse> warehouses = BaseDal.Db.Queryable<Dt_Warehouse>().Where(x => true).ToList();
            List<Sys_RoleDataPermission> oldDatas = BaseDal.Db.Queryable<Sys_RoleDataPermission>().Where(x => x.RoleId == role.RoleId).ToList();
            List<Sys_RoleDataPermission> roleDataPermissions = new List<Sys_RoleDataPermission>();
            if (!string.IsNullOrEmpty(authorityScope))
            {
                string[] scopes = authorityScope.Split(',');
                foreach (string scope in scopes)
                {
                    Sys_RoleDataPermission? oldData = oldDatas.FirstOrDefault(x => x.WarehouseId == Convert.ToInt32(scope));
                    if (oldData == null)
                    {
                        Dt_Warehouse? warehouse = warehouses.FirstOrDefault(x => x.WarehouseId == Convert.ToInt32(scope));
                        if (warehouse != null)
                        {
                            roleDataPermissions.Add(new Sys_RoleDataPermission
                            {
                                WarehouseId = warehouse.WarehouseId,
                                WarehouseName = warehouse.WarehouseName,
                                RoleName = role.RoleName
                            });
                        }
                    }
                    else
                    {
                        oldDatas.Remove(oldData);
                    }
                }
            }
            roleDataPermissions.ForEach(x => { x.RoleId = role.RoleId; });
            BaseDal.Db.Insertable(roleDataPermissions).ExecuteCommand();
            BaseDal.Db.Deleteable(oldDatas).ExecuteCommand();
            return WebResponseContent.Instance.OK();
        }
        public List<RoleNodes> GetAllChildren(int roleId)
        {
            if (roleId <= 0) return new List<RoleNodes>() { };