FactoryRegionDal.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using DllEapEntity.Dtos;
  4. using log4net.Util;
  5. using SharpCifs.Util.Sharpen;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. namespace DllEapDal
  12. {
  13. public class FactoryRegionDal
  14. {
  15. private IDatabase CurrDb = null;
  16. public FactoryRegionDal(IDatabase db)
  17. {
  18. CurrDb = db;
  19. }
  20. public IEnumerable<FactoryRegion> Get(int start, int length, string sort, string order, string filter, string errorinfo)
  21. {
  22. var pros = CurrDb.FindListForCondition<FactoryRegion>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  23. return pros;
  24. }
  25. public int GetCount(string filter)
  26. {
  27. string errorinfo = string.Empty;
  28. var entities = CurrDb.FindListForCondition<FactoryRegion>(filter, ref errorinfo);
  29. if (entities != null)
  30. {
  31. return entities.Count();
  32. }
  33. return 0;
  34. }
  35. public FactoryRegion Get(int id)
  36. {
  37. var pro = CurrDb.FindEntityFor<FactoryRegion>(id);
  38. return pro;
  39. }
  40. /// <summary>
  41. /// 添加角色并返回角色Id
  42. /// </summary>
  43. /// <param name="role"></param>
  44. /// <param name="userCode"></param>
  45. /// <returns></returns>
  46. public int Add(FactoryRegion pro, string userCode, ref string errorinfo)
  47. {
  48. var entities = CurrDb.FindListForCondition<FactoryRegion>($" and " +
  49. $"a.FName='{pro.FName}' and a.ParentId='{pro.ParentId}'", ref errorinfo);
  50. if (entities != null && entities.Count() > 0)
  51. {
  52. errorinfo = "已存在相同名称的区域,请确认";
  53. return -1;
  54. }
  55. CurrDb.InsertFor(pro, userCode);
  56. var sql = "select @@identity;";
  57. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  58. return id;
  59. }
  60. /// <summary>
  61. /// 待优化 存在删除后不能
  62. /// </summary>
  63. /// <param name="role"></param>
  64. /// <param name="userCode"></param>
  65. /// <param name="errorinfo"></param>
  66. /// <returns></returns>
  67. public int Update(FactoryRegion role, string userCode, ref string errorinfo)
  68. {
  69. var entities = CurrDb.FindListForCondition<FactoryRegion>($" and a.FName='{role.FName}' " +
  70. $"and a.ID<>{role.Id} and a.ParentId={role.ParentId}", ref errorinfo);
  71. if (entities != null && entities.Count() > 0)
  72. {
  73. errorinfo = "已存在相同的名称的区域,请确认";
  74. return -1;
  75. }
  76. if (CurrDb.UpdateFor(role, userCode) < 0)
  77. {
  78. return -1;
  79. }
  80. return role.Id;
  81. }
  82. public IEnumerable<FactoryRegion> getFactoryRegion(int id)
  83. {
  84. var sql = $"select * from FactoryRegion where parentId={id}";
  85. return CurrDb.FindList<FactoryRegion>(sql);
  86. }
  87. public int Delete(int id, ref string msg)
  88. {
  89. var children = CurrDb.FindListForCondition<FactoryRegion>($" and a.ParentId={id}",
  90. ref msg);
  91. if (children != null && children.Count() > 0)
  92. {
  93. var sql = $"delete from FactoryRegion where parentId={id}";
  94. if (CurrDb.ExecuteBySql(sql) < 0)
  95. {
  96. msg = "删除该节点下的子节点失败";
  97. return -1;
  98. }
  99. }
  100. if (CurrDb.DeleteFor<FactoryRegion>(id) < 0)
  101. {
  102. msg = "删除失败";
  103. return -1;
  104. }
  105. msg = string.Empty;
  106. return 1;
  107. }
  108. /// <summary>
  109. /// 获取区域树
  110. /// </summary>
  111. /// <param name="filter"></param>
  112. /// <param name="errorinfo"></param>
  113. /// <returns></returns>
  114. public IEnumerable<FactoryRegionTreeDto> GetRegionTree(string filter, ref string errorinfo)
  115. {
  116. var datas = this.Get(1, 10000, "id", "asc", filter, errorinfo);
  117. var trees = new List<FactoryRegionTreeDto>();
  118. int level = 1;
  119. if (datas != null && datas.Count() > 0)
  120. {
  121. var minParentId = datas.Min(c => c.ParentId);
  122. var list = datas.Where(c => c.ParentId == minParentId); // 根节点
  123. if (list != null && list.Count() > 0)
  124. {
  125. trees = list.Select(c => new FactoryRegionTreeDto
  126. {
  127. Id = c.Id,
  128. FCode = c.FCode,
  129. FName = c.FName,
  130. Key = c.Id.ToString(),
  131. Name = c.FName,
  132. IsUse = c.IsUse,
  133. Level = level,
  134. Rank = c.Rank,
  135. Remark = c.Remark,
  136. RecCode = c.RecCode,
  137. RecTime = c.RecTime,
  138. ModCode = c.ModCode,
  139. ModTime = c.ModTime,
  140. RecName = c.RecName,
  141. ModName = c.ModName
  142. }).ToList();
  143. for (int i = 0; i < trees.Count; i++)
  144. {
  145. this.GetChildren(trees[i], datas, level);
  146. }
  147. }
  148. }
  149. return trees;
  150. }
  151. /// <summary>
  152. /// 根据ParentID获取子节点
  153. /// </summary>
  154. /// <param name="parentId"></param>
  155. /// <param name="datas"></param>
  156. /// <returns></returns>
  157. private void GetChildren(FactoryRegionTreeDto entity,
  158. IEnumerable<FactoryRegion> datas, int level)
  159. {
  160. var list = datas.Where(c => c.ParentId == entity.Id);
  161. List<FactoryRegionTreeDto> children = null;
  162. if (list != null && list.Count() > 0)
  163. {
  164. children = list.Select(c => new FactoryRegionTreeDto
  165. {
  166. Id = c.Id,
  167. FCode = c.FCode,
  168. FName = c.FName,
  169. IsUse = c.IsUse,
  170. Rank = c.Rank,
  171. Level = ++level,
  172. Key = c.Id.ToString(),
  173. Name = c.FName,
  174. Remark = c.Remark,
  175. RecCode = c.RecCode,
  176. RecTime = c.RecTime,
  177. ModCode = c.ModCode,
  178. ModTime = c.ModTime,
  179. RecName = c.RecName,
  180. ModName = c.ModName
  181. }).ToList();
  182. for (int i = 0; i < children.Count; i++)
  183. {
  184. this.GetChildren(children[i], datas, level);
  185. }
  186. entity.Children = children;
  187. }
  188. else
  189. {
  190. entity.Children = null;
  191. }
  192. }
  193. public IEnumerable<TreeSelectDto> GetRegionTreeSelect(int id, ref string errorinfo)
  194. {
  195. string filter = " and a.IsUse=1 ";
  196. if (id > 0)
  197. {
  198. filter += $" and a.Id<>{id}";
  199. }
  200. var datas = this.Get(1, 10000, "id", "asc", filter, errorinfo);
  201. var trees = new List<TreeSelectDto>();
  202. if (datas != null && datas.Count() > 0)
  203. {
  204. var list = datas.Where(c => c.ParentId == 0); // 根节点
  205. if (list != null && list.Count() > 0)
  206. {
  207. trees = list.Select(c => new TreeSelectDto
  208. {
  209. Title = c.FName,
  210. Key = c.Id,
  211. Label = c.FName,
  212. Value = c.Id
  213. }).ToList();
  214. for (int i = 0; i < trees.Count; i++)
  215. {
  216. this.GetSelectChildren(trees[i], datas);
  217. }
  218. }
  219. }
  220. return trees;
  221. }
  222. private void GetSelectChildren(TreeSelectDto entity,
  223. IEnumerable<FactoryRegion> datas)
  224. {
  225. var list = datas.Where(c => c.ParentId == Convert.ToInt32(entity.Key));
  226. if (list == null || list.Count() <= 0)
  227. {
  228. entity.Children = null;
  229. entity.IsLeaf = true;
  230. return;
  231. }
  232. var children = list.Select(c => new TreeSelectDto
  233. {
  234. Title = c.FName,
  235. Key = c.Id,
  236. Label = c.FName,
  237. Value = c.Id,
  238. ParentId =Convert.ToInt32(entity.Key)
  239. }).ToList();
  240. entity.Children = children;
  241. entity.IsLeaf = false;
  242. for (var i = 0; i < children.Count; i++)
  243. {
  244. this.GetSelectChildren(children[i], datas);
  245. }
  246. }
  247. /// <summary>
  248. /// 根据区域Id获取区域的完整名称
  249. /// </summary>
  250. /// <param name="regionId"></param>
  251. /// <returns></returns>
  252. public string GetFullRegionName(int regionId, IEnumerable<FactoryRegion> allRegions)
  253. {
  254. string fullName = "";
  255. var region = allRegions.FirstOrDefault(f => f.Id == regionId);
  256. if (region == null)
  257. {
  258. return string.Empty;
  259. }
  260. fullName = $"/{region.FName}";
  261. return this.GetFName(region, fullName, allRegions).Substring(1);
  262. }
  263. private string GetFName(FactoryRegion region, string fullName, IEnumerable<FactoryRegion> allRegions)
  264. {
  265. var parent = allRegions.FirstOrDefault(c => c.Id == region.ParentId);
  266. if (parent != null)
  267. {
  268. fullName = "/" + parent.FName + fullName;
  269. return this.GetFName(parent, fullName, allRegions);
  270. }
  271. return fullName;
  272. }
  273. public FactoryRegion GetRoot(int regionId)
  274. {
  275. FactoryRegion region = CurrDb.FindEntityFor<FactoryRegion>(regionId);
  276. if (region.ParentId > 0)
  277. {
  278. return this.GetParent(region);
  279. }
  280. return region;
  281. }
  282. private FactoryRegion GetParent(FactoryRegion region)
  283. {
  284. var parentId = region.ParentId;
  285. var parent = CurrDb.FindEntityFor<FactoryRegion>(parentId);
  286. if (parent == null)
  287. return region;
  288. return this.GetParent(parent);
  289. }
  290. public IEnumerable<SelectDto<int?>> GetFactorySelect(ref string errorinfo)
  291. {
  292. var datas = CurrDb.FindListForCondition<FactoryRegion>($" and a.ParentId=0" +
  293. $" and a.IsUse=1", ref errorinfo);
  294. var selects = new List<SelectDto<int?>>();
  295. if (datas != null && datas.Count() > 0)
  296. {
  297. selects = datas.Select(c => new SelectDto<int?>
  298. {
  299. Code = c.FCode,
  300. Label = c.FName,
  301. Value = c.Id
  302. }).ToList();
  303. selects.Insert(0, new SelectDto<int?>
  304. {
  305. Label = "全部",
  306. Value = null
  307. });
  308. }
  309. return selects;
  310. }
  311. public IEnumerable<SelectDto<int?>> GetFactorySelectMulti(ref string errorinfo)
  312. {
  313. var datas = CurrDb.FindListForCondition<FactoryRegion>($" and a.ParentId=0" +
  314. $" and a.IsUse=1", ref errorinfo);
  315. var selects = new List<SelectDto<int?>>();
  316. if (datas != null && datas.Count() > 0)
  317. {
  318. selects = datas.Select(c => new SelectDto<int?>
  319. {
  320. Code = c.FCode,
  321. Label = c.FName,
  322. Value = c.Id
  323. }).ToList();
  324. }
  325. return selects;
  326. }
  327. public IEnumerable<SelectDto<int?>> GetChildrenSelect(int parentId, ref string errorinfo)
  328. {
  329. var sql = $@"select a.id as value,a.fname as label
  330. from factoryregion a
  331. where a.parentID={parentId}";
  332. return CurrDb.FindList<SelectDto<int?>>(sql);
  333. }
  334. /// <summary>
  335. /// 根据选中的楼层车间获取该车间下所有的线体
  336. /// </summary>
  337. /// <param name="factoryId"></param>
  338. /// <param name="errorinfo"></param>
  339. /// <returns></returns>
  340. public IEnumerable<SelectDto<int?>> GetLinesSelect(int[] floorIds)
  341. {
  342. var sql = $@"select a.id as value,CONCAT(b.fname,'/',a.FName) as label
  343. from factoryregion a
  344. left join factoryregion b on a.parentid = b.id
  345. where b.id in ({string.Join(",", floorIds)})";
  346. return CurrDb.FindList<SelectDto<int?>>(sql);
  347. }
  348. /// <summary>
  349. /// 获取机台位置下拉框数据源
  350. /// </summary>
  351. /// <param name="filter"></param>
  352. /// <param name="type">1 园区 2厂房 3楼层 4线体</param>
  353. /// <returns></returns>
  354. public IEnumerable<SelectDto<int?>> GetRegionSelects(string filter, int type = 1)
  355. {
  356. string sql = string.Empty;
  357. switch (type)
  358. {
  359. case 1:
  360. sql = $@"select a.id as value,a.fname as label from factoryregion
  361. where 1=1 {filter}";
  362. break;
  363. case 2:
  364. sql = $@"select CONCAT(b.fname,'/',a.FName) as label,a.id as value
  365. from factoryregion a
  366. left join factoryregion b on a.parentid = b.id
  367. where 1=1 {filter}";
  368. break;
  369. case 3:
  370. sql = $@"select CONCAT(c.fname,'/',b.fname,'/',a.FName) as label,a.id as value
  371. from factoryregion a
  372. left join factoryregion b on a.parentid = b.id
  373. left join factoryregion c on b.parentid = c.id
  374. where 1=1 {filter}";
  375. break;
  376. default:
  377. sql = $@"select a.id as value,a.fname as label from factoryregion
  378. where 1=1 {filter}";
  379. break;
  380. }
  381. return CurrDb.FindList<SelectDto<int?>>(sql).OrderBy(c => c.Label);
  382. }
  383. //public FactoryRegion AddOrUpdate(string region, ref string errorinfo, params string[] parents)
  384. //{
  385. // if (parents == null || parents.Count() == 0)
  386. // {
  387. // var factory = CurrDb.FindListForCondition<FactoryRegion>($" and a.fName='{region}'",
  388. // ref errorinfo).FirstOrDefault();
  389. // if (factory != null)
  390. // return factory;
  391. // var enity = new FactoryRegion { FCode= }
  392. // }
  393. //}
  394. }
  395. }