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.

894 lines
40 KiB

  1. 
  2. using CounsellorBL.Common;
  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;
  11. using SoldierData.EnterprizeV4;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Linq;
  16. namespace CounsellorBL.BLStructure.SYS
  17. {
  18. class MemberService : SingleDataTableTemplate<tb_sys_user>
  19. {
  20. const string calcolor = "calcolor";
  21. const string role_uid = "role_uid";
  22. public MemberService()
  23. {
  24. dgReadCommandGenerator = readCommandGenerator;
  25. dgReadCommandPostDataHandler = readCommandPostDataHandler;
  26. dgCreateCommandGenerator = createCommandGenerator;
  27. dgDeleteCommandGenerator = deleteCommandGenerator;
  28. dgUpdateCommandGenerator = updateCommandGenerator;
  29. }
  30. [Auth(false)]
  31. public new CResponseMessage Read(CRequestMessage i_crmInput) => base.Read(i_crmInput);
  32. protected string readCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_c,
  33. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  34. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  35. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  36. {
  37. string sMsg;
  38. Command cRes = null;
  39. try
  40. {
  41. do
  42. {
  43. List<string> lsEmpColumns = EntityBase.GetAllColumnName(typeof(tb_hr_employee));
  44. Dictionary<string, object> dicWhereData = i_jaData[0][BLWording.WHEREDATA].ToObject<Dictionary<string, object>>();
  45. // 取得condition
  46. Dictionary<string, string> dicCondition = GetQueryMasterFirstQJEDicwherecols(i_crmInput);
  47. var lsGroup = ProjectHelper.GetUserGroup(i_crmInput);
  48. /**開始組指令**/
  49. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  50. QueryJsonElement qjeUser = lBlocks.GetInst();
  51. qjeUser.table = tb_sys_user.TABLENAME;
  52. qjeUser.displaycols = new List<string>() { tb_sys_user.CN_ACCOUNT, tb_sys_user.CN_UID };
  53. List<WhereNode> wnQuery = new List<WhereNode>();
  54. if (dicCondition.ContainsKey(tb_sys_user.CN_STATUS_FLAG))
  55. {
  56. wnQuery.Add(new WhereNode(tb_sys_user.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user), dicCondition[tb_sys_user.CN_STATUS_FLAG]));
  57. }
  58. if (dicCondition.ContainsKey(tb_sys_user.CN_ACCOUNT))
  59. {
  60. wnQuery.Add(new WhereNode(tb_sys_user.CN_ACCOUNT, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user), dicCondition[tb_sys_user.CN_ACCOUNT]));
  61. }
  62. if (wnQuery.Any())
  63. {
  64. qjeUser.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, wnQuery.ToArray());
  65. }
  66. QueryJsonElement qjeEmp = lBlocks.GetInst();
  67. qjeEmp.table = tb_hr_employee.TABLENAME;
  68. qjeEmp.jointype = QueryJsonElement.LEFT_JOIN;
  69. qjeEmp.jointable = qjeUser;
  70. qjeEmp.joincols = new Dictionary<string, string>() {
  71. { tb_hr_employee.CN_UID,tb_sys_user.CN_UID }};
  72. qjeEmp.displaycols = lsEmpColumns;
  73. qjeEmp.dicwherecols = dicCondition;
  74. QueryJsonElement qjeEnterCode = lBlocks.GetInst();
  75. qjeEnterCode.table = tb_sys_user2entercode.TABLENAME;
  76. qjeEnterCode.jointype = QueryJsonElement.LEFT_JOIN;
  77. qjeEnterCode.jointable = qjeUser;
  78. qjeEnterCode.joincols = new Dictionary<string, string>() {
  79. { tb_sys_user2entercode.CN_USER_UID, tb_sys_user.CN_UID }};
  80. qjeEnterCode.displaycols = new List<string>() { tb_sys_user2entercode.CN_USER_ENTERCODE };
  81. qjeEnterCode.aliascols = new Dictionary<string, List<string>>() {
  82. { tb_sys_user2entercode.CN_USER_ENTERCODE, new List<string>(){ "confirm_entercode" }
  83. } };
  84. lBlocks.Add(qjeUser);
  85. lBlocks.Add(qjeEmp);
  86. lBlocks.Add(qjeEnterCode);
  87. QueryJsonElement qje2Role = lBlocks.GetInst();
  88. qje2Role.table = tb_sys_user2role.TABLENAME;
  89. qje2Role.jointable = qjeUser;
  90. qje2Role.jointype = QueryJsonElement.LEFT_JOIN;
  91. qje2Role.joincols = new Dictionary<string, string>() {
  92. { tb_sys_user2role.CN_USER_UID, tb_sys_user.CN_UID }};
  93. qje2Role.displaycols = new List<string>() { tb_sys_user2role.CN_ROLE_UID };
  94. if (dicWhereData != null && dicWhereData.ContainsKey(role_uid))
  95. {
  96. qje2Role.wherecols = new WhereNode(tb_sys_user2role.CN_ROLE_UID,
  97. WhereNode.EColumnOperation.EOT_EQ,
  98. typeof(tb_sys_user2role),
  99. dicWhereData[role_uid].ToString()
  100. );
  101. }
  102. lBlocks.Add(qje2Role);
  103. QueryJsonElement qjeEmp2Branch = lBlocks.GetInst();
  104. qjeEmp2Branch.table = tb_hr_employee2branch.TABLENAME;
  105. qjeEmp2Branch.jointable = qjeEmp;
  106. qjeEmp2Branch.jointype = QueryJsonElement.LEFT_JOIN;
  107. qjeEmp2Branch.joincols = new Dictionary<string, string>() {
  108. { tb_hr_employee2branch.CN_UID, tb_hr_employee.CN_UID }};
  109. qjeEmp2Branch.displaycols = new List<string>() { tb_hr_employee2branch.CN_BRANCH_UID };
  110. lBlocks.Add(qjeEmp2Branch);
  111. QueryJsonElement qjeBranch = lBlocks.GetInst();
  112. qjeBranch.table = tb_grp_branch.TABLENAME;
  113. qjeBranch.jointable = qjeEmp2Branch;
  114. qjeBranch.jointype = QueryJsonElement.LEFT_JOIN;
  115. qjeBranch.joincols = new Dictionary<string, string>() {
  116. { tb_grp_branch.CN_UID, tb_hr_employee2branch.CN_BRANCH_UID }};
  117. qjeBranch.displaycols = new List<string>() { tb_grp_branch.CN_GROUP_UID };
  118. lBlocks.Add(qjeBranch);
  119. QueryJsonElement qjeGroup = lBlocks.GetInst();
  120. qjeGroup.table = tb_grp_group.TABLENAME;
  121. qjeGroup.jointable = qjeBranch;
  122. qjeGroup.jointype = QueryJsonElement.LEFT_JOIN;
  123. qjeGroup.joincols = new Dictionary<string, string>() {
  124. { tb_grp_group.CN_UID, tb_grp_branch.CN_GROUP_UID }};
  125. if (i_crmInput.token != "guest")
  126. {
  127. qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_grp_group), lsGroup.ToArray());
  128. }
  129. lBlocks.Add(qjeGroup);
  130. sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRead);
  131. if (sMsg != null)
  132. {
  133. break;
  134. }
  135. cRes = cRead;
  136. }
  137. while (false);
  138. }
  139. catch (Exception ex)
  140. {
  141. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  142. sMsg = $"{nameof(readCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  143. #if DEBUG
  144. System.Diagnostics.Debug.WriteLine(sMsg);
  145. #endif
  146. }
  147. o_c = cRes;
  148. return sMsg;
  149. }
  150. class MemberModel : tb_hr_employee
  151. {
  152. public string account { get; set; }
  153. public string user_entercode { get; set; }
  154. public string role_uid { get; set; }
  155. public string group_uid { get; set; }
  156. public string branch_uid { get; set; }
  157. public string confirm_entercode { get; set; }
  158. }
  159. protected string readCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault,
  160. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  161. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  162. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  163. {
  164. string sMsg = null;
  165. object oResultData = null;
  166. try
  167. {
  168. do
  169. {
  170. List<MemberModel> qds = i_aiArsenal.RunQueryList<MemberModel>(i_cCmd);
  171. if (!i_cCmd.IsSuccess)
  172. {
  173. sMsg = i_cCmd.LastErrorCode;
  174. break;
  175. }
  176. oResultData = qds
  177. .GroupBy(x => new
  178. {
  179. x.account,
  180. x.uid,
  181. x.user_entercode,
  182. x.email,
  183. x.status_flag,
  184. x.name,
  185. x.confirm_entercode,
  186. x.personal_setting,
  187. x.receive_branch_uid
  188. })
  189. .Select(x => new
  190. {
  191. x.Key.account,
  192. x.Key.name,
  193. x.Key.uid,
  194. x.Key.user_entercode,
  195. x.Key.confirm_entercode,
  196. x.Key.email,
  197. x.Key.status_flag,
  198. x.Key.personal_setting,
  199. x.Key.receive_branch_uid,
  200. role_uid = x.Where(x => !string.IsNullOrEmpty(x.role_uid)).Select(x => x.role_uid).Distinct().ToList(),
  201. group_uid = x.Where(x => !string.IsNullOrEmpty(x.group_uid)).Select(x => x.group_uid).ToList(),
  202. branch_uid = x.Where(x => !string.IsNullOrEmpty(x.branch_uid)).Select(x => x.branch_uid).ToList()
  203. }).ToList();
  204. }
  205. while (false);
  206. }
  207. catch (Exception ex)
  208. {
  209. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  210. sMsg = $"{nameof(readCommandPostDataHandler)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  211. #if DEBUG
  212. System.Diagnostics.Debug.WriteLine(sMsg);
  213. #endif
  214. }
  215. o_oReault = oResultData;
  216. return sMsg;
  217. }
  218. protected string createCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
  219. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  220. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  221. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  222. {
  223. string sMsg;
  224. List<Command> lCmds = new List<Command>();
  225. try
  226. {
  227. do
  228. {
  229. Dictionary<string, object> dicData = i_jaData[0][BLWording.DATA].ToObject<Dictionary<string, object>>();
  230. dicData.Remove(tb_hr_employee.CN_UID);
  231. string sUserUID = Guid.NewGuid().ToString();
  232. // 新增帳號
  233. tb_sys_user cNewUser = new tb_sys_user
  234. {
  235. uid = sUserUID,
  236. account = dicData[tb_sys_user.CN_ACCOUNT].ToString()
  237. };
  238. lCmds.Add(Command.SetupInsertCmd(cNewUser));
  239. // 新增個人資訊
  240. tb_hr_employee cNewEmp = new tb_hr_employee();
  241. cNewEmp.FillData(dicData);
  242. cNewEmp.uid = sUserUID;
  243. PersonalSetting personalSetting = new PersonalSetting();
  244. if (dicData.ContainsKey(calcolor))
  245. {
  246. personalSetting.CalColor = dicData[calcolor].ToString();
  247. }
  248. personalSetting.Favorite = new List<string>() { "0d5bcacf-0958-443d-aab4-0f207327cce7", "70c34b69-56fe-499c-ada6-967457dd4026", "2255ae6a-e1cb-4e01-a225-e97a7c73c1d5", "bbd2d114-556b-4663-99bb-c4f7a2698077", "6e03e650-8a35-47e7-8144-2bb4334ab7a9" };
  249. cNewEmp.personal_setting = JsonConvert.SerializeObject(personalSetting); // 將物件序列化
  250. lCmds.Add(Command.SetupInsertCmd(cNewEmp));
  251. sMsg = EncryptHelper.Encrypt(dicData[tb_sys_user2entercode.CN_USER_ENTERCODE].ToString(), out string sEnc);
  252. if (sMsg != null)
  253. {
  254. break;
  255. }
  256. // 新增密碼
  257. tb_sys_user2entercode cNewEnterCode = new tb_sys_user2entercode
  258. {
  259. user_uid = sUserUID,
  260. user_entercode = sEnc
  261. };
  262. lCmds.Add(Command.SetupInsertCmd(cNewEnterCode));
  263. // 新增角色資訊
  264. if (dicData.ContainsKey(role_uid))
  265. {
  266. var jaRole = dicData[role_uid] as JArray;
  267. if (jaRole != null)
  268. {
  269. foreach (var role in jaRole)
  270. {
  271. tb_sys_user2role cNew = new tb_sys_user2role()
  272. {
  273. user_uid = sUserUID,
  274. role_uid = role.ToString()
  275. };
  276. lCmds.Add(Command.SetupInsertCmd(cNew));
  277. }
  278. }
  279. }
  280. // 新增所屬分店
  281. if (dicData.ContainsKey(tb_hr_employee2branch.CN_BRANCH_UID))
  282. {
  283. var jaBranch = dicData[tb_hr_employee2branch.CN_BRANCH_UID] as JArray;
  284. foreach (var branch in jaBranch)
  285. {
  286. tb_hr_employee2branch cNew = new tb_hr_employee2branch()
  287. {
  288. uid = sUserUID,
  289. branch_uid = branch.ToString()
  290. };
  291. lCmds.Add(Command.SetupInsertCmd(cNew));
  292. }
  293. }
  294. }
  295. while (false);
  296. }
  297. catch (Exception ex)
  298. {
  299. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  300. sMsg = $"{nameof(createCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  301. #if DEBUG
  302. System.Diagnostics.Debug.WriteLine(sMsg);
  303. #endif
  304. }
  305. o_lResult = lCmds;
  306. return sMsg;
  307. }
  308. protected string updateCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lResult, List<string> i_saQryContainKeys,
  309. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  310. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  311. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  312. {
  313. string sMsg = null;
  314. List<Command> lCmds = new List<Command>();
  315. try
  316. {
  317. do
  318. {
  319. Dictionary<string, object> dicData = i_jaData[0][BLWording.DATA].ToObject<Dictionary<string, object>>();
  320. Dictionary<string, object> dicWhereData = i_jaData[0][BLWording.WHEREDATA].ToObject<Dictionary<string, object>>();
  321. string sUserUID = dicWhereData[tb_sys_user.CN_UID].ToString();
  322. var data = i_jaData[0][BLWording.DATA] as JObject;
  323. data.Remove(role_uid);
  324. // 更新個人資訊 tb_hr_employee
  325. tb_hr_employee cEmpData = new tb_hr_employee();
  326. sMsg = valueAssignment(i_crmInput, cEmpData, data, out List<Command> lcUpdEmp);
  327. if (sMsg != null)
  328. {
  329. break;
  330. }
  331. Command cEmp = Command.SetupUpdateCmd(cEmpData, new WhereNode(tb_hr_employee.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_hr_employee), sUserUID));
  332. lCmds.Add(cEmp);
  333. // 更新帳號資訊 tb_sys_user
  334. tb_sys_user cUserData = new tb_sys_user();
  335. sMsg = valueAssignment(i_crmInput, cUserData, data, out List<Command> lcUpdUser);
  336. if (sMsg != null)
  337. {
  338. break;
  339. }
  340. Command cUser = Command.SetupUpdateCmd(cUserData, new WhereNode(tb_sys_user.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user), sUserUID));
  341. lCmds.Add(cUser);
  342. // 更新密碼
  343. if (dicData.ContainsKey(tb_sys_user2entercode.CN_USER_ENTERCODE) && dicData[tb_sys_user2entercode.CN_USER_ENTERCODE] != null)
  344. {
  345. sMsg = EncryptHelper.Encrypt(dicData[tb_sys_user2entercode.CN_USER_ENTERCODE].ToString(), out string sEnc);
  346. if (sMsg != null)
  347. {
  348. break;
  349. }
  350. tb_sys_user2entercode cEnterCode = new tb_sys_user2entercode
  351. {
  352. user_entercode = sEnc
  353. };
  354. tb_sys_user2entercode cEnterCodeCond = new tb_sys_user2entercode() { user_uid = sUserUID };
  355. lCmds.Add(Command.SetupUpdateCmd(cEnterCode, cEnterCodeCond));
  356. }
  357. // 更新角色
  358. // 查詢原始角色資料
  359. tb_sys_user2role cRole = new tb_sys_user2role();
  360. cRole.SetDirty(tb_sys_user2role.CN_ROLE_UID);
  361. tb_sys_user2role cRoleCon = new tb_sys_user2role() { user_uid = sUserUID };
  362. Command cRoleSelect = Command.SetupSelectCmd(cRole, cRoleCon);
  363. ArsenalInterface ai = ArsenalDBMgr.GetInst(cRoleSelect);
  364. List<tb_sys_user2role> qdsRole = ai.RunQueryList<tb_sys_user2role>(cRoleSelect);
  365. var origRole = qdsRole.Select(x => x.role_uid).ToList();
  366. var lsRole = new List<string>() { };
  367. if (dicData.ContainsKey(role_uid))
  368. {
  369. var jaRole = dicData[role_uid] as JArray;
  370. foreach (var role in jaRole)
  371. {
  372. lsRole.Add(role.ToString());
  373. }
  374. }
  375. var addRole = lsRole.Except(origRole);
  376. var delRole = origRole.Except(lsRole);
  377. if (addRole.Any())
  378. {
  379. foreach (var id in addRole)
  380. {
  381. tb_sys_user2role cNew = new tb_sys_user2role()
  382. {
  383. user_uid = sUserUID,
  384. role_uid = id
  385. };
  386. lCmds.Add(Command.SetupInsertCmd(cNew));
  387. }
  388. }
  389. if (delRole.Any())
  390. {
  391. foreach (var id in delRole)
  392. {
  393. tb_sys_user2role cCond = new tb_sys_user2role() { user_uid = sUserUID, role_uid = id };
  394. lCmds.Add(Command.SetupDeleteCmd(cCond));
  395. }
  396. }
  397. // 更新所屬分店
  398. // 查詢分店原始資料
  399. tb_hr_employee2branch cEmp2Branch = new tb_hr_employee2branch();
  400. cEmp2Branch.SetDirty(tb_hr_employee2branch.CN_BRANCH_UID);
  401. tb_hr_employee2branch cCon = new tb_hr_employee2branch() { uid = sUserUID };
  402. Command cSelect = Command.SetupSelectCmd(cEmp2Branch, cCon);
  403. ai = ArsenalDBMgr.GetInst(cSelect);
  404. List<tb_hr_employee2branch> qdsBranch = ai.RunQueryList<tb_hr_employee2branch>(cSelect);
  405. var origin = qdsBranch.Select(x => x.branch_uid).ToList();
  406. var lsBranch = new List<string>() { };
  407. if (dicData.ContainsKey(tb_hr_employee2branch.CN_BRANCH_UID))
  408. {
  409. var jaBranch = dicData[tb_hr_employee2branch.CN_BRANCH_UID] as JArray;
  410. foreach (var branch in jaBranch)
  411. {
  412. lsBranch.Add(branch.ToString());
  413. }
  414. var addBranch = lsBranch.Except(origin);
  415. var delBranch = origin.Except(lsBranch);
  416. if (addBranch.Any())
  417. {
  418. foreach (var id in addBranch)
  419. {
  420. tb_hr_employee2branch cNew = new tb_hr_employee2branch()
  421. {
  422. uid = sUserUID,
  423. branch_uid = id
  424. };
  425. lCmds.Add(Command.SetupInsertCmd(cNew));
  426. }
  427. }
  428. if (delBranch.Any())
  429. {
  430. foreach (var id in delBranch)
  431. {
  432. tb_hr_employee2branch cCond = new tb_hr_employee2branch() { uid = sUserUID, branch_uid = id };
  433. lCmds.Add(Command.SetupDeleteCmd(cCond));
  434. }
  435. }
  436. }
  437. }
  438. while (false);
  439. }
  440. catch (Exception ex)
  441. {
  442. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  443. sMsg = $"{nameof(updateCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  444. #if DEBUG
  445. System.Diagnostics.Debug.WriteLine(sMsg);
  446. #endif
  447. }
  448. o_lResult = lCmds;
  449. return sMsg;
  450. }
  451. protected string deleteCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out List<Command> o_lcResult, List<string> i_saQryContainKeys,
  452. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  453. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  454. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  455. {
  456. string sMsg = null;
  457. List<Command> lCmds = new List<Command>();
  458. try
  459. {
  460. do
  461. {
  462. foreach (JToken jtkProg in i_jaData)
  463. {
  464. Dictionary<string, object> dicData = jtkProg.ToObject<Dictionary<string, object>>();
  465. Dictionary<string, object> wheredataDic = null;
  466. string user_uid = null;
  467. if (dicData.ContainsKey(BLWording.WHEREDATA))
  468. {
  469. JObject wheredata = dicData[BLWording.WHEREDATA] as JObject;
  470. wheredataDic = wheredata.ToObject<Dictionary<string, object>>();
  471. if (wheredataDic.ContainsKey(BLWording.UID))
  472. {
  473. user_uid = wheredataDic[BLWording.UID].ToString();
  474. }
  475. }
  476. // 刪除角色
  477. tb_sys_user2role r = new tb_sys_user2role()
  478. {
  479. user_uid = user_uid
  480. };
  481. lCmds.Add(Command.SetupDeleteCmd(r));
  482. // 刪除密碼
  483. tb_sys_user2entercode p = new tb_sys_user2entercode()
  484. {
  485. user_uid = user_uid
  486. };
  487. lCmds.Add(Command.SetupDeleteCmd(p));
  488. // 刪除個人資訊
  489. tb_hr_employee u = new tb_hr_employee()
  490. {
  491. uid = user_uid
  492. };
  493. lCmds.Add(Command.SetupDeleteCmd(u));
  494. // 刪除帳號
  495. tb_sys_user cUser = new tb_sys_user()
  496. {
  497. uid = user_uid
  498. };
  499. lCmds.Add(Command.SetupDeleteCmd(cUser));
  500. }
  501. }
  502. while (false);
  503. }
  504. catch (Exception ex)
  505. {
  506. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  507. sMsg = $"{nameof(deleteCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  508. #if DEBUG
  509. System.Diagnostics.Debug.WriteLine(sMsg);
  510. #endif
  511. }
  512. o_lcResult = lCmds;
  513. return sMsg;
  514. }
  515. //儲存密碼資訊
  516. public CResponseMessage CreatePassword(CRequestMessage i_crmInput)
  517. {
  518. CResponseMessage crm = null;
  519. string sMsg = null;
  520. Command cRes = null;
  521. do
  522. {
  523. try
  524. {
  525. List<Command> lCmdMaster = new List<Command>();
  526. string sAccount = "";
  527. string sEnterCode = "";
  528. object i_dicData = i_crmInput.param[BLWording.ADD_MASTER];
  529. if (i_dicData is JArray)
  530. {
  531. JArray jaData = i_dicData as JArray;
  532. foreach (JToken joData in jaData)
  533. {
  534. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  535. JObject jdata = dicData[BLWording.DATA] as JObject;
  536. Dictionary<string, object> dicInput = jdata.ToObject<Dictionary<string, object>>();
  537. if (dicInput.ContainsKey(tb_sys_user.CN_ACCOUNT))
  538. {
  539. sAccount = dicInput[tb_sys_user.CN_ACCOUNT].ToString();
  540. }
  541. if (dicInput.ContainsKey(tb_sys_user2entercode.CN_USER_ENTERCODE))
  542. {
  543. sEnterCode = dicInput[tb_sys_user2entercode.CN_USER_ENTERCODE].ToString();
  544. }
  545. }
  546. }
  547. // 查詢原始資料
  548. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  549. QueryJsonElement qjeOrigin = lBlocks.GetInst();
  550. qjeOrigin.table = tb_sys_user.TABLENAME;
  551. qjeOrigin.displaycols = new List<string>() { tb_sys_user.CN_UID };
  552. qjeOrigin.wherecols = new WhereNode(tb_sys_user.CN_ACCOUNT, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user), sAccount);
  553. lBlocks.Add(qjeOrigin);
  554. sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes);
  555. if (sMsg != null)
  556. {
  557. break;
  558. }
  559. ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
  560. List<tb_sys_user> qds = ai.RunQueryList<tb_sys_user>(cRes);
  561. if (!cRes.IsSuccess)
  562. {
  563. sMsg = cRes.LastErrorCode;
  564. break;
  565. }
  566. if (qds.Any())
  567. {
  568. var origin = qds.Select(x => x.uid).ToList();
  569. if (origin.Any())
  570. {
  571. foreach (var id in origin)
  572. {
  573. tb_sys_user2entercode cNew = new tb_sys_user2entercode()
  574. {
  575. user_uid = id,
  576. user_entercode = sEnterCode,
  577. };
  578. lCmdMaster.Add(Command.SetupInsertCmd(cNew));
  579. }
  580. }
  581. ai = ArsenalDBMgr.GetInst(lCmdMaster[0], GetDefaultSystemColumnInfo());
  582. ai.RunEditCmds(lCmdMaster);
  583. sMsg = GetLastErrorCode(lCmdMaster);
  584. if (sMsg != null)
  585. {
  586. break;
  587. }
  588. crm = new CSuccessResponseMessage(null, i_crmInput);
  589. }
  590. }
  591. catch (Exception ex)
  592. {
  593. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_crmInput);
  594. sMsg = $"{nameof(CreatePassword)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}.";
  595. #if DEBUG
  596. System.Diagnostics.Debug.WriteLine(sMsg);
  597. #endif
  598. }
  599. }
  600. while (false);
  601. if (sMsg != null)
  602. {
  603. crm = new CErrorResponseMessage(sMsg, i_crmInput);
  604. }
  605. return crm;
  606. }
  607. /// <summary>
  608. /// 取得帳號與角色的關係
  609. /// </summary>
  610. /// <param name="i_crmInput"></param>
  611. /// <returns></returns>
  612. [Auth(false)]
  613. public CResponseMessage GetUser2Role(CRequestMessage i_crmInput)
  614. {
  615. CResponseMessage crmRes = null;
  616. string sMsg;
  617. try
  618. {
  619. do
  620. {
  621. string user_uid = null;
  622. object i_dicData = i_crmInput.param[BLWording.QRY_MASTER];
  623. if (i_dicData is JArray)
  624. {
  625. JArray jaData = i_dicData as JArray;
  626. foreach (JToken joData in jaData)
  627. {
  628. Dictionary<string, object> dicData = joData.ToObject<Dictionary<string, object>>();
  629. user_uid = dicData[BLWording.USER_ID].ToString();
  630. }
  631. }
  632. // 查詢原始資料
  633. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  634. QueryJsonElement qjeOrigin = lBlocks.GetInst();
  635. qjeOrigin.table = tb_sys_user2role.TABLENAME;
  636. qjeOrigin.displaycols = new List<string>() { tb_sys_user2role.CN_ROLE_UID };
  637. qjeOrigin.aliascols = new Dictionary<string, List<string>>()
  638. { {tb_sys_user2role.CN_ROLE_UID, new List<string>(){ tb_sys_role.CN_UID } } };
  639. qjeOrigin.wherecols = new WhereNode(tb_sys_user2role.CN_USER_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_sys_user2role), user_uid);
  640. lBlocks.Add(qjeOrigin);
  641. QueryJsonElement qjeRole = lBlocks.GetInst();
  642. qjeRole.table = tb_sys_role.TABLENAME;
  643. qjeRole.displaycols = new List<string>() { tb_sys_role.CN_NAME };
  644. qjeRole.jointype = QueryJsonElement.LEFT_JOIN;
  645. qjeRole.jointable = qjeOrigin;
  646. qjeRole.joincols = new Dictionary<string, string>() {
  647. { tb_sys_role.CN_UID, tb_sys_user2role.CN_ROLE_UID}
  648. };
  649. lBlocks.Add(qjeRole);
  650. sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes);
  651. if (sMsg != null)
  652. {
  653. break;
  654. }
  655. ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
  656. List<tb_sys_role> qdRole = ai.RunQueryList<tb_sys_role>(cRes);
  657. var resp = new Dictionary<object, string>();
  658. if (qdRole.Count > 0)
  659. {
  660. foreach (var role in qdRole)
  661. {
  662. resp.Add(role.name, role.uid);
  663. }
  664. }
  665. crmRes = new CSuccessResponseMessage(null, i_crmInput);
  666. // 填寫回傳
  667. crmRes.param.Add(BLWording.DATA, resp);
  668. }
  669. while (false);
  670. }
  671. catch (Exception ex)
  672. {
  673. LogHelper.DBLog(Util.GetLastExceptionMsg(ex));
  674. sMsg = $"{nameof(GetUser2Role)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. ";
  675. #if DEBUG
  676. System.Diagnostics.Debug.WriteLine(sMsg);
  677. #endif
  678. }
  679. if (null != sMsg)
  680. {
  681. crmRes = new CErrorResponseMessage(sMsg, i_crmInput);
  682. }
  683. return crmRes;
  684. }
  685. /// <summary>
  686. /// 更新tb_sys_user personalsetting欄位
  687. /// </summary>
  688. /// <param name="i_sUserUID"></param>
  689. /// <param name="i_sKey"></param>
  690. /// <param name="i_sVal"></param>
  691. /// <returns></returns>
  692. public string UpdateUserProfile(string i_sUserUID, string i_sKey, string i_sVal, out Command o_cUpdateCommand)
  693. {
  694. string sMsg = null;
  695. Command cUpdateCommand = null;
  696. // 查詢personal_setting資料
  697. tb_hr_employee cSelect = new tb_hr_employee();
  698. tb_hr_employee cCon = new tb_hr_employee() { uid = i_sUserUID };
  699. cSelect.SetFullDirty();
  700. Command cSe = Command.SetupSelectCmd(cSelect, cCon);
  701. ArsenalInterface ai = ArsenalDBMgr.GetInst(cSe);
  702. QueryDataSet qds = ai.RunQueryDataSet(cSe, null);
  703. var data = qds.DATA.Tables[0].AsEnumerable();
  704. if (data.Any())
  705. {
  706. var jsonSetting = "";
  707. var personalSetting = data.Select(x => x.Field<string>(tb_hr_employee.CN_PERSONAL_SETTING)).SingleOrDefault();
  708. if (personalSetting != null)
  709. {
  710. var setting = JsonConvert.DeserializeObject<SoldierData.PersonalSetting>(personalSetting); // 將json字串反序列化
  711. switch (i_sKey)
  712. {
  713. case PersonalSetting.CN_FAVORITE:
  714. List<string> lsFavorite = i_sVal.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
  715. List<string> data_ls = new List<string>();
  716. foreach (string s in lsFavorite)
  717. {
  718. if (!data_ls.Contains(s))
  719. {
  720. data_ls.Add(s);
  721. }
  722. }
  723. setting.Favorite = data_ls;
  724. break;
  725. case PersonalSetting.CN_CALCOLOR:
  726. setting.CalColor = i_sVal;
  727. break;
  728. }
  729. jsonSetting = JsonConvert.SerializeObject(setting); // 將物件序列化
  730. }
  731. else
  732. {
  733. SoldierData.PersonalSetting psModel = new SoldierData.PersonalSetting();
  734. switch (i_sKey)
  735. {
  736. case PersonalSetting.CN_FAVORITE:
  737. psModel.Favorite = i_sVal.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
  738. break;
  739. case PersonalSetting.CN_CALCOLOR:
  740. psModel.CalColor = i_sVal;
  741. break;
  742. }
  743. jsonSetting = JsonConvert.SerializeObject(psModel); // 將物件序列化
  744. }
  745. // 更新帳號資料表的personal_setting欄位
  746. tb_hr_employee cUpdateData = new tb_hr_employee
  747. {
  748. personal_setting = jsonSetting
  749. };
  750. tb_hr_employee cUpdateCond = new tb_hr_employee() { uid = i_sUserUID };
  751. cUpdateCommand = Command.SetupUpdateCmd(cUpdateData, cUpdateCond);
  752. }
  753. o_cUpdateCommand = cUpdateCommand;
  754. return sMsg;
  755. }
  756. /// <summary>
  757. /// 新增到我的最愛
  758. /// </summary>
  759. /// <param name="i_crmInput"></param>
  760. /// <returns></returns>
  761. public CResponseMessage Favorite(CRequestMessage i_crmInput)
  762. {
  763. string sMsg = null;
  764. CResponseMessage crmRes = null;
  765. do
  766. {
  767. List<Dictionary<string, object>> lDic = getQryParameterList(i_crmInput);
  768. if (lDic != null && lDic.Any())
  769. {
  770. tb_sys_session user = i_crmInput.customparam[BLWording.SESSION_USER] as tb_sys_session;
  771. var userUID = user.update_user_uid;
  772. sMsg = new MemberService().UpdateUserProfile(userUID, SoldierData.PersonalSetting.CN_FAVORITE, lDic[0]["content"].ToString(), out Command cUpdate);
  773. if (sMsg != null)
  774. {
  775. break;
  776. }
  777. if (cUpdate != null)
  778. {
  779. ArsenalInterface ai = ArsenalDBMgr.GetInst(cUpdate, GetDefaultSystemColumnInfo());
  780. ai.RunEditCmds(new List<Command>() { cUpdate });
  781. sMsg = GetLastErrorCode(cUpdate);
  782. if (sMsg != null)
  783. {
  784. break;
  785. }
  786. }
  787. }
  788. crmRes = new CSuccessResponseMessage(null, i_crmInput);
  789. }
  790. while (false);
  791. if (sMsg != null)
  792. {
  793. crmRes = new CErrorResponseMessage(sMsg, i_crmInput);
  794. }
  795. return crmRes;
  796. }
  797. }
  798. }