123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- 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
- {
- /// <summary>
- /// Service层部门管理
- /// add 作者: 季健国 QQ:181589805 by 2016-05-22
- /// </summary>
- public class DepartmentManage : RepositoryBase<SYS_DISTRIBUTORS>, IService.IDepartmentManage
- {
- /// <summary>
- /// 自动创建部门编号
- /// add 作者: 季健国 QQ:181589805 by 2016-06-03
- /// <param name="parentId">上级部门ID 注:ID不是Code,数据表已改</param>
- /// </summary>
- 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;
- }
- /// <summary>
- /// 功能描述:根据传入的数字 返回补码后的3位部门编号
- /// 创建标号:add by 季健国 2013-7-18 12:01
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 验证当前删除的部门是否存在下级部门
- /// </summary>
- public bool DepartmentIsExists(string idlist)
- {
- return this.IsExist(p => idlist.Contains(p.PARENTID));
- }
- /// <summary>
- /// 递归部门列表,返回排序后的对象集合
- /// add 作者: 季健国 QQ:181589805 by 2016-06-03
- /// </summary>
- public List<SYS_DISTRIBUTORS> RecursiveDepartment(List<SYS_DISTRIBUTORS> list)
- {
- var result = new List<SYS_DISTRIBUTORS>();
- if (list.Count > 0)
- {
- ChildDepartment(list, result, null);
- }
- return result;
- }
- /// <summary>
- /// 递归部门列表,返回排序后的对象集合
- /// add 作者: 季健国 QQ:181589805 by 2016-06-03
- /// </summary>
- public List<SYS_DISTRIBUTORS> RecursiveDepartmentNew(List<SYS_DISTRIBUTORS> list)
- {
- var result = new List<SYS_DISTRIBUTORS>();
- if (list.Count > 0)
- {
- ChildDepartmentNew(list, result, null);
- }
- return result;
- }
- /// <summary>
- /// 根据部门ID递归部门列表,返回子部门+本部门的对象集合
- /// add 作者: 季健国 QQ:181589805 by 2016-06-03
- /// </summary>
- public List<SYS_DISTRIBUTORS> RecursiveDepartment(string parentId)
- {
- //原始数据
- var list = this.LoadAll(null);
- //新数据
- var result = new List<SYS_DISTRIBUTORS>();
- 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;
- }
- /// <summary>
- /// 根据部门ID递归部门列表,返回子部门+本部门的对象集合
- /// add 作者: 季健国 QQ:181589805 by 2016-06-03
- /// </summary>
- public List<SYS_DISTRIBUTORS> RecursiveDepartmentNew(string parentId)
- {
- //原始数据
- var list = this.LoadAll(p => p.BUSINESSLEVEL == 2);
- //新数据
- var result = new List<SYS_DISTRIBUTORS>();
- 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<SYS_DISTRIBUTORS> newlist, List<SYS_DISTRIBUTORS> 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<SYS_DISTRIBUTORS> newlist, List<SYS_DISTRIBUTORS> 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);
- }
- }
- }
- }
- /// <summary>
- /// 根据部门ID获取部门名称,不存在返回空
- /// </summary>
- public string GetDepartmentName(string id)
- {
- var query = this.LoadAll(p => p.ID == id);
- if (query == null || !query.Any())
- return "";
- return query.First().NAME;
- }
- /// <summary>
- /// 显示错层方法
- /// </summary>
- 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;
- }
- /// <summary>
- /// 获取父级列表
- /// </summary>
- 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);
- }
- /// <summary>
- /// 获取父级列表
- /// </summary>
- 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<EntSYS_DISTRIBUTORS>();
- var userdis = orm.Queryable<EntYW_UserDistributor>();
- IJoiningQuery<EntSYS_DISTRIBUTORS, EntYW_UserDistributor> 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<SYS_DISTRIBUTORS> dislist = new List<SYS_DISTRIBUTORS>();
- 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<EntSYS_DISTRIBUTORS>();
- 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<SYS_DISTRIBUTORS> dislist = new List<SYS_DISTRIBUTORS>();
- 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);
- }
- /// <summary>
- /// 获取父级列表
- /// </summary>
- 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);
- }
- /// <summary>
- /// 显示错层方法
- /// </summary>
- 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;
- }
- }
- }
|