using Ant.Data; using Ant.ORM; using Ant.Service.Common; using Central.Control.Domain; using MES.Production.Entity; using System; using System.Collections; 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 DepartmentManage : RepositoryBase, IService.IDepartmentManage { /// /// 自动创建部门编号 /// add 作者: 季健国 QQ:181589805 by 2016-06-03 /// 上级部门ID 注:ID不是Code,数据表已改 /// public string CreateCode(string parentId) { string _strCode = string.Empty; #region 验证上级部门code是否为空,为空返回,第一级部门的Code if (string.IsNullOrEmpty(parentId)) { //注意:Oracle存储值为空=null MsSql 空=空 null=null int num = 1; var query = this.LoadAll(p => p.PARENTID == null || p.PARENTID == "").OrderBy(p => p.CODE).ToList(); if (!query.Any()) { return num.ToString("D5"); } //按照之前的逻辑,查漏补缺 for (int i = 1; i <= query.Count; i++) { string code = query[i - 1].CODE; if (string.IsNullOrEmpty(code)) { return FormatCode(i, 1); } if (i != int.Parse(code)) { return FormatCode(i, 1); } } return FormatCode(query.Count + 1, 1); } #endregion #region 上级部门不为空,返回当前上级部门下的部门Code /* *根据部门编号获取下级部门 查询条件为: * 1.下级部门编号长度=当前部门编号+3 * 2.下级部门上级部门ID=当前部门ID * */ var parentDpt = this.Get(p => p.ID == parentId); if (parentDpt != null)//上级部门存在 { //查询同等级部门下的所有数据 var queryable = this.LoadAll(p => p.PARENTID == parentId).OrderBy(p => p.CODE).ToList(); if (queryable.Any()) { //需要验证是否存在编号缺失的情况 方法:遍历下级部门列表, //用部门编号去掉上级部门编号,然后转化成数字和for循环的索引进行对比,遇到第一个不相等时,返回此编号,并跳出循环 for (int i = 1; i <= queryable.Count; i++) { string _code = queryable[i - 1].CODE; _code = _code.Substring(parentDpt.CODE.Length); int _intCode = 0; Int32.TryParse(_code, out _intCode); //下级部门编号中不存在 if (i != _intCode) { //返回此编号,并退出循环 _strCode = parentDpt.CODE + FormatCode(i, parentDpt.BUSINESSLEVEL.ToInt32() + 1); return _strCode; } } //不存在编号缺失情况 _strCode = parentDpt.CODE + FormatCode(queryable.Count + 1, parentDpt.BUSINESSLEVEL.ToInt32() + 1); } else { _strCode = parentDpt.CODE + FormatCode(1, parentDpt.BUSINESSLEVEL.ToInt32() + 1); return _strCode; } }//上级部门不存在,返回空,这种情况基本不会出现 #endregion return _strCode; } /// /// 功能描述:根据传入的数字 返回补码后的3位部门编号 /// 创建标号:add by 季健国 2013-7-18 12:01 /// public string FormatCode(int dptCode, int num) { try { string code = string.Empty; switch (num) { case 1: code = dptCode.ToString("D3"); break; case 2: code = dptCode.ToString("D5"); break; case 3: code = dptCode.ToString("D3"); break; default: code = dptCode.ToString("D3"); break; } return code; //string _strCode = string.Empty; ////<=9 一位数 //if (dptCode <= 9 && dptCode >= 1) //{ // return "00" + dptCode; //} ////<=99 两位数 //else if (dptCode <= 99 && dptCode > 9) //{ // return "0" + dptCode; //} ////<==999 三位数 //else if (dptCode <= 999 && dptCode > 99) //{ // return _strCode; //} //return string.Empty; } catch (Exception ex) { throw ex; } } /// /// 验证当前删除的部门是否存在下级部门 /// public bool DepartmentIsExists(string idlist) { return this.IsExist(p => idlist.Contains(p.PARENTID)); } /// /// 递归部门列表,返回排序后的对象集合 /// add 作者: 季健国 QQ:181589805 by 2016-06-03 /// public List RecursiveDepartment(List list) { var result = new List(); if (list.Count > 0) { ChildDepartment(list, result, null); } return result; } /// /// 递归部门列表,返回排序后的对象集合 /// add 作者: 季健国 QQ:181589805 by 2016-06-03 /// public List RecursiveDepartmentNew(List list) { var result = new List(); if (list.Count > 0) { ChildDepartmentNew(list, result, null); } return result; } /// /// 根据部门ID递归部门列表,返回子部门+本部门的对象集合 /// add 作者: 季健国 QQ:181589805 by 2016-06-03 /// public List RecursiveDepartment(string parentId) { //原始数据 var list = this.LoadAll(null); //新数据 var result = new List(); if (list.Any()) { result.AddRange(list.Where(p => p.ID == parentId).ToList()); if (!string.IsNullOrEmpty(parentId)) ChildDepartment(list.ToList(), result, parentId); else ChildDepartment(list.ToList(), result, null);//oracle使用null sql使用空 } return result; } /// /// 根据部门ID递归部门列表,返回子部门+本部门的对象集合 /// add 作者: 季健国 QQ:181589805 by 2016-06-03 /// public List RecursiveDepartmentNew(string parentId) { //原始数据 var list = this.LoadAll(p => p.BUSINESSLEVEL == 2); //新数据 var result = new List(); if (list.Any()) { result.AddRange(list.Where(p => p.ID == parentId).ToList()); if (!string.IsNullOrEmpty(parentId)) ChildDepartmentNew(list.ToList(), result, parentId); else ChildDepartmentNew(list.ToList(), result, null);//oracle使用null sql使用空 } return result; } private void ChildDepartment(List newlist, List list, string id) { if (string.IsNullOrEmpty(id)) { var result = newlist.Where(p => (p.PARENTID == null || p.PARENTID == "")).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList(); if (result.Any()) { for (int i = 0; i < result.Count(); i++) { list.Add(result[i]); ChildDepartment(newlist, list, result[i].ID); } } } else { var result = newlist.Where(p => p.PARENTID == id).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList(); if (result.Any()) { for (int i = 0; i < result.Count(); i++) { list.Add(result[i]); ChildDepartment(newlist, list, result[i].ID); } } } } private void ChildDepartmentNew(List newlist, List list, string id) { if (string.IsNullOrEmpty(id)) { var result = newlist.Where(p => (p.PARENTID == null || p.PARENTID == "")).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList(); if (result.Any()) { for (int i = 0; i < result.Count(); i++) { list.Add(result[i]); ChildDepartmentNew(newlist, list, result[i].ID); } } } else { var result = newlist.Where(p => p.PARENTID == id).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList(); if (result.Any()) { for (int i = 0; i < result.Count(); i++) { list.Add(result[i]); ChildDepartmentNew(newlist, list, result[i].ID); } } } } /// /// 根据部门ID获取部门名称,不存在返回空 /// public string GetDepartmentName(string id) { var query = this.LoadAll(p => p.ID == id); if (query == null || !query.Any()) return ""; return query.First().NAME; } /// /// 显示错层方法 /// public object GetDepartmentName(string name, decimal? level) { if (level > 1) { string nbsp = "  "; for (int i = 0; i < level; i++) { nbsp += "  "; } name = nbsp + "|--" + name; } return name; } /// /// 获取父级列表 /// public IList GetDepartmentByDetail() { var list = RecursiveDepartment(this.LoadAll(null).ToList()) .Select(p => new { id = p.ID, code = p.CODE, name = GetDepartmentName(p.NAME, p.BUSINESSLEVEL) }).ToList(); return JsonConverter.JsonClass(list); } /// /// 获取父级列表 /// public IList GetDepartmentByDetailNew(int salseid, int? prov = 0) { if (salseid > 0) { using (AntORM orm = new AntORM()) { orm.db = Ant.Data.DataAccessFactory.CreateDataConnection("CyclingItem"); RequestModel request = new RequestModel(); request.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" }; var dis = orm.Queryable(); var userdis = orm.Queryable(); IJoiningQuery view = dis.LeftJoin(userdis, (user, city) => user.ID == city.distributorId); var q = view.Select((user, city) => new { Users = user, Citys = city }).Where(a => a.Citys.userId == salseid); if (prov > 0) { q = q.Where(p => p.Users.Province == prov); } var disreslut = q.OrderByDesc(a => a.Users.DealerNumber).ThenBy(a => a.Users.BUSINESSLEVEL).ToList(request); List dislist = new List(); SYS_DISTRIBUTORS moddis = new SYS_DISTRIBUTORS(); moddis.ID = "32f7a4bd-84de-4587-be29-734d65ad6f70"; moddis.NAME = "常发经销商及仓库信息"; dislist.Add(moddis); if (disreslut.IsSuccess) { var list = disreslut.ResultModel; foreach (var mod in list) { EntSYS_DISTRIBUTORS dismod = mod.Users; dismod.NAME = GetDepartmentNameNew(dismod.NAME, dismod.BUSINESSLEVEL.ToDec()).ToString(); SYS_DISTRIBUTORS disenty = new SYS_DISTRIBUTORS(); disenty.ID = dismod.ID; disenty.NAME = dismod.NAME; dislist.Add(disenty); } return JsonConverter.JsonClass(dislist); } } } else { using (AntORM orm = new AntORM()) { orm.db = DataAccessFactory.CreateDataConnection("CyclingItem"); RequestModel request = new RequestModel { newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" } }; var q = orm.Queryable(); if (prov > 0) { q = q.Where(p => p.Province == prov); } q = q.Where(p => p.BUSINESSLEVEL > 1); var disreslut = q.OrderByDesc(a => a.DealerNumber).ThenBy(a => a.BUSINESSLEVEL).ToList(request); if (disreslut.IsSuccess) { List dislist = new List(); SYS_DISTRIBUTORS moddis = new SYS_DISTRIBUTORS(); moddis.ID = "32f7a4bd-84de-4587-be29-734d65ad6f70"; moddis.NAME = "常发经销商及仓库信息"; dislist.Add(moddis); var list = disreslut.ResultModel; foreach (var mod in list) { EntSYS_DISTRIBUTORS dismod = mod; dismod.NAME = GetDepartmentNameNew(dismod.NAME, dismod.BUSINESSLEVEL.ToDec()).ToString(); SYS_DISTRIBUTORS disenty = new SYS_DISTRIBUTORS { ID = dismod.ID, NAME = dismod.NAME }; dislist.Add(disenty); } return JsonConverter.JsonClass(dislist); } } } return JsonConverter.JsonClass(null); } /// /// 获取父级列表 /// public IList GetDepartmentByDetailNew() { var list = RecursiveDepartmentNew(this.LoadAll(p => p.BUSINESSLEVEL > 0).ToList()) .Select(p => new { ID = p.ID, CODE = p.CODE, NAME = GetDepartmentNameNew(p.NAME, p.BUSINESSLEVEL) }).ToList(); return JsonConverter.JsonClass(list); } /// /// 显示错层方法 /// public object GetDepartmentNameNew(string name, decimal? level) { if (level > 1) { string nbsp = "  "; for (int i = 0; i < level; i++) { nbsp += "  "; } name = nbsp + "|--" + name; } return name; } } }