OpenAuth.Net/OpenAuth.App/RevelanceManagerApp.cs

111 lines
3.9 KiB
C#
Raw Normal View History

2017-11-29 20:49:14 +08:00
using System;
2017-11-29 18:26:36 +08:00
using System.Collections.Generic;
using System.Linq;
2017-11-29 20:49:14 +08:00
using OpenAuth.Repository.Domain;
namespace OpenAuth.App
{
2017-12-11 00:27:24 +08:00
public class RevelanceManagerApp :BaseApp<Relevance>
{
/// <summary>
/// 添加关联
/// <para>比如给用户分配资源那么firstId就是用户IDsecIds就是资源ID列表</para>
/// </summary>
2017-12-12 17:19:46 +08:00
/// <param name="type">关联的类型如Define.USERRESOURCE</param>
public void Assign(string type, string firstId, string[] secIds)
{
Assign(type, secIds.ToLookup(u => firstId));
}
public void Assign(string key, ILookup<string, string> idMaps)
{
DeleteBy(key, idMaps);
2017-12-11 00:27:24 +08:00
UnitWork.BatchAdd((from sameVals in idMaps
from value in sameVals
select new Relevance
{
Key = key,
FirstId = sameVals.Key,
SecondId = value,
OperateTime = DateTime.Now
}).ToArray());
2017-12-11 00:27:24 +08:00
UnitWork.Save();
}
/// <summary>
/// 删除关联
/// </summary>
/// <param name="key">关联标识</param>
/// <param name="idMaps">关联的&lt;firstId, secondId&gt;数组</param>
public void DeleteBy(string key, ILookup<string, string> idMaps)
{
foreach (var sameVals in idMaps)
{
foreach (var value in sameVals)
{
2017-12-11 00:27:24 +08:00
Repository.Delete(u => u.Key == key && u.FirstId == sameVals.Key && u.SecondId == value);
}
}
}
/// <summary>
/// 取消关联
/// </summary>
2017-12-12 17:19:46 +08:00
/// <param name="type">关联的类型如Define.USERRESOURCE</param>
/// <param name="firstId">The first identifier.</param>
/// <param name="secIds">The sec ids.</param>
public void UnAssign(string type, string firstId, string[] secIds)
{
DeleteBy(type, secIds.ToLookup(u =>firstId));
}
public void DeleteBy(string key, params string[] firstIds)
{
2017-12-11 00:27:24 +08:00
Repository.Delete(u => firstIds.Contains(u.FirstId) && u.Key == key);
}
/// <summary>
/// 添加新的关联
/// </summary>
/// <param name="key">关联标识</param>
/// <param name="idMaps">关联的&lt;firstId, secondId&gt;数组</param>
public void AddRelevance(string key, ILookup<string, string> idMaps)
{
DeleteBy(key, idMaps);
2017-12-11 00:27:24 +08:00
UnitWork.BatchAdd<Relevance>((from sameVals in idMaps
from value in sameVals
select new Relevance
{
Key = key,
FirstId = sameVals.Key,
SecondId = value,
OperateTime = DateTime.Now
}).ToArray());
2017-12-11 00:27:24 +08:00
UnitWork.Save();
}
2017-11-29 18:26:36 +08:00
/// <summary>
/// 根据关联表的一个键获取另外键的值
/// </summary>
/// <param name="key">映射标识</param>
/// <param name="returnSecondIds">返回的是否为映射表的第二列,如果不是则返回第一列</param>
/// <param name="ids">已知的ID列表</param>
/// <returns>List&lt;System.String&gt;.</returns>
public List<string> Get(string key, bool returnSecondIds, params string[] ids)
{
if (returnSecondIds)
{
2017-12-11 00:27:24 +08:00
return Repository.Find(u => u.Key == key
2017-11-29 18:26:36 +08:00
&& ids.Contains(u.FirstId)).Select(u => u.SecondId).ToList();
}
else
{
2017-12-11 00:27:24 +08:00
return Repository.Find(u => u.Key == key
2017-11-29 18:26:36 +08:00
&& ids.Contains(u.SecondId)).Select(u => u.FirstId).ToList();
}
}
}
}