379 lines
14 KiB
C#
379 lines
14 KiB
C#
using CRM.Core.BLL.Util;
|
|
using CRM.Core.Model.Entity;
|
|
using CRM.Core.Model.Map;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using WX.CRM.Common;
|
|
|
|
namespace CRM.Core.BLL.Base
|
|
{
|
|
public class BAS_PERMISSION_BL
|
|
{
|
|
#region 新增
|
|
/// <summary>
|
|
/// 新增
|
|
/// </summary>
|
|
/// <param name="errors"></param>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public bool Create(ref ValidationErrors errors, Bas_Permisson m)
|
|
{
|
|
try
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
BAS_PERMISSION entry = db.BAS_PERMISSION.FirstOrDefault(w => w.CODE == m.CODE);
|
|
if (entry != null)
|
|
{
|
|
errors.Add("编码" + m.CODE + "已被占用");
|
|
return false;
|
|
}
|
|
int maxsort = 1;
|
|
BAS_PERMISSION sortrole = db.BAS_PERMISSION.Where(wm => wm.PRENTID == m.PRENTID).OrderByDescending(w => w.SORT).FirstOrDefault();
|
|
if (sortrole != null)
|
|
{
|
|
maxsort = sortrole.SORT + 1;
|
|
}
|
|
//Sequences_BL seqbl = new Sequences_BL();
|
|
//Decimal id = seqbl.Seq_base_get();
|
|
BAS_PERMISSION model = new BAS_PERMISSION()
|
|
{
|
|
CEID = m.CEID,
|
|
CODE = m.CODE,
|
|
SORT = maxsort,
|
|
CTIME = m.CTIME,
|
|
ICON = m.ICON,
|
|
//ID = id,
|
|
ISFORBIDDEN = m.ISFORBIDDEN,
|
|
ISHIDDEN = m.ISHIDDEN,
|
|
NAME = m.NAME,
|
|
PRENTID = m.PRENTID,
|
|
TYPE = m.TYPE,
|
|
URL = m.URL,
|
|
isblank = m.isblank
|
|
};
|
|
db.BAS_PERMISSION.Add(model);
|
|
db.SaveChanges();
|
|
if (m.buttonlist != null)
|
|
{
|
|
foreach (Bas_Permisson_Button item in m.buttonlist)
|
|
{
|
|
db.BAS_PERMISSION_BUTTON.Add(new BAS_PERMISSION_BUTTON()
|
|
{
|
|
BUTTONID = item.BUTTONID,
|
|
CODE = item.CODE,
|
|
NAME = item.NAME,
|
|
PID = model.ID
|
|
});
|
|
}
|
|
}
|
|
return db.SaveChanges().GetResult();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errors.Add(ex.Message);
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
CacheHelper.Remove(cacheKey);//清除缓存
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 修改数据
|
|
/// <summary>
|
|
/// 修改数据
|
|
/// </summary>
|
|
/// <param name="errors"></param>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public bool Update(ref ValidationErrors errors, Bas_Permisson w)
|
|
{
|
|
try
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
BAS_PERMISSION entry2 = db.BAS_PERMISSION.FirstOrDefault(m => m.CODE == w.CODE && m.ID != w.ID);
|
|
if (entry2 != null)
|
|
{
|
|
errors.Add("编码" + w.CODE + "已被占用");
|
|
return false;
|
|
}
|
|
|
|
BAS_PERMISSION entry = db.BAS_PERMISSION.FirstOrDefault(m => m.ID == w.ID);
|
|
entry.ICON = w.ICON;
|
|
if (entry.PRENTID != w.PRENTID)
|
|
{
|
|
BAS_PERMISSION sortrole = db.BAS_PERMISSION.Where(wm => wm.PRENTID == w.PRENTID).OrderByDescending(aw => aw.SORT).FirstOrDefault();
|
|
if (sortrole != null)
|
|
entry.SORT = sortrole.SORT + 1;
|
|
else
|
|
entry.SORT = 1;
|
|
}
|
|
else
|
|
{
|
|
entry.SORT = w.SORT;
|
|
}
|
|
entry.PRENTID = w.PRENTID;
|
|
entry.NAME = w.NAME;
|
|
entry.CODE = w.CODE;
|
|
entry.ISFORBIDDEN = w.ISFORBIDDEN;
|
|
entry.ISHIDDEN = w.ISHIDDEN;
|
|
entry.URL = w.URL;
|
|
entry.TYPE = w.TYPE;
|
|
entry.isblank = w.isblank;
|
|
|
|
//Sequences_BL seqbl = new Sequences_BL();
|
|
db.BAS_PERMISSION_BUTTON.RemoveRange(db.BAS_PERMISSION_BUTTON.Where(m => m.PID == w.ID));
|
|
if (w.buttonlist != null)
|
|
{
|
|
foreach (Bas_Permisson_Button item in w.buttonlist)
|
|
{
|
|
//decimal btnid = seqbl.Seq_base_get();
|
|
db.BAS_PERMISSION_BUTTON.Add(new BAS_PERMISSION_BUTTON()
|
|
{
|
|
BUTTONID = item.BUTTONID,
|
|
CODE = item.CODE,
|
|
NAME = item.NAME,
|
|
PID = w.ID,
|
|
SITE = item.SITE
|
|
});
|
|
}
|
|
}
|
|
db.SaveChanges();
|
|
//db.bas_role.Attach(model);
|
|
//db.Entry<bas_role>(model).State = EntityState.Modified;
|
|
return true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errors.Add(ex.Message);
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
CacheHelper.Remove(cacheKey);//清除缓存
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
public List<BAS_PERMISSION> GetList()
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
return db.BAS_PERMISSION.OrderBy(m => m.SORT).ToList();
|
|
}
|
|
}
|
|
private string cacheKey = "cache_Get_BasPerMissionList";
|
|
public List<BAS_PERMISSION_BUTTON> GetPermissionList()
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
return db.BAS_PERMISSION_BUTTON.ToList();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 返回树形列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<Bas_Permisson> Get_BasPerMissionList()
|
|
{
|
|
|
|
List<Bas_Permisson> list = new List<Bas_Permisson>();
|
|
if (CacheHelper.Exists(cacheKey))//有缓存就从缓存中取
|
|
list = CacheHelper.Get<List<Bas_Permisson>>(cacheKey);
|
|
else
|
|
{
|
|
List<BAS_PERMISSION> permissionlist = GetList();
|
|
List<BAS_PERMISSION_BUTTON> buttonlist = GetPermissionList();
|
|
foreach (BAS_PERMISSION item in permissionlist.Where(m => m.PRENTID == 0))//首页
|
|
{
|
|
Bas_Permisson model = new Bas_Permisson();
|
|
model.ID = item.ID;
|
|
model.ICON = item.ICON;
|
|
model.ISHIDDEN = item.ISHIDDEN;
|
|
model.NAME = item.NAME;
|
|
model.PRENTID = item.PRENTID;
|
|
model.SORT = item.SORT;
|
|
model.TYPE = item.TYPE;
|
|
model.CEID = item.CEID;
|
|
model.CTIME = item.CTIME;
|
|
model.ISFORBIDDEN = item.ISFORBIDDEN;
|
|
model.URL = item.URL;
|
|
model.CODE = item.CODE;
|
|
model.isblank = item.isblank;
|
|
model.buttonlist = ToButtonList(buttonlist.Where(m => m.PID == model.ID).OrderBy(m => m.BUTTONID).ToList());
|
|
List<BAS_PERMISSION> childList = permissionlist.Where(m => m.PRENTID == item.ID).ToList();
|
|
if (childList.Count > 0)
|
|
model.ISHAVECHILD = 1;
|
|
else
|
|
model.ISHAVECHILD = 0;
|
|
list.Add(model);
|
|
model.PARENTSTR = "";
|
|
GetChildPermisson(list, permissionlist, childList, buttonlist, model.PARENTSTR + string.Format("[{0}]", model.ID));
|
|
}
|
|
CacheHelper.Set(cacheKey, list);//设置缓存
|
|
}
|
|
return list;
|
|
}
|
|
private void GetChildPermisson(List<Bas_Permisson> list, List<BAS_PERMISSION> permissionlist, List<BAS_PERMISSION> childList, List<BAS_PERMISSION_BUTTON> buttonlist, string parentIds)
|
|
{
|
|
foreach (BAS_PERMISSION item in childList)//首页
|
|
{
|
|
Bas_Permisson model = new Bas_Permisson();
|
|
model.ID = item.ID;
|
|
model.ICON = item.ICON;
|
|
model.ISHIDDEN = item.ISHIDDEN;
|
|
model.NAME = item.NAME;
|
|
model.PRENTID = item.PRENTID;
|
|
model.SORT = item.SORT;
|
|
model.TYPE = item.TYPE;
|
|
model.CEID = item.CEID;
|
|
model.ISFORBIDDEN = item.ISFORBIDDEN;
|
|
model.CTIME = item.CTIME;
|
|
model.URL = item.URL;
|
|
model.CODE = item.CODE;
|
|
model.isblank = item.isblank;
|
|
model.buttonlist = ToButtonList(buttonlist.Where(m => m.PID == model.ID).OrderBy(m => m.BUTTONID).ToList());
|
|
List<BAS_PERMISSION> childList2 = permissionlist.Where(m => m.PRENTID == item.ID).ToList();
|
|
if (childList2.Count > 0)
|
|
model.ISHAVECHILD = 1;
|
|
else
|
|
model.ISHAVECHILD = 0;
|
|
model.PARENTSTR = parentIds;
|
|
list.Add(model);
|
|
GetChildPermisson(list, permissionlist, childList2, buttonlist, model.PARENTSTR + string.Format("[{0}]", model.ID));
|
|
}
|
|
}
|
|
private List<Bas_Permisson_Button> ToButtonList(List<BAS_PERMISSION_BUTTON> buttons)
|
|
{
|
|
List<Bas_Permisson_Button> list = new List<Bas_Permisson_Button>();
|
|
foreach (var item in buttons)
|
|
{
|
|
Bas_Permisson_Button model = new Bas_Permisson_Button()
|
|
{
|
|
BUTTONID = item.BUTTONID,
|
|
CODE = item.CODE,
|
|
ID = item.ID,
|
|
NAME = item.NAME,
|
|
PID = item.PID,
|
|
SITE = item.SITE
|
|
};
|
|
list.Add(model);
|
|
}
|
|
return list;
|
|
}
|
|
#region 获取单个实体
|
|
/// <summary>
|
|
/// 获取单个实体
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public Bas_Permisson GetModel(int id)
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
BAS_PERMISSION item = db.BAS_PERMISSION.SingleOrDefault(w => w.ID == id);
|
|
if (item == null)
|
|
{
|
|
throw new Exception("找不到数据!");
|
|
}
|
|
Bas_Permisson model = new Bas_Permisson();
|
|
model.ID = item.ID;
|
|
model.ICON = item.ICON;
|
|
model.ISHIDDEN = item.ISHIDDEN;
|
|
model.NAME = item.NAME;
|
|
model.PRENTID = item.PRENTID;
|
|
model.SORT = item.SORT;
|
|
model.TYPE = item.TYPE;
|
|
model.CEID = item.CEID;
|
|
model.ISFORBIDDEN = item.ISFORBIDDEN;
|
|
model.CTIME = item.CTIME;
|
|
model.URL = item.URL;
|
|
model.CODE = item.CODE;
|
|
model.isblank = item.isblank;
|
|
model.buttonlist = ToButtonList(db.BAS_PERMISSION_BUTTON.Where(a => a.PID == id).OrderBy(m => m.BUTTONID).ToList());
|
|
return model;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 修改顺序
|
|
/// <summary>
|
|
/// 批量修改顺序
|
|
/// </summary>
|
|
/// <param name="id1">角色ID</param>
|
|
/// <param name="id2">排序号</param>
|
|
/// <returns></returns>
|
|
public bool Sort(ref ValidationErrors errors, int id1, int id2)
|
|
{
|
|
try
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
BAS_PERMISSION entry1 = db.BAS_PERMISSION.FirstOrDefault(m => m.ID == id1);
|
|
BAS_PERMISSION entry2 = db.BAS_PERMISSION.FirstOrDefault(m => m.ID == id2);
|
|
if (entry1 == null || entry2 == null)
|
|
return false;
|
|
int sort = entry1.SORT;
|
|
entry1.SORT = entry2.SORT;
|
|
entry2.SORT = sort;
|
|
|
|
db.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
catch (Exception ex) { errors.Add(ex.Message); return false; }
|
|
finally
|
|
{
|
|
CacheHelper.Remove(cacheKey);//清除缓存
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 删除
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="errors"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public bool Delete(ref ValidationErrors errors, decimal id)
|
|
{
|
|
try
|
|
{
|
|
using (zxdContext db = new zxdContext())
|
|
{
|
|
|
|
db.BAS_PERMISSION_BUTTON.RemoveRange(db.BAS_PERMISSION_BUTTON.Where(m => m.PID == id));
|
|
|
|
BAS_PERMISSION entry = db.BAS_PERMISSION.FirstOrDefault(m => m.ID == id);
|
|
if (entry == null)
|
|
{
|
|
errors.Add("数据已删除!");
|
|
return false;
|
|
}
|
|
db.BAS_PERMISSION.Remove(entry);
|
|
return db.SaveChanges().GetResult();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errors.Add(ex.Message);
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
CacheHelper.Remove(cacheKey);//清除缓存
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|