using Central.Control.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MES.Production.Service.ServiceImp
{
///
/// 用户部门关系业务实现类
/// add 作者: 季健国 QQ:181589805 by 2016-09-08
///
public class UserDepartmentManage:RepositoryBase,IService.IUserDepartmentManage
{
///
/// 根据部门ID获取当前部门的所有用户ID集合
///
public List GetUserListByDptId(List dptId)
{
return this.LoadAll(p => dptId.Contains(p.DEPARTMENT_ID)).Select(p => p.SYS_USER).ToList();
}
///
/// 根据用户ID获取所在的部门ID集合
///
public List GetDptListByUserId(int userId)
{
return this.LoadAll(p => p.USER_ID == userId).Select(p=>p.SYS_DISTRIBUTORS).ToList();
}
///
/// 保存用户部门关系
///
/// 用户ID
/// 部门ID集合
public bool SaveUserDpt(int userId, string dptId)
{
try
{
//原始部门人员关系是否与当前设置一致,不一致重新构造
if (this.IsExist(p => p.USER_ID == userId))
{
//存在之后再对比是否一致
var oldCount = this.LoadAll(p => p.USER_ID == userId && dptId.Contains(p.DEPARTMENT_ID)).Select(p => p.DEPARTMENT_ID).ToList();
var newdptid = dptId.Split(',').OrderBy(c => c).ToList();
if (oldCount.Count == newdptid.Count && oldCount.All(newdptid.Contains)) return true;
//删除原有关系
this.Delete(p => p.USER_ID == userId);
}
if (!string.IsNullOrEmpty(dptId))
{
//添加现有关系
var list = dptId.Split(',').Select(item => new SYS_USER_DEPARTMENT()
{
DEPARTMENT_ID = item,
USER_ID = userId
}).ToList();
return this.SaveList(list) > 0;
}
return true;
}
catch (Exception e) { throw e.InnerException; }
}
}
}