143 lines
7.0 KiB
C#
143 lines
7.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using WX.CRM.BLL.Util;
|
||
using WX.CRM.Common;
|
||
using WX.CRM.IBLL.Base;
|
||
namespace WX.CRM.BLL.Base
|
||
{
|
||
public class BAS_INNERUSERROLE_BL : IBAS_INNERUSERROLE, IBAS_INNERUSERROLE_Q
|
||
{
|
||
|
||
#region 用户角色列表信息
|
||
public List<WX.CRM.Model.Entity.BAS_INNERUSERROLE_Extend> GetList(ref Pager pager, string eId, string uName, string roleId)
|
||
{
|
||
using (WX.CRM.Model.Entity.crmContext db = new WX.CRM.Model.Entity.crmContext())
|
||
{
|
||
IQueryable<WX.CRM.Model.Entity.BAS_INNERUSER> queryData = db.BAS_INNERUSER.AsQueryable();//
|
||
if (!string.IsNullOrWhiteSpace(eId))
|
||
{
|
||
decimal ieId = Convert.ToInt32(eId);
|
||
queryData = queryData.Where(m => m.EID.Equals(ieId));
|
||
}
|
||
if (!string.IsNullOrWhiteSpace(uName))
|
||
{
|
||
queryData = queryData.Where(m => m.UNAME.Contains(uName));
|
||
}
|
||
if (!string.IsNullOrWhiteSpace(roleId) && roleId != "0")
|
||
{
|
||
string[] roleIdStr = roleId.Split(',');
|
||
decimal[] roleIds = new decimal[roleIdStr.Length];
|
||
for (int i = 0; i < roleIdStr.Length; i++)
|
||
{
|
||
roleIds[i] = Convert.ToDecimal(roleIdStr[i]);
|
||
}
|
||
queryData = queryData.Where(m => (from a in db.BAS_INNERUSERROLE where roleIds.Contains(a.ROLEID) select a.INNERUSERID).Contains(m.PKID));
|
||
|
||
}
|
||
queryData = queryData.OrderByDescending(m => m.CTIME);
|
||
PagerUtil.SetPager<WX.CRM.Model.Entity.BAS_INNERUSER>(ref queryData, ref pager);//分页
|
||
List<WX.CRM.Model.Entity.BAS_INNERUSERROLE_Extend> list = (from a in queryData
|
||
select new WX.CRM.Model.Entity.BAS_INNERUSERROLE_Extend()
|
||
{
|
||
EID = a.EID,
|
||
PKID = a.PKID,
|
||
UNAME = a.UNAME,
|
||
ROLEIDS = "",
|
||
ROLESNAMES = ""
|
||
|
||
}).ToList();
|
||
List<WX.CRM.Model.Entity.BAS_ROLE> roleList = db.BAS_ROLE.ToList();
|
||
foreach (var item in list)
|
||
{
|
||
decimal pkid = item.PKID;
|
||
var roleids = (from a in db.BAS_INNERUSERROLE where a.INNERUSERID.Equals(pkid) select a.ROLEID).ToList();
|
||
foreach (var role in roleList.Where(m => roleids.Contains(m.ROLEID)))//赋值
|
||
{
|
||
item.ROLEIDS += role.ROLEID + ",";
|
||
item.ROLESNAMES += "[" + role.RNAME + "] ";
|
||
}
|
||
if (item.ROLEIDS != null && item.ROLEIDS.Length > 0)
|
||
item.ROLEIDS = item.ROLEIDS.Substring(0, item.ROLEIDS.Length - 1);
|
||
}
|
||
return list;
|
||
}
|
||
}
|
||
#endregion
|
||
/// <summary>
|
||
/// 保存员工角色
|
||
/// </summary>
|
||
/// <param name="user">当前操作人员</param>
|
||
/// <param name="roleIds">角色的ID集合(","隔开)</param>
|
||
/// <param name="pkids">员工ID集合(","隔开)</param>
|
||
/// <returns></returns>
|
||
public bool Save(ref ValidationErrors errors, int user, string roleIds, string pkids)
|
||
{
|
||
try
|
||
{
|
||
using (var db = new WX.CRM.Model.Entity.crmContext())
|
||
{
|
||
string[] roleIdStr = roleIds == null? "".Split(','): roleIds.Split(',');
|
||
string[] pkIdStr = pkids.Split(',');
|
||
decimal[] roleIdArry = new decimal[roleIdStr.Length];
|
||
decimal[] pkIdArry = new decimal[pkIdStr.Length];
|
||
for (int i = 0; i < pkIdStr.Length; i++)
|
||
{
|
||
if (pkIdStr[i] == "" || pkIdStr[i] == "0")
|
||
continue;
|
||
pkIdArry[i] = Convert.ToDecimal(pkIdStr[i]);
|
||
decimal pkid = pkIdArry[i];
|
||
for (int n = 0; n < roleIdStr.Length; n++)
|
||
{
|
||
if (roleIdStr[n] == "" || roleIdStr[n] == "0")
|
||
continue;
|
||
roleIdArry[n] = Convert.ToDecimal(roleIdStr[n]);
|
||
decimal roleid = roleIdArry[n];
|
||
var entry = db.BAS_INNERUSERROLE.FirstOrDefault(m => m.ROLEID.Equals(roleid) && m.INNERUSERID.Equals(pkid));
|
||
if (entry == null)
|
||
{
|
||
WX.CRM.Model.Entity.BAS_INNERUSERROLE model = new WX.CRM.Model.Entity.BAS_INNERUSERROLE();
|
||
model.ROLEID = roleIdArry[n];
|
||
model.INNERUSERID = pkIdArry[i];
|
||
model.CTIME = DateTime.Now;
|
||
model.CREATEUSER = user;
|
||
db.BAS_INNERUSERROLE.Add(model);
|
||
}
|
||
}
|
||
}
|
||
//删除 取消的数据
|
||
foreach (WX.CRM.Model.Entity.BAS_INNERUSERROLE entry in (from a in db.BAS_INNERUSERROLE
|
||
where pkIdArry.Contains(a.INNERUSERID)
|
||
where !roleIdArry.Contains(a.ROLEID)
|
||
select a
|
||
))
|
||
{
|
||
db.BAS_INNERUSERROLE.Remove(entry);
|
||
};
|
||
db.SaveChanges();
|
||
return true;
|
||
}
|
||
|
||
}
|
||
catch (Exception ex) { errors.Add(ex.Message); return false; }
|
||
}
|
||
|
||
|
||
public List<WX.CRM.Model.Entity.BAS_INNERUSERROLE> GetInneruserRoleByUserId(decimal userId)
|
||
{
|
||
using (var db = new WX.CRM.Model.Entity.crmContext())
|
||
{
|
||
return db.BAS_INNERUSERROLE.Where(p => p.INNERUSERID.Equals(userId)).ToList();
|
||
}
|
||
}
|
||
|
||
public List<Model.Entity.BAS_INNERUSERROLE> GetInneruserRoleByUserIdList(List<decimal> userIds)
|
||
{
|
||
using (var db = new WX.CRM.Model.Entity.crmContext())
|
||
{
|
||
return db.BAS_INNERUSERROLE.Where(p => userIds.Contains(p.INNERUSERID)).ToList();
|
||
}
|
||
}
|
||
}
|
||
}
|