UnitManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using Sugar.Enties;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Data;
  8. using System.Linq.Expressions;
  9. using WebAPIBase.Utils;
  10. public class UnitManager : DbContext<Unit>
  11. {
  12. //当前类已经继承了 DbContext增、删、查、改的方法
  13. //这里面写的代码不会给覆盖,如果要重新生成请删除 UnitManager.cs
  14. /// <summary>
  15. /// 根据项目ID获取项目下的部门,及部门下面的人员,获取到的是树形结构
  16. /// </summary>
  17. /// <param name="projectid">项目ID</param>
  18. public TreeDTO GetUnitUser(string projectid)
  19. {
  20. //获取项目
  21. var root = UnitDb.GetList(m => m.UnitCode == projectid).FirstOrDefault();
  22. var tree = new TreeDTO();
  23. tree.Id = root.UnitCode;
  24. tree.Name = root.UnitName;
  25. tree.User = false;
  26. tree.Checked = false;
  27. tree.Pid = "-1";
  28. //获取部门
  29. var subList = UnitDb.GetList(m => m.ParentUnitCode == projectid);
  30. var list = new List<TreeDTO>();
  31. foreach (var item in subList)
  32. {
  33. var subTree = new TreeDTO();
  34. subTree.Id = item.UnitCode;
  35. subTree.Name = item.UnitName;
  36. subTree.User = false;
  37. subTree.Checked = false;
  38. subTree.Pid = item.ParentUnitCode;
  39. //获取岗位
  40. var stationList = StationDb.GetList(g => g.UnitCode == item.UnitCode);
  41. var subStationList = new List<TreeDTO>();
  42. foreach (var station in stationList)
  43. {
  44. var subStationTree = new TreeDTO();
  45. subStationTree.Id = station.StationCode;
  46. subStationTree.Name = station.StationName;
  47. subStationTree.User = false;
  48. subStationTree.Checked = false;
  49. subStationTree.Pid = item.UnitCode;
  50. //自用户表和岗位用户关联表中获取用户数据
  51. var queryList = Db.Queryable<SystemUser, StationUser>((su, st) => new object[] { JoinType.Inner, su.UserCode == st.UserCode }).Where((su, st) => st.StationCode == station.StationCode).Select((su, st) => new SubNodeDTO { Id = su.UserCode, Name = su.UserName, Pid = st.StationCode }).ToList();
  52. var userList = new List<TreeDTO>();
  53. foreach (var item1 in queryList)
  54. {
  55. var subUserTree = new TreeDTO();
  56. subUserTree.Id = item1.Id;
  57. subUserTree.Name = item1.Name;
  58. subUserTree.Pid = item1.Pid;
  59. subUserTree.User = true;
  60. subUserTree.Checked = false;
  61. userList.Add(subUserTree);
  62. }
  63. subStationTree.Children = userList;
  64. subStationList.Add(subStationTree);
  65. }
  66. subTree.Children = subStationList;
  67. list.Add(subTree);
  68. }
  69. tree.Children = list;
  70. return tree;
  71. }
  72. /// <summary>
  73. /// 根据项目ID获取项目下的部门,树形结构
  74. /// </summary>
  75. /// <param name="projectid"></param>
  76. /// <returns></returns>
  77. public TreeDTO GetUnit(string projectid)
  78. {
  79. //获取项目
  80. var root = UnitDb.GetList(m => m.UnitCode == projectid).FirstOrDefault();
  81. var tree = new TreeDTO();
  82. tree.Id = root.UnitCode;
  83. tree.Name = root.UnitName;
  84. tree.User = false;
  85. tree.Checked = false;
  86. tree.Pid = "-1";
  87. //获取部门
  88. var subList = UnitDb.GetList(m => m.ParentUnitCode == projectid);
  89. var list = new List<TreeDTO>();
  90. foreach (var item in subList)
  91. {
  92. var subTree = new TreeDTO();
  93. subTree.Id = item.UnitCode;
  94. subTree.Name = item.UnitName;
  95. subTree.User = true;
  96. subTree.Checked = false;
  97. subTree.Pid = item.ParentUnitCode;
  98. list.Add(subTree);
  99. }
  100. tree.Children = list;
  101. return tree;
  102. }
  103. public List<TreeDTO> GetMaterial(string classCode)
  104. {
  105. var returnList = new List<TreeDTO>();
  106. //获取项目
  107. var list = SystemGroupDb.GetList(m => m.ClassCode == classCode && string.IsNullOrEmpty(m.ParentCode));
  108. foreach (var item in list)
  109. {
  110. var tree = new TreeDTO();
  111. tree.Id = item.GroupCode;
  112. tree.Name = item.GroupName;
  113. tree.User = false;
  114. tree.Checked = false;
  115. tree.Pid = "-1";
  116. tree.Children = GetMaterialRecursion(tree, item);
  117. returnList.Add(tree);
  118. }
  119. return returnList;
  120. }
  121. private List<TreeDTO> GetMaterialRecursion(TreeDTO tree, SystemGroup parentItem)
  122. {
  123. var list1 = new List<TreeDTO>();
  124. var subList = SystemGroupDb.GetList(m => m.ParentCode == parentItem.GroupCode);
  125. foreach (var subItem in subList)
  126. {
  127. var subTree = new TreeDTO();
  128. subTree.Id = subItem.GroupCode;
  129. subTree.Name = subItem.GroupName;
  130. subTree.User = true;
  131. subTree.Checked = false;
  132. subTree.Pid = subItem.ParentCode;
  133. subTree.Children = GetMaterialRecursion(subTree, subItem);
  134. list1.Add(subTree);
  135. }
  136. if (subList.Count <= 0)
  137. {
  138. tree.User = true;
  139. tree.Children = null;
  140. }
  141. else
  142. {
  143. tree.Children = list1;
  144. }
  145. return list1;
  146. }
  147. /// <summary>
  148. /// 根据用户代码获取所属部门
  149. /// </summary>
  150. /// <param name="userCode"></param>
  151. /// <returns></returns>
  152. public string GetUserDepartment(string userCode)
  153. {
  154. string sql = $"SELECT stationcode FROM stationuser WHERE usercode='{userCode}'";
  155. var stationcode = Db.Ado.GetString(sql);
  156. if (string.IsNullOrEmpty(stationcode))
  157. {
  158. return "";
  159. }
  160. sql = $"SELECT UnitCode FROM station WHERE stationcode='{stationcode}'";
  161. var unitcode = Db.Ado.GetString(sql);
  162. if (string.IsNullOrEmpty(unitcode))
  163. {
  164. return "";
  165. }
  166. sql = $"SELECT FullCode FROM unit WHERE unitcode='{unitcode}'";
  167. var fullcode = Db.Ado.GetString(sql);
  168. if (string.IsNullOrEmpty(fullcode))
  169. {
  170. return "";
  171. }
  172. var arr = fullcode.Split('-');
  173. var list = Db.Queryable<Unit>().Where(u => arr.Contains(u.UnitCode)).ToList();
  174. var str = "";
  175. foreach (var item in list)
  176. {
  177. str += item.UnitName + "-";
  178. }
  179. if (str.EndsWith("-"))
  180. {
  181. str = str.Substring(0, str.Length - 1);
  182. }
  183. return str;
  184. }
  185. /// <summary>
  186. /// 获取用户所在部门的编码
  187. /// </summary>
  188. /// <param name="userCode"></param>
  189. /// <returns></returns>
  190. public string GetUserDepartmentCode(string userCode)
  191. {
  192. string sql = $"SELECT stationcode FROM stationuser WHERE usercode='{userCode}'";
  193. var stationcode = Db.Ado.GetString(sql);
  194. if (string.IsNullOrEmpty(stationcode))
  195. {
  196. return "";
  197. }
  198. sql = $"SELECT UnitCode FROM station WHERE stationcode='{stationcode}'";
  199. var unitcode = Db.Ado.GetString(sql);
  200. return unitcode;
  201. }
  202. /// <summary>
  203. /// 根据用户号获取岗位信息
  204. /// </summary>
  205. /// <param name="userCode"></param>
  206. /// <returns></returns>
  207. public Station GetUserStation(string userCode)
  208. {
  209. string sql = $"SELECT stationcode FROM stationuser WHERE usercode='{userCode}'";
  210. var stationcode = Db.Ado.GetString(sql);
  211. if (string.IsNullOrEmpty(stationcode))
  212. {
  213. return null;
  214. }
  215. sql = $"SELECT * FROM station WHERE stationcode='{stationcode}'";
  216. var station = Db.SqlQueryable<Station>(sql).First();
  217. return station;
  218. }
  219. /// <summary>
  220. /// 获取项目列表
  221. /// </summary>
  222. /// <returns></returns>
  223. public List<Unit> GetProjectList()
  224. {
  225. var list = UnitDb.GetList(mbox => mbox.UnitType == "项目").ToList();
  226. return list;
  227. }
  228. /// <summary>
  229. /// 根据规则返回系统编码
  230. /// </summary>
  231. /// <param name="codeName"></param>
  232. /// <param name="codeRule"></param>
  233. /// <param name="startNum"></param>
  234. /// <param name="systemCode"></param>
  235. /// <param name="projectCode"></param>
  236. /// <param name="buildingCode"></param>
  237. /// <param name="roomCode"></param>
  238. /// <param name="billDate"></param>
  239. /// <returns></returns>
  240. public string GetNewSysCode(string codeName, string codeRule, int startNum, int systemCode, string projectCode, string buildingCode, string roomCode, DateTime? billDate)
  241. {
  242. string text = codeName;
  243. int length = 0;
  244. string text2 = "";
  245. string text3 = "『number』";
  246. if (!string.IsNullOrEmpty(projectCode))
  247. {
  248. text = codeName + "-" + projectCode;
  249. }
  250. DateTime dateTime = DateTime.Today;
  251. if (billDate.HasValue)
  252. {
  253. dateTime = billDate.Value;
  254. }
  255. StringBuilder stringBuilder = new StringBuilder(codeRule);
  256. int num = 0;
  257. while (num < 100)
  258. {
  259. num++;
  260. int num2 = stringBuilder.ToString().IndexOf("{");
  261. if (num2 < 0)
  262. {
  263. text2 += stringBuilder.ToString();
  264. break;
  265. }
  266. int num3 = stringBuilder.ToString().IndexOf("}", num2);
  267. if (num3 < 0)
  268. {
  269. break;
  270. }
  271. if (num2 > 0)
  272. {
  273. text2 += stringBuilder.ToString(0, num2);
  274. }
  275. string text4 = stringBuilder.ToString(num2 + 1, num3 - num2 - 1);
  276. string text5 = "";
  277. if (text4.StartsWith("#"))
  278. {
  279. length = text4.Length;
  280. text2 += text3;
  281. }
  282. else
  283. {
  284. switch (text4.ToLower())
  285. {
  286. case "y":
  287. text5 = dateTime.Year.ToString();
  288. break;
  289. case "yy":
  290. text5 = dateTime.Year.ToString().PadLeft(2, '0');
  291. break;
  292. case "yyyy":
  293. text5 = dateTime.Year.ToString().PadLeft(4, '0');
  294. break;
  295. case "m":
  296. text5 = dateTime.Month.ToString();
  297. break;
  298. case "mm":
  299. text5 = dateTime.Month.ToString().PadLeft(2, '0');
  300. break;
  301. case "d":
  302. text5 = dateTime.Day.ToString();
  303. break;
  304. case "dd":
  305. text5 = dateTime.Day.ToString().PadLeft(2, '0');
  306. break;
  307. case "p":
  308. text5 = GetProjectId(projectCode, systemCode);
  309. break;
  310. case "b":
  311. text5 = ((!string.IsNullOrEmpty(buildingCode) || string.IsNullOrEmpty(roomCode)) ? GetBuildingName(buildingCode, systemCode) : GetBuildingName(GetBuildingCodeByRoom(roomCode, systemCode), systemCode));
  312. break;
  313. case "r":
  314. text5 = GetRoomName(roomCode, systemCode);
  315. break;
  316. }
  317. if (text5 != "")
  318. {
  319. text = text + "-" + text5;
  320. text2 += text5;
  321. }
  322. }
  323. stringBuilder.Remove(0, num3 + 1);
  324. }
  325. string sysCodeFromDatabase = GetSysCodeFromDatabase(text, systemCode, startNum, length);
  326. if (text2 == "")
  327. {
  328. text2 = text3;
  329. }
  330. string text6 = text2.Replace(text3, sysCodeFromDatabase);
  331. if (text6.Length > 50)
  332. {
  333. throw new ApplicationException("自动生成的编号长度超出50位,请检查编号规则");
  334. }
  335. return text6;
  336. }
  337. public string GetNewSysCode(string codeName, string codeRule, int startNum, int systemCode, string projectCode, string buildingCode, string roomCode)
  338. {
  339. return GetNewSysCode(codeName, codeRule, startNum, systemCode, projectCode, buildingCode, roomCode, null);
  340. }
  341. public string GetNewSysCode(string codeName, string codeRule, int startNum, int systemCode, string projectCode)
  342. {
  343. return GetNewSysCode(codeName, codeRule, startNum, systemCode, projectCode, "", "");
  344. }
  345. private string GetProjectId(string projectCode, int systemCode)
  346. {
  347. switch (systemCode)
  348. {
  349. case 0:
  350. return GetPmProjectId(projectCode);
  351. case 1:
  352. return GetSaProjectId(projectCode);
  353. case 3:
  354. return GetPrManagementId(projectCode);
  355. default:
  356. return "";
  357. }
  358. }
  359. private string GetPmProjectId(string projectCode)
  360. {
  361. if (!string.IsNullOrEmpty(projectCode))
  362. {
  363. string commandText = "select ProjectId from project where ProjectCode = @projectCode";
  364. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  365. new SugarParameter("@projectCode", projectCode)
  366. });
  367. if (dataTable.Rows.Count > 0)
  368. {
  369. return string.Concat(dataTable.Rows[0][0], "");
  370. }
  371. }
  372. return "";
  373. }
  374. private string GetSaProjectId(string projectCode)
  375. {
  376. if (!string.IsNullOrEmpty(projectCode))
  377. {
  378. string commandText = "select pCode from SaProject where pId = @projectCode";
  379. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  380. new SugarParameter("@projectCode", projectCode)
  381. });
  382. if (dataTable.Rows.Count > 0)
  383. {
  384. return string.Concat(dataTable.Rows[0][0], "");
  385. }
  386. }
  387. return "";
  388. }
  389. private string GetPrManagementId(string projectCode)
  390. {
  391. if (!string.IsNullOrEmpty(projectCode))
  392. {
  393. string commandText = "select mCode from PrManagement where mId = @projectCode";
  394. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  395. new SugarParameter("@projectCode", projectCode)
  396. });
  397. if (dataTable.Rows.Count > 0)
  398. {
  399. return string.Concat(dataTable.Rows[0][0], "");
  400. }
  401. }
  402. return "";
  403. }
  404. private string GetBuildingName(string buildingCode, int systemCode)
  405. {
  406. if (systemCode == 1)
  407. {
  408. return GetSaBuildingName(buildingCode);
  409. }
  410. return "";
  411. }
  412. private string GetSaBuildingName(string buildingCode)
  413. {
  414. if (!string.IsNullOrEmpty(buildingCode))
  415. {
  416. string commandText = "select bName from SaBuilding where bId = @buildingCode";
  417. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  418. new SugarParameter("@buildingCode", buildingCode)
  419. });
  420. if (dataTable.Rows.Count > 0)
  421. {
  422. return string.Concat(dataTable.Rows[0][0], "");
  423. }
  424. }
  425. return "";
  426. }
  427. private string GetBuildingCodeByRoom(string roomCode, int systemCode)
  428. {
  429. if (systemCode == 1)
  430. {
  431. return GetSaBuildingCodeByRoom(roomCode);
  432. }
  433. return "";
  434. }
  435. private string GetSaBuildingCodeByRoom(string roomCode)
  436. {
  437. if (!string.IsNullOrEmpty(roomCode))
  438. {
  439. string commandText = string.Format("select buildingId from SaRoom where rId = {0}", "@roomCode");
  440. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  441. new SugarParameter("@roomCode", roomCode)
  442. });
  443. if (dataTable.Rows.Count > 0)
  444. {
  445. return string.Concat(dataTable.Rows[0][0], "");
  446. }
  447. }
  448. return "";
  449. }
  450. private string GetRoomName(string roomCode, int systemCode)
  451. {
  452. if (systemCode == 1)
  453. {
  454. return GetSaRoomName(roomCode);
  455. }
  456. return "";
  457. }
  458. private string GetSaRoomName(string roomCode)
  459. {
  460. if (!string.IsNullOrEmpty(roomCode))
  461. {
  462. string commandText = string.Format("select rFullName from SaRoom where rId = {0}", "@roomCode");
  463. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  464. new SugarParameter("@roomCode", roomCode)
  465. });
  466. if (dataTable.Rows.Count > 0)
  467. {
  468. return string.Concat(dataTable.Rows[0][0], "");
  469. }
  470. }
  471. return "";
  472. }
  473. private string GetSysCodeFromDatabase(string codeName, int systemCode, int startNum, int length)
  474. {
  475. int num = 0;
  476. string commandText = "select CodeValue from SysCode where CodeName = @codeName and SystemName = @systemCode";
  477. DataTable dataTable = Db.Ado.GetDataTable(commandText, new List<SugarParameter>() {
  478. new SugarParameter("@codeName", codeName),
  479. new SugarParameter("@systemCode", systemCode)
  480. });
  481. if (dataTable.Rows.Count > 0)
  482. {
  483. try
  484. {
  485. num = int.Parse(string.Concat(dataTable.Rows[0]["CodeValue"], ""));
  486. }
  487. catch
  488. {
  489. throw new Exception(string.Format("自动编号SysCode“{0}”不是有效的数值", dataTable.Rows[0]["CodeValue"]));
  490. }
  491. }
  492. num++;
  493. if (num < startNum)
  494. {
  495. num = startNum;
  496. }
  497. commandText = ((dataTable.Rows.Count <= 0) ? "insert into SysCode (CodeName, CodeValue, SystemName) values (@codeName, @num, @systemCode)" : "update SysCode set CodeValue = @num where CodeName = @codeName and SystemName = @systemCode");
  498. Db.Ado.ExecuteCommand(commandText, new List<SugarParameter>() {
  499. new SugarParameter("@codeName", codeName),
  500. new SugarParameter("@num", num),
  501. new SugarParameter("@systemCode", systemCode)
  502. });
  503. string text = num.ToString();
  504. if (length > 0)
  505. {
  506. text = text.PadLeft(length, '0');
  507. }
  508. return text;
  509. }
  510. /// <summary>
  511. /// 根据项目代码获取材料合同
  512. /// </summary>
  513. /// <param name="projectcode"></param>
  514. /// <param name="searchValue"></param>
  515. /// <returns></returns>
  516. public List<Contract> GetContracts(string projectcode, string searchValue)
  517. {
  518. var sql = $"SELECT * FROM dbo.Contract WHERE ProjectCode='{projectcode}' AND contracttype='材料合同' AND Status IN (0,2)";
  519. if (!string.IsNullOrEmpty(searchValue))
  520. {
  521. sql += $" and (ContractID like '%{searchValue}%' or ContractName like '%{searchValue}%')";
  522. }
  523. var list = Db.Ado.SqlQuery<Contract>(sql);
  524. return list;
  525. }
  526. /// <summary>
  527. /// 材料合同或工程合同列表选择,供入库单和领料单添加或修改使用
  528. /// </summary>
  529. /// <param name="lamda"></param>
  530. /// <returns></returns>
  531. public List<Contract> GetContracts(Expression<Func<Contract, bool>> lamda)
  532. {
  533. var list = Db.Queryable<Contract>().Where(lamda).OrderBy("ContractID DESC").ToList();
  534. return list;
  535. }
  536. /// <summary>
  537. /// 根据cbs费用项表中的费用编码,获取该费用各级费用编码的全编码
  538. /// </summary>
  539. /// <param name="costCode"></param>
  540. /// <returns></returns>
  541. public string GetCBSFullCode(string costCode)
  542. {
  543. var sql = $"SELECT dbo.GetCBSFullCode('{costCode}')";
  544. var fullCode = Db.Ado.GetString(sql);
  545. return fullCode;
  546. }
  547. /// <summary>
  548. /// 根据费用代码获取该费用的全名称
  549. /// </summary>
  550. /// <param name="code"></param>
  551. /// <returns></returns>
  552. public string GetCostFullName(string code)
  553. {
  554. string text = "";
  555. if (code.IsNullOrEmpty())
  556. {
  557. return text;
  558. }
  559. string cBSFullCode = GetCBSFullCode(code);
  560. string[] array = cBSFullCode.Split('-');
  561. int num = 0;
  562. for (int i = num; i < array.Length; i++)
  563. {
  564. if (text != "")
  565. {
  566. text += "->";
  567. }
  568. text += GetCostName(array[i]);
  569. }
  570. return text;
  571. }
  572. /// <summary>
  573. /// 根据费用代码获取费用名称
  574. /// </summary>
  575. /// <param name="code"></param>
  576. /// <returns></returns>
  577. public string GetCostName(string code)
  578. {
  579. string result = "";
  580. if (code.IsNullOrEmpty())
  581. {
  582. return result;
  583. }
  584. var sql = $"SELECT CostName FROM dbo.cbs WHERE CostCode='{code}'";
  585. result = Db.Ado.GetString(sql);
  586. return result;
  587. }
  588. }