using Central.Control.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MES.Production.Service.ServiceImp
{
///
/// Service模型处理类
/// add 作者: 季健国 QQ:181589805 by 2016-05-22
///
public class ModuleManage : RepositoryBase, IService.IModuleManage
{
///
/// 获取用户权限模块集合
/// add 作者: 季健国 QQ:181589805 by 2016-05-30
///
/// 用户ID
/// 用户授权集合
/// 站点ID
///
public List GetModule(int userId, List permission,string siteId)
{
//返回模块
var retmodule = new List();
var permodule = new List();
//权限转模块
if (permission != null)
{
permodule.AddRange(permission.Select(p => p.SYS_MODULE));
//去重
permodule = permodule.Distinct(new ModuleDistinct()).ToList();
}
//检索显示与系统
permodule = permodule.Where(p => p.ISSHOW == 1 && p.FK_BELONGSYSTEM.ToString() == siteId).ToList();
//构造上级导航模块
var prevModule = this.LoadListAll(p => p.FK_BELONGSYSTEM.ToString() == siteId);
//反向递归算法构造模块带上级上上级模块
if (permodule.Count > 0)
{
foreach (var item in permodule)
{
RecursiveModule(prevModule, retmodule, item.PARENTID);
retmodule.Add(item);
}
}
//去重
retmodule = retmodule.Distinct(new ModuleDistinct()).ToList();
//返回模块集合
return retmodule.OrderBy(p => p.LEVELS).ThenBy(p => p.SHOWORDER).ToList();
}
///
/// 反向递归模块集合,可重复模块数据,最后去重
///
/// 总模块
/// 返回模块
/// 上级ID
private void RecursiveModule(List PrevModule, List retmodule, int? parentId)
{
var result = PrevModule.Where(p => p.ID == parentId);
if (result != null)
{
foreach (var item in result)
{
retmodule.Add(item);
RecursiveModule(PrevModule, retmodule, item.PARENTID);
}
}
}
///
/// 递归模块列表,返回按级别排序
/// add 作者: 季健国 QQ:181589805 by 2016-06-03
///
public List RecursiveModule(List list)
{
List result = new List();
if (list!=null && list.Count>0)
{
ChildModule(list, result, 0);
}
return result;
}
///
/// 递归模块列表
/// add 作者: 季健国 QQ:181589805 by 2016-06-03
///
private void ChildModule(List list, List newlist, int parentId)
{
var result = list.Where(p => p.PARENTID == parentId).OrderBy(p => p.LEVELS).OrderBy(p => p.SHOWORDER).ToList();
if (result.Count() > 0)
{
for (int i = 0; i < result.Count(); i++)
{
newlist.Add(result[i]);
ChildModule(list, newlist, result[i].ID);
}
}
}
///
/// 批量变更下级模块的级别
///
public bool MoreModifyModule(int moduleId, int levels)
{
//根据当前模块ID获取下级模块的集合
var ChildModule = this.LoadAll(p => p.PARENTID == moduleId).ToList();
if (ChildModule.Any())
{
foreach (var item in ChildModule)
{
item.LEVELS = levels + 1;
this.Update(item);
MoreModifyModule(item.ID, item.LEVELS);
}
}
return true;
}
}
///
/// 模型去重,非常重要
/// add 作者: 季健国 QQ:181589805 by 2016-08-03
///
public class ModuleDistinct : IEqualityComparer
{
public bool Equals(SYS_MODULE x, SYS_MODULE y)
{
return x.ID == y.ID;
}
public int GetHashCode(SYS_MODULE obj)
{
return obj.ToString().GetHashCode();
}
}
}