DepartmentManage.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using Ant.Data;
  2. using Ant.ORM;
  3. using Ant.Service.Common;
  4. using Central.Control.Domain;
  5. using MES.Production.Entity;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. namespace MES.Production.Service.ServiceImp
  12. {
  13. /// <summary>
  14. /// Service层部门管理
  15. /// add 作者: 季健国 QQ:181589805 by 2016-05-22
  16. /// </summary>
  17. public class DepartmentManage : RepositoryBase<SYS_DISTRIBUTORS>, IService.IDepartmentManage
  18. {
  19. /// <summary>
  20. /// 自动创建部门编号
  21. /// add 作者: 季健国 QQ:181589805 by 2016-06-03
  22. /// <param name="parentId">上级部门ID 注:ID不是Code,数据表已改</param>
  23. /// </summary>
  24. public string CreateCode(string parentId)
  25. {
  26. string _strCode = string.Empty;
  27. #region 验证上级部门code是否为空,为空返回,第一级部门的Code
  28. if (string.IsNullOrEmpty(parentId))
  29. {
  30. //注意:Oracle存储值为空=null MsSql 空=空 null=null
  31. int num = 1;
  32. var query = this.LoadAll(p => p.PARENTID == null || p.PARENTID == "").OrderBy(p => p.CODE).ToList();
  33. if (!query.Any())
  34. {
  35. return num.ToString("D5");
  36. }
  37. //按照之前的逻辑,查漏补缺
  38. for (int i = 1; i <= query.Count; i++)
  39. {
  40. string code = query[i - 1].CODE;
  41. if (string.IsNullOrEmpty(code))
  42. {
  43. return FormatCode(i, 1);
  44. }
  45. if (i != int.Parse(code))
  46. {
  47. return FormatCode(i, 1);
  48. }
  49. }
  50. return FormatCode(query.Count + 1, 1);
  51. }
  52. #endregion
  53. #region 上级部门不为空,返回当前上级部门下的部门Code
  54. /* *根据部门编号获取下级部门 查询条件为:
  55. * 1.下级部门编号长度=当前部门编号+3
  56. * 2.下级部门上级部门ID=当前部门ID
  57. * */
  58. var parentDpt = this.Get(p => p.ID == parentId);
  59. if (parentDpt != null)//上级部门存在
  60. {
  61. //查询同等级部门下的所有数据
  62. var queryable = this.LoadAll(p => p.PARENTID == parentId).OrderBy(p => p.CODE).ToList();
  63. if (queryable.Any())
  64. {
  65. //需要验证是否存在编号缺失的情况 方法:遍历下级部门列表,
  66. //用部门编号去掉上级部门编号,然后转化成数字和for循环的索引进行对比,遇到第一个不相等时,返回此编号,并跳出循环
  67. for (int i = 1; i <= queryable.Count; i++)
  68. {
  69. string _code = queryable[i - 1].CODE;
  70. _code = _code.Substring(parentDpt.CODE.Length);
  71. int _intCode = 0;
  72. Int32.TryParse(_code, out _intCode);
  73. //下级部门编号中不存在
  74. if (i != _intCode)
  75. {
  76. //返回此编号,并退出循环
  77. _strCode = parentDpt.CODE + FormatCode(i, parentDpt.BUSINESSLEVEL.ToInt32() + 1);
  78. return _strCode;
  79. }
  80. }
  81. //不存在编号缺失情况
  82. _strCode = parentDpt.CODE + FormatCode(queryable.Count + 1, parentDpt.BUSINESSLEVEL.ToInt32() + 1);
  83. }
  84. else
  85. {
  86. _strCode = parentDpt.CODE + FormatCode(1, parentDpt.BUSINESSLEVEL.ToInt32() + 1);
  87. return _strCode;
  88. }
  89. }//上级部门不存在,返回空,这种情况基本不会出现
  90. #endregion
  91. return _strCode;
  92. }
  93. /// <summary>
  94. /// 功能描述:根据传入的数字 返回补码后的3位部门编号
  95. /// 创建标号:add by 季健国 2013-7-18 12:01
  96. /// </summary>
  97. public string FormatCode(int dptCode, int num)
  98. {
  99. try
  100. {
  101. string code = string.Empty;
  102. switch (num)
  103. {
  104. case 1:
  105. code = dptCode.ToString("D3"); break;
  106. case 2:
  107. code = dptCode.ToString("D5"); break;
  108. case 3:
  109. code = dptCode.ToString("D3"); break;
  110. default:
  111. code = dptCode.ToString("D3"); break;
  112. }
  113. return code;
  114. //string _strCode = string.Empty;
  115. ////<=9 一位数
  116. //if (dptCode <= 9 && dptCode >= 1)
  117. //{
  118. // return "00" + dptCode;
  119. //}
  120. ////<=99 两位数
  121. //else if (dptCode <= 99 && dptCode > 9)
  122. //{
  123. // return "0" + dptCode;
  124. //}
  125. ////<==999 三位数
  126. //else if (dptCode <= 999 && dptCode > 99)
  127. //{
  128. // return _strCode;
  129. //}
  130. //return string.Empty;
  131. }
  132. catch (Exception ex)
  133. {
  134. throw ex;
  135. }
  136. }
  137. /// <summary>
  138. /// 验证当前删除的部门是否存在下级部门
  139. /// </summary>
  140. public bool DepartmentIsExists(string idlist)
  141. {
  142. return this.IsExist(p => idlist.Contains(p.PARENTID));
  143. }
  144. /// <summary>
  145. /// 递归部门列表,返回排序后的对象集合
  146. /// add 作者: 季健国 QQ:181589805 by 2016-06-03
  147. /// </summary>
  148. public List<SYS_DISTRIBUTORS> RecursiveDepartment(List<SYS_DISTRIBUTORS> list)
  149. {
  150. var result = new List<SYS_DISTRIBUTORS>();
  151. if (list.Count > 0)
  152. {
  153. ChildDepartment(list, result, null);
  154. }
  155. return result;
  156. }
  157. /// <summary>
  158. /// 递归部门列表,返回排序后的对象集合
  159. /// add 作者: 季健国 QQ:181589805 by 2016-06-03
  160. /// </summary>
  161. public List<SYS_DISTRIBUTORS> RecursiveDepartmentNew(List<SYS_DISTRIBUTORS> list)
  162. {
  163. var result = new List<SYS_DISTRIBUTORS>();
  164. if (list.Count > 0)
  165. {
  166. ChildDepartmentNew(list, result, null);
  167. }
  168. return result;
  169. }
  170. /// <summary>
  171. /// 根据部门ID递归部门列表,返回子部门+本部门的对象集合
  172. /// add 作者: 季健国 QQ:181589805 by 2016-06-03
  173. /// </summary>
  174. public List<SYS_DISTRIBUTORS> RecursiveDepartment(string parentId)
  175. {
  176. //原始数据
  177. var list = this.LoadAll(null);
  178. //新数据
  179. var result = new List<SYS_DISTRIBUTORS>();
  180. if (list.Any())
  181. {
  182. result.AddRange(list.Where(p => p.ID == parentId).ToList());
  183. if (!string.IsNullOrEmpty(parentId))
  184. ChildDepartment(list.ToList(), result, parentId);
  185. else
  186. ChildDepartment(list.ToList(), result, null);//oracle使用null sql使用空
  187. }
  188. return result;
  189. }
  190. /// <summary>
  191. /// 根据部门ID递归部门列表,返回子部门+本部门的对象集合
  192. /// add 作者: 季健国 QQ:181589805 by 2016-06-03
  193. /// </summary>
  194. public List<SYS_DISTRIBUTORS> RecursiveDepartmentNew(string parentId)
  195. {
  196. //原始数据
  197. var list = this.LoadAll(p => p.BUSINESSLEVEL == 2);
  198. //新数据
  199. var result = new List<SYS_DISTRIBUTORS>();
  200. if (list.Any())
  201. {
  202. result.AddRange(list.Where(p => p.ID == parentId).ToList());
  203. if (!string.IsNullOrEmpty(parentId))
  204. ChildDepartmentNew(list.ToList(), result, parentId);
  205. else
  206. ChildDepartmentNew(list.ToList(), result, null);//oracle使用null sql使用空
  207. }
  208. return result;
  209. }
  210. private void ChildDepartment(List<SYS_DISTRIBUTORS> newlist, List<SYS_DISTRIBUTORS> list, string id)
  211. {
  212. if (string.IsNullOrEmpty(id))
  213. {
  214. var result = newlist.Where(p => (p.PARENTID == null || p.PARENTID == "")).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList();
  215. if (result.Any())
  216. {
  217. for (int i = 0; i < result.Count(); i++)
  218. {
  219. list.Add(result[i]);
  220. ChildDepartment(newlist, list, result[i].ID);
  221. }
  222. }
  223. }
  224. else
  225. {
  226. var result = newlist.Where(p => p.PARENTID == id).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList();
  227. if (result.Any())
  228. {
  229. for (int i = 0; i < result.Count(); i++)
  230. {
  231. list.Add(result[i]);
  232. ChildDepartment(newlist, list, result[i].ID);
  233. }
  234. }
  235. }
  236. }
  237. private void ChildDepartmentNew(List<SYS_DISTRIBUTORS> newlist, List<SYS_DISTRIBUTORS> list, string id)
  238. {
  239. if (string.IsNullOrEmpty(id))
  240. {
  241. var result = newlist.Where(p => (p.PARENTID == null || p.PARENTID == "")).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList();
  242. if (result.Any())
  243. {
  244. for (int i = 0; i < result.Count(); i++)
  245. {
  246. list.Add(result[i]);
  247. ChildDepartmentNew(newlist, list, result[i].ID);
  248. }
  249. }
  250. }
  251. else
  252. {
  253. var result = newlist.Where(p => p.PARENTID == id).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList();
  254. if (result.Any())
  255. {
  256. for (int i = 0; i < result.Count(); i++)
  257. {
  258. list.Add(result[i]);
  259. ChildDepartmentNew(newlist, list, result[i].ID);
  260. }
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 根据部门ID获取部门名称,不存在返回空
  266. /// </summary>
  267. public string GetDepartmentName(string id)
  268. {
  269. var query = this.LoadAll(p => p.ID == id);
  270. if (query == null || !query.Any())
  271. return "";
  272. return query.First().NAME;
  273. }
  274. /// <summary>
  275. /// 显示错层方法
  276. /// </summary>
  277. public object GetDepartmentName(string name, decimal? level)
  278. {
  279. if (level > 1)
  280. {
  281. string nbsp = "&nbsp;&nbsp;";
  282. for (int i = 0; i < level; i++)
  283. {
  284. nbsp += "&nbsp;&nbsp;";
  285. }
  286. name = nbsp + "|--" + name;
  287. }
  288. return name;
  289. }
  290. /// <summary>
  291. /// 获取父级列表
  292. /// </summary>
  293. public IList GetDepartmentByDetail()
  294. {
  295. var list = RecursiveDepartment(this.LoadAll(null).ToList())
  296. .Select(p => new
  297. {
  298. id = p.ID,
  299. code = p.CODE,
  300. name = GetDepartmentName(p.NAME, p.BUSINESSLEVEL)
  301. }).ToList();
  302. return JsonConverter.JsonClass(list);
  303. }
  304. /// <summary>
  305. /// 获取父级列表
  306. /// </summary>
  307. public IList GetDepartmentByDetailNew(int salseid, int? prov = 0)
  308. {
  309. if (salseid > 0)
  310. {
  311. using (AntORM orm = new AntORM())
  312. {
  313. orm.db = Ant.Data.DataAccessFactory.CreateDataConnection("CyclingItem");
  314. RequestModel request = new RequestModel();
  315. request.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" };
  316. var dis = orm.Queryable<EntSYS_DISTRIBUTORS>();
  317. var userdis = orm.Queryable<EntYW_UserDistributor>();
  318. IJoiningQuery<EntSYS_DISTRIBUTORS, EntYW_UserDistributor> view = dis.LeftJoin(userdis, (user, city) => user.ID == city.distributorId);
  319. var q = view.Select((user, city) => new { Users = user, Citys = city }).Where(a => a.Citys.userId == salseid);
  320. if (prov > 0)
  321. {
  322. q = q.Where(p => p.Users.Province == prov);
  323. }
  324. var disreslut = q.OrderByDesc(a => a.Users.DealerNumber).ThenBy(a => a.Users.BUSINESSLEVEL).ToList(request);
  325. List<SYS_DISTRIBUTORS> dislist = new List<SYS_DISTRIBUTORS>();
  326. SYS_DISTRIBUTORS moddis = new SYS_DISTRIBUTORS();
  327. moddis.ID = "32f7a4bd-84de-4587-be29-734d65ad6f70";
  328. moddis.NAME = "常发经销商及仓库信息";
  329. dislist.Add(moddis);
  330. if (disreslut.IsSuccess)
  331. {
  332. var list = disreslut.ResultModel;
  333. foreach (var mod in list)
  334. {
  335. EntSYS_DISTRIBUTORS dismod = mod.Users;
  336. dismod.NAME = GetDepartmentNameNew(dismod.NAME, dismod.BUSINESSLEVEL.ToDec()).ToString();
  337. SYS_DISTRIBUTORS disenty = new SYS_DISTRIBUTORS();
  338. disenty.ID = dismod.ID;
  339. disenty.NAME = dismod.NAME;
  340. dislist.Add(disenty);
  341. }
  342. return JsonConverter.JsonClass(dislist);
  343. }
  344. }
  345. }
  346. else
  347. {
  348. using (AntORM orm = new AntORM())
  349. {
  350. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  351. RequestModel request = new RequestModel
  352. {
  353. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询菜单的单个实体方法" }
  354. };
  355. var q = orm.Queryable<EntSYS_DISTRIBUTORS>();
  356. if (prov > 0)
  357. {
  358. q = q.Where(p => p.Province == prov);
  359. }
  360. q = q.Where(p => p.BUSINESSLEVEL > 1);
  361. var disreslut = q.OrderByDesc(a => a.DealerNumber).ThenBy(a => a.BUSINESSLEVEL).ToList(request);
  362. if (disreslut.IsSuccess)
  363. {
  364. List<SYS_DISTRIBUTORS> dislist = new List<SYS_DISTRIBUTORS>();
  365. SYS_DISTRIBUTORS moddis = new SYS_DISTRIBUTORS();
  366. moddis.ID = "32f7a4bd-84de-4587-be29-734d65ad6f70";
  367. moddis.NAME = "常发经销商及仓库信息";
  368. dislist.Add(moddis);
  369. var list = disreslut.ResultModel;
  370. foreach (var mod in list)
  371. {
  372. EntSYS_DISTRIBUTORS dismod = mod;
  373. dismod.NAME = GetDepartmentNameNew(dismod.NAME, dismod.BUSINESSLEVEL.ToDec()).ToString();
  374. SYS_DISTRIBUTORS disenty = new SYS_DISTRIBUTORS
  375. {
  376. ID = dismod.ID,
  377. NAME = dismod.NAME
  378. };
  379. dislist.Add(disenty);
  380. }
  381. return JsonConverter.JsonClass(dislist);
  382. }
  383. }
  384. }
  385. return JsonConverter.JsonClass(null);
  386. }
  387. /// <summary>
  388. /// 获取父级列表
  389. /// </summary>
  390. public IList GetDepartmentByDetailNew()
  391. {
  392. var list = RecursiveDepartmentNew(this.LoadAll(p => p.BUSINESSLEVEL > 0).ToList())
  393. .Select(p => new
  394. {
  395. ID = p.ID,
  396. CODE = p.CODE,
  397. NAME = GetDepartmentNameNew(p.NAME, p.BUSINESSLEVEL)
  398. }).ToList();
  399. return JsonConverter.JsonClass(list);
  400. }
  401. /// <summary>
  402. /// 显示错层方法
  403. /// </summary>
  404. public object GetDepartmentNameNew(string name, decimal? level)
  405. {
  406. if (level > 1)
  407. {
  408. string nbsp = "&nbsp;&nbsp;";
  409. for (int i = 0; i < level; i++)
  410. {
  411. nbsp += "&nbsp;&nbsp;";
  412. }
  413. name = nbsp + "|--" + name;
  414. }
  415. return name;
  416. }
  417. }
  418. }