You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
20 KiB

  1. namespace CounsellorBL.BLStructure.SYS
  2. {
  3. using CounsellorBL.Helper;
  4. using MonumentDefine;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using OT.COM.ArsenalDB;
  8. using OT.COM.LogisticsUtil;
  9. using OT.COM.SignalerMessage;
  10. using SoldierData.EnterprizeV4;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. class RoleManageService : SingleDataTableTemplate<tb_sys_role>
  15. {
  16. public RoleManageService() : base()
  17. {
  18. dgReadCommandGenerator = readCommandGenerator;
  19. dgCreateCommandGenerator = createCommandGenerator;
  20. dgDeleteCommandGenerator = deleteCommandGenerator;
  21. dgUpdateCommandGenerator = updateCommandGenerator;
  22. }
  23. protected string readCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_c,
  24. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  25. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  26. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  27. {
  28. string sMsg;
  29. Command cRes = null;
  30. try
  31. {
  32. do
  33. {
  34. List<string> lsColumns = EntityBase.GetAllColumnName(typeof(tb_sys_role));
  35. // 取得condition
  36. Dictionary<string, string> dicCondition = GetQueryMasterFirstQJEDicwherecols(i_crmInput);
  37. var lsGroup = ProjectHelper.GetUserGroup(i_crmInput);
  38. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  39. QueryJsonElement qjeRole = lBlocks.GetInst();
  40. qjeRole.table = tb_sys_role.TABLENAME;
  41. qjeRole.displaycols = lsColumns;
  42. qjeRole.wherecols = new WhereNode(tb_sys_role.CN_GROUP_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_grp_group), lsGroup.ToArray());
  43. if (dicCondition != null && dicCondition.Any())
  44. {
  45. qjeRole.dicwherecols = dicCondition;
  46. }
  47. lBlocks.Add(qjeRole);
  48. sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRead);
  49. if (sMsg != null)
  50. {
  51. break;
  52. }
  53. cRes = cRead;
  54. }
  55. while (false);
  56. }
  57. catch (Exception ex)
  58. {
  59. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  60. sMsg = $"{nameof(readCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  61. #if DEBUG
  62. System.Diagnostics.Debug.WriteLine(sMsg);
  63. #endif
  64. }
  65. o_c = cRes;
  66. return sMsg;
  67. }
  68. protected string createCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
  69. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  70. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  71. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  72. {
  73. string sMsg = null;
  74. List<Command> lCmds = new List<Command>();
  75. try
  76. {
  77. do
  78. {
  79. foreach (JToken joData in i_jaData)
  80. {
  81. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  82. JObject jdata = dicData[BLWording.DATA] as JObject;
  83. Dictionary<string, object> dicInput = jdata.ToObject<Dictionary<string, object>>();
  84. //取得新增的帳號uid
  85. string roleid = Guid.NewGuid().ToString();
  86. tb_sys_role cInsert = new tb_sys_role()
  87. {
  88. uid = roleid,
  89. group_uid = dicInput[tb_sys_role.CN_GROUP_UID]?.ToString(),
  90. code = (dicInput[tb_sys_role.CN_CODE] ?? "").ToString(),
  91. name = dicInput[tb_sys_role.CN_NAME].ToString(),
  92. memo = (dicInput[tb_sys_role.CN_MEMO] ?? "").ToString(),
  93. remark = (dicInput[tb_sys_role.CN_REMARK] ?? "").ToString(),
  94. };
  95. Command c = Command.SetupInsertCmd(cInsert);
  96. lCmds.Add(c);
  97. tb_sys_role2org cData = new tb_sys_role2org()
  98. {
  99. org_uid = i_sSessionUser.update_org_uid,
  100. role_uid = roleid,
  101. };
  102. Command d = Command.SetupInsertCmd(cData);
  103. lCmds.Add(d);
  104. //if (dicInput.ContainsKey(BLWording.UID))
  105. //{
  106. // string[] lsRole = dicInput[BLWording.UID].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  107. // lsRole = lsRole.Distinct().ToArray();
  108. // foreach (var role in lsRole)
  109. // {
  110. // tb_sys_user2role cNew = new tb_sys_user2role()
  111. // {
  112. // user_uid = role,
  113. // role_uid = roleid
  114. // };
  115. // lCmds.Add(Command.SetupInsertCmd(cNew));
  116. // }
  117. //}
  118. }
  119. }
  120. while (false);
  121. }
  122. catch (Exception ex)
  123. {
  124. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  125. sMsg = $"{nameof(createCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  126. #if DEBUG
  127. System.Diagnostics.Debug.WriteLine(sMsg);
  128. #endif
  129. }
  130. o_lResult = lCmds;
  131. return sMsg;
  132. }
  133. protected string deleteCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
  134. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  135. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  136. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  137. {
  138. string sMsg = null;
  139. List<Command> lCmds = new List<Command>();
  140. try
  141. {
  142. do
  143. {
  144. foreach (JToken joData in i_jaData)
  145. {
  146. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  147. Dictionary<string, object> wheredataDic;
  148. string role_id = null;
  149. if (dicData.ContainsKey(BLWording.WHEREDATA) && dicData[BLWording.WHEREDATA] is JObject wheredata)
  150. {
  151. wheredataDic = wheredata.ToObject<Dictionary<string, object>>();
  152. if (wheredataDic.ContainsKey(BLWording.UID))
  153. {
  154. role_id = wheredataDic[BLWording.UID].ToString();
  155. }
  156. }
  157. tb_sys_user2role ur = new tb_sys_user2role()
  158. {
  159. role_uid = role_id
  160. };
  161. lCmds.Add(Command.SetupDeleteCmd(ur));
  162. tb_sys_role2org r = new tb_sys_role2org()
  163. {
  164. role_uid = role_id
  165. };
  166. lCmds.Add(Command.SetupDeleteCmd(r));
  167. tb_sys_role d = new tb_sys_role()
  168. {
  169. uid = role_id
  170. };
  171. lCmds.Add(Command.SetupDeleteCmd(d));
  172. }
  173. }
  174. while (false);
  175. }
  176. catch (Exception ex)
  177. {
  178. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  179. sMsg = $"{nameof(deleteCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  180. #if DEBUG
  181. System.Diagnostics.Debug.WriteLine(sMsg);
  182. #endif
  183. }
  184. o_lResult = lCmds;
  185. return sMsg;
  186. }
  187. protected string updateCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
  188. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  189. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  190. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  191. {
  192. string sMsg;
  193. List<Command> lCmds = new List<Command>();
  194. try
  195. {
  196. do
  197. {
  198. string role_id = null;
  199. foreach (JToken joData in i_jaData)
  200. {
  201. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  202. //取得user_uid
  203. if (dicData.ContainsKey(BLWording.WHEREDATA))
  204. {
  205. JObject wheredata = dicData[BLWording.WHEREDATA] as JObject;
  206. Dictionary<string, object> wheredataDic = wheredata.ToObject<Dictionary<string, object>>();
  207. if (wheredataDic.ContainsKey(BLWording.UID))
  208. {
  209. role_id = wheredataDic[BLWording.UID].ToString();
  210. }
  211. }
  212. tb_sys_role u = new tb_sys_role();
  213. sMsg = valueAssignment(i_crmInput, u, dicData[BLWording.DATA] as JObject, out List<Command> lcAddInsert, i_bIsCreate: false);
  214. if (sMsg != null)
  215. {
  216. break;
  217. }
  218. Command c = Command.SetupUpdateCmd(u, new WhereNode(tb_sys_role.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_role), role_id));
  219. lCmds.Add(c);
  220. tb_sys_role2org udro = new tb_sys_role2org() { };
  221. Command udr = Command.SetupUpdateCmd(udro, new WhereNode(tb_sys_role2org.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_role2org), role_id));
  222. lCmds.Add(udr);
  223. }
  224. // 查詢原始資料
  225. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  226. QueryJsonElement qjeOrigin = lBlocks.GetInst();
  227. qjeOrigin.table = tb_sys_user2role.TABLENAME;
  228. qjeOrigin.displaycols = new List<string>() { tb_sys_user2role.CN_USER_UID };
  229. qjeOrigin.wherecols = new WhereNode(tb_sys_user2role.CN_ROLE_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user2role), role_id);
  230. lBlocks.Add(qjeOrigin);
  231. sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes);
  232. if (sMsg != null)
  233. {
  234. break;
  235. }
  236. }
  237. while (false);
  238. }
  239. catch (Exception ex)
  240. {
  241. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  242. sMsg = $"{nameof(updateCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  243. #if DEBUG
  244. System.Diagnostics.Debug.WriteLine(sMsg);
  245. #endif
  246. }
  247. o_lResult = lCmds;
  248. return sMsg;
  249. }
  250. protected string getUserNameHandler(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_cCmd,
  251. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  252. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  253. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  254. {
  255. Command cRes = null;
  256. string sMsg;
  257. try
  258. {
  259. do
  260. {
  261. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  262. QueryJsonElement qjeUser = lBlocks.GetInst();
  263. qjeUser.table = tb_sys_user.TABLENAME;
  264. qjeUser.displaycols = new List<string>() { tb_sys_user.CN_ACCOUNT, tb_sys_user.CN_UID };
  265. QueryJsonElement qjeEmp = lBlocks.GetInst();
  266. qjeEmp.table = tb_hr_employee.TABLENAME;
  267. qjeEmp.jointype = QueryJsonElement.LEFT_JOIN;
  268. qjeEmp.jointable = qjeUser;
  269. qjeEmp.joincols = new Dictionary<string, string>() {
  270. { tb_hr_employee.CN_UID,tb_sys_user.CN_UID }};
  271. qjeEmp.displaycols = new List<string>() { tb_hr_employee.CN_NAME };
  272. lBlocks.Add(qjeUser);
  273. lBlocks.Add(qjeEmp);
  274. sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes);
  275. }
  276. while (false);
  277. }
  278. catch (Exception ex)
  279. {
  280. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  281. sMsg = $"{nameof(getUserNameHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  282. #if DEBUG
  283. System.Diagnostics.Debug.WriteLine(sMsg);
  284. #endif
  285. }
  286. o_cCmd = cRes;
  287. return sMsg;
  288. }
  289. /// <summary>
  290. /// 所有人員的資料
  291. /// </summary>
  292. /// <param name="i_crmInput"></param>
  293. /// <returns></returns>
  294. public CResponseMessage GetUserName(CRequestMessage i_crmInput)
  295. {
  296. return simpleQuery(i_crmInput, null, getUserNameHandler);
  297. }
  298. protected string getRole2UserCommand(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_c,
  299. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  300. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  301. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  302. {
  303. Command cRes = null;
  304. string sMsg;
  305. try
  306. {
  307. do
  308. {
  309. string role_id = null;
  310. object i_dicData = i_crmInput.param[BLWording.QRY_MASTER];
  311. if (i_dicData is JArray)
  312. {
  313. JArray jaData = i_dicData as JArray;
  314. foreach (JToken joData in jaData)
  315. {
  316. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  317. role_id = dicData[BLWording.ROLE_ID].ToString();
  318. }
  319. }
  320. // 查詢原始資料
  321. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  322. QueryJsonElement qjeOrigin = lBlocks.GetInst();
  323. qjeOrigin.table = tb_sys_user2role.TABLENAME;
  324. qjeOrigin.displaycols = new List<string>() { tb_sys_user2role.CN_USER_UID };
  325. qjeOrigin.aliascols = new Dictionary<string, List<string>>()
  326. { {tb_sys_user2role.CN_USER_UID, new List<string>(){ tb_sys_user.CN_UID } } };
  327. qjeOrigin.wherecols = new WhereNode(tb_sys_user2role.CN_ROLE_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user2role), role_id);
  328. lBlocks.Add(qjeOrigin);
  329. QueryJsonElement qjeRole = lBlocks.GetInst();
  330. qjeRole.table = tb_sys_user.TABLENAME;
  331. qjeRole.displaycols = new List<string>() { tb_sys_user.CN_ACCOUNT };
  332. qjeRole.jointype = QueryJsonElement.LEFT_JOIN;
  333. qjeRole.jointable = qjeOrigin;
  334. qjeRole.joincols = new Dictionary<string, string>() {
  335. { tb_sys_user.CN_UID, tb_sys_user2role.CN_USER_UID}
  336. };
  337. lBlocks.Add(qjeRole);
  338. sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes);
  339. }
  340. while (false);
  341. }
  342. catch (Exception ex)
  343. {
  344. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  345. sMsg = $"{nameof(getRole2UserCommand)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  346. #if DEBUG
  347. System.Diagnostics.Debug.WriteLine(sMsg);
  348. #endif
  349. }
  350. o_c = cRes;
  351. return sMsg;
  352. }
  353. protected string getRole2UserPostHandleData(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault,
  354. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  355. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  356. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  357. {
  358. string sMsg = null;
  359. object oResultData = null;
  360. try
  361. {
  362. do
  363. {
  364. List<tb_sys_user> qdRole = i_aiArsenal.RunQueryList<tb_sys_user>(i_cCmd);
  365. Dictionary<object, string> dicResultData = new Dictionary<object, string>();
  366. if (qdRole.Count > 0)
  367. {
  368. foreach (var role in qdRole)
  369. {
  370. dicResultData.Add(role.account, role.uid);
  371. }
  372. }
  373. oResultData = dicResultData;
  374. }
  375. while (false);
  376. }
  377. catch (Exception ex)
  378. {
  379. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  380. sMsg = $"{nameof(getRole2UserPostHandleData)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  381. #if DEBUG
  382. System.Diagnostics.Debug.WriteLine(sMsg);
  383. #endif
  384. }
  385. o_oReault = oResultData;
  386. return sMsg;
  387. }
  388. public CResponseMessage GetRole2User(CRequestMessage i_crmInput)
  389. {
  390. return simpleQuery(i_crmInput, null, getRole2UserCommand, getRole2UserPostHandleData);
  391. }
  392. }
  393. }