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.

507 lines
21 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyBL.WEBAPP.ShowEasy;
  3. using EasyNet;
  4. using Entity.Sugar;
  5. using Entity.ViewModels;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using SqlSugar;
  9. using SqlSugar.Base;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. namespace EasyBL.WEBAPP.WSM
  15. {
  16. public class MembersMaintain_UpdService : ServiceBase
  17. {
  18. #region 帳號管理(單筆查詢)
  19. /// <summary>
  20. /// 帳號管理(單筆查詢)
  21. /// </summary>
  22. /// <param name="i_crm"></param>
  23. /// <returns></returns>
  24. public ResponseMessage QueryOne(RequestMessage i_crm)
  25. {
  26. ResponseMessage rm = null;
  27. string sMsg = null;
  28. var db = SugarBase.GetIntance();
  29. try
  30. {
  31. do
  32. {
  33. var sMemberID = _fetchString(i_crm, @"MemberID");
  34. var oEntity = db.Queryable<OTB_SYS_Members, OTB_SYS_Members, OTB_SYS_Members>
  35. ((t1, t2, t3) =>
  36. new object[] {
  37. JoinType.Left, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  38. JoinType.Left, t1.OrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
  39. }
  40. )
  41. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.MemberID == sMemberID)
  42. .Select((t1, t2, t3) => new View_SYS_Members
  43. {
  44. MemberID = SqlFunc.GetSelfAndAutoFill(t1.MemberID),
  45. CreateUserName = t2.MemberName,
  46. ModifyUserName = t3.MemberName,
  47. RuleIDs = SqlFunc.MappingColumn(t1.MemberID, "(select RuleID+',' from OTB_SYS_MembersToRule mr where mr.OrgID=t1.OrgID and mr.MemberID=t1.MemberID for xml path(''))")
  48. })
  49. .Single();
  50. rm = new SuccessResponseMessage(null, i_crm);
  51. rm.DATA.Add(BLWording.REL, oEntity);
  52. } while (false);
  53. }
  54. catch (Exception ex)
  55. {
  56. sMsg = Util.GetLastExceptionMsg(ex);
  57. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(MembersMaintain_UpdService), "", "QueryOne(帳號管理(單筆查詢))", "", "", "");
  58. }
  59. finally
  60. {
  61. if (null != sMsg)
  62. {
  63. rm = new ErrorResponseMessage(sMsg, i_crm);
  64. }
  65. }
  66. return rm;
  67. }
  68. #endregion 帳號管理(單筆查詢)
  69. #region 帳號管理(新增)
  70. /// <summary>
  71. /// 帳號管理(新增)
  72. /// </summary>
  73. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  74. /// <returns></returns>
  75. public ResponseMessage Insert(RequestMessage i_crm)
  76. {
  77. ResponseMessage rm = null;
  78. string sMsg = null;
  79. try
  80. {
  81. rm = SugarBase.ExecTran(db =>
  82. {
  83. do
  84. {
  85. var oEntity = _fetchEntity<OTB_SYS_Members>(i_crm);
  86. _setEntityBase(oEntity, i_crm);
  87. //Yang 2019/01/25 判斷Password是否為空值,是直接給預序密碼加密;否取得密碼加密
  88. if (!string.IsNullOrEmpty(oEntity.Password))
  89. {
  90. oEntity.Password = SecurityUtil.Encrypt(oEntity.Password); //Yang 2019/01/25 取得密碼後加密
  91. }
  92. else
  93. {
  94. oEntity.Password = SecurityUtil.Encrypt("123456"); //Yang 2019/01/25 新增帳號預設密碼加密,建議預設值放到web.config
  95. }
  96. oEntity.SysShowMode = "M";
  97. var iRel = db.Insertable(oEntity).ExecuteCommand();
  98. var saRuleInsert = new List<OTB_SYS_MembersToRule>();
  99. //更新角色
  100. if (i_crm.DATA["roles"] is JArray saRoles && saRoles.Count > 0)
  101. {
  102. foreach (var roleid in saRoles)
  103. {
  104. var oMembersToRule = new OTB_SYS_MembersToRule
  105. {
  106. RuleID = roleid.ToString(),
  107. MemberID = oEntity.MemberID
  108. };
  109. _setEntityBase(oMembersToRule, i_crm);
  110. saRuleInsert.Add(oMembersToRule);
  111. }
  112. iRel += db.Insertable(saRuleInsert).ExecuteCommand();
  113. }
  114. string sError = null;
  115. var sEmail = oEntity.Email; //取得信箱
  116. var oEmail = new Emails(); //寄件人
  117. var toEmail = new List<EmailTo>(); //收件人
  118. var oEmailTo = new EmailTo //收件人資訊
  119. {
  120. ToUserID = oEntity.MemberID,
  121. ToUserName = oEntity.MemberName,
  122. ToEmail = oEntity.Email,
  123. Type = "to"
  124. };
  125. toEmail.Add(oEmailTo);
  126. oEmail.FromUserName = "No-Reply@showeasy.com";//取fonfig
  127. oEmail.Title = "ShowEasy 帳號成功建立";//取fonfig
  128. oEmail.EmailBody = NewMember(oEntity.MemberID, oEntity.MemberName);
  129. oEmail.IsCCSelf = false;
  130. oEmail.Attachments = null;
  131. oEmail.EmailTo = toEmail;
  132. var bSend = new MailService(oEntity.OrgID, true).MailFactory(oEmail, out sError);
  133. if (sError != null)
  134. {
  135. break;
  136. }
  137. rm = new SuccessResponseMessage(null, i_crm);
  138. rm.DATA.Add(BLWording.REL, iRel);
  139. } while (false);
  140. return rm;
  141. });
  142. }
  143. catch (Exception ex)
  144. {
  145. sMsg = Util.GetLastExceptionMsg(ex);
  146. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(MembersMaintain_UpdService), @"帳號管理", @"Add(帳號管理(新增))", @"", @"", @"");
  147. }
  148. finally
  149. {
  150. if (null != sMsg)
  151. {
  152. rm = new ErrorResponseMessage(sMsg, i_crm);
  153. }
  154. }
  155. return rm;
  156. }
  157. #endregion 帳號管理(新增)
  158. #region 帳號管理(修改)
  159. /// <summary>
  160. /// 帳號管理(修改)
  161. /// </summary>
  162. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  163. /// <returns></returns>
  164. public ResponseMessage Update(RequestMessage i_crm)
  165. {
  166. ResponseMessage rm = null;
  167. string sMsg = null;
  168. try
  169. {
  170. rm = SugarBase.ExecTran(db =>
  171. {
  172. do
  173. {
  174. var oNewEntity = _fetchEntity<OTB_SYS_Members>(i_crm);
  175. var NewDpt = _fetchString(i_crm, @"DepartmentID");
  176. var JobTitle = _fetchString(i_crm, @"JobTitle");
  177. var MemberID = _fetchString(i_crm, @"MemberID");
  178. _setEntityBase(oNewEntity, i_crm);
  179. UpdateCheckFlows(db, MemberID, i_crm.ORIGID, NewDpt, JobTitle, i_crm.USERID);
  180. var iRel = db.Deleteable<OTB_SYS_MembersToRule>().Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == oNewEntity.MemberID).ExecuteCommand();
  181. var saRuleInsert = new List<OTB_SYS_MembersToRule>();
  182. if (i_crm.DATA["roles"] is JArray saRoles && saRoles.Count > 0)
  183. {
  184. //更新角色
  185. foreach (var roleid in saRoles)
  186. {
  187. var oMembersToRule = new OTB_SYS_MembersToRule
  188. {
  189. RuleID = roleid.ToString(),
  190. MemberID = oNewEntity.MemberID
  191. };
  192. _setEntityBase(oMembersToRule, i_crm);
  193. saRuleInsert.Add(oMembersToRule);
  194. }
  195. iRel += db.Insertable(saRuleInsert).ExecuteCommand();
  196. }
  197. iRel += db.Updateable(oNewEntity)
  198. .IgnoreColumns(x => new
  199. {
  200. x.Password,
  201. x.SysShowMode,
  202. x.CreateUser,
  203. x.CreateDate
  204. }).ExecuteCommand();
  205. rm = new SuccessResponseMessage(null, i_crm);
  206. rm.DATA.Add(BLWording.REL, iRel);
  207. } while (false);
  208. return rm;
  209. });
  210. }
  211. catch (Exception ex)
  212. {
  213. sMsg = Util.GetLastExceptionMsg(ex);
  214. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(MembersMaintain_UpdService), @"帳號管理", @"Update(帳號管理(修改))", @"", @"", @"");
  215. }
  216. finally
  217. {
  218. if (null != sMsg)
  219. {
  220. rm = new ErrorResponseMessage(sMsg, i_crm);
  221. }
  222. }
  223. return rm;
  224. }
  225. #endregion 帳號管理(修改)
  226. #region 帳號管理,更新CheckFlows資料
  227. public void UpdateCheckFlows(SqlSugarClient db, string MemberID, string OrgID, string NewDepID, string JobTitle, string UserID)
  228. {
  229. var User = db.Queryable<OTB_SYS_Members>().Where(m => m.MemberID == MemberID && m.OrgID == OrgID).First();
  230. var Dept = db.Queryable<OTB_SYS_Departments>().Where(d => d.DepartmentID == NewDepID && d.OrgID == OrgID).First();
  231. var Job = db.Queryable<OTB_SYS_Jobtitle>().Where(d => d.JobtitleID == JobTitle && d.OrgID == OrgID).First();
  232. if (User != null)
  233. {
  234. var UpdateCheckFlow = new List<OTB_EIP_CheckFlow>();
  235. var MatchedCheckFlows = db.Queryable<OTB_EIP_CheckFlow>().Where(cf => cf.Flows.Contains(MemberID)).ToList();
  236. if (MatchedCheckFlows.Any())
  237. {
  238. foreach (var cf in MatchedCheckFlows)
  239. {
  240. var Flows = JsonConvert.DeserializeObject<List<CheckFlow>>(cf.Flows).OrderBy(c => c.Order).ToList();
  241. foreach (var f in Flows)
  242. {
  243. foreach (var M in f.SignedMember)
  244. {
  245. if (M.id == MemberID)
  246. {
  247. M.deptname = Dept.DepartmentShortName;
  248. M.jobname = Job.JobtitleName;
  249. }
  250. }
  251. }
  252. UpdateCheckFlow.Add(new OTB_EIP_CheckFlow() { OrgID = cf.OrgID, Guid = cf.Guid, Flows = JsonConvert.SerializeObject(Flows), ModifyDate = DateTime.Now, ModifyUser = UserID });
  253. }
  254. if (UpdateCheckFlow.Count > 0)
  255. {
  256. db.Updateable(UpdateCheckFlow)
  257. .UpdateColumns(it => new { it.Flows, it.ModifyDate, it.ModifyUser }).ExecuteCommand();
  258. }
  259. }
  260. }
  261. //比對部門。
  262. //取得所有相關Flows
  263. //更新裡面資料//JArray
  264. }
  265. #endregion
  266. #region 帳號管理(刪除)
  267. /// <summary>
  268. /// 帳號管理(刪除)
  269. /// </summary>
  270. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  271. /// <returns></returns>
  272. public ResponseMessage Delete(RequestMessage i_crm)
  273. {
  274. ResponseMessage rm = null;
  275. string sMsg = null;
  276. try
  277. {
  278. rm = SugarBase.ExecTran(db =>
  279. {
  280. do
  281. {
  282. var sMemberID = _fetchString(i_crm, @"MemberID");
  283. var sOrgID = _fetchString(i_crm, @"OrgID");
  284. var oMembers = db.Queryable<OTB_SYS_Members>().Single(x => x.MemberID == sMemberID && x.OrgID == sOrgID);
  285. var iRel = db.Deleteable<OTB_SYS_Members>().Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == sMemberID && x.OrgID == sOrgID).ExecuteCommand();
  286. iRel += db.Deleteable<OTB_SYS_MembersToRule>().Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == sMemberID && x.OrgID == sOrgID).ExecuteCommand();
  287. i_crm.DATA.Add("FileID", oMembers.MemberPic);
  288. i_crm.DATA.Add("IDType", "parent");
  289. new CommonService().DelFile(i_crm);
  290. rm = new SuccessResponseMessage(null, i_crm);
  291. rm.DATA.Add(BLWording.REL, iRel);
  292. } while (false);
  293. return rm;
  294. });
  295. }
  296. catch (Exception ex)
  297. {
  298. sMsg = Util.GetLastExceptionMsg(ex);
  299. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(MembersMaintain_UpdService), @"帳號管理", @"Delete(帳號管理(刪除))", @"", @"", @"");
  300. }
  301. finally
  302. {
  303. if (null != sMsg)
  304. {
  305. rm = new ErrorResponseMessage(sMsg, i_crm);
  306. }
  307. }
  308. return rm;
  309. }
  310. #endregion 帳號管理(刪除)
  311. #region 帳號管理(職稱下拉查詢)
  312. /// <summary>
  313. /// 帳號管理(職稱下拉查詢)
  314. /// </summary>
  315. /// <param name="i_crm"></param>
  316. /// <returns></returns>
  317. public ResponseMessage GetJobTitleDrop(RequestMessage i_crm)
  318. {
  319. ResponseMessage rm = null;
  320. string sMsg = null;
  321. var db = SugarBase.GetIntance();
  322. try
  323. {
  324. do
  325. {
  326. var sEffective = _fetchString(i_crm, @"Effective");
  327. var saJobtitle = db.Queryable<OTB_SYS_Jobtitle>()
  328. .Where(x => x.OrgID == i_crm.ORIGID)
  329. .WhereIF(!string.IsNullOrEmpty(sEffective), x => x.Effective == sEffective)
  330. .ToList();
  331. rm = new SuccessResponseMessage(null, i_crm);
  332. rm.DATA.Add(BLWording.REL, saJobtitle);
  333. } while (false);
  334. }
  335. catch (Exception ex)
  336. {
  337. sMsg = Util.GetLastExceptionMsg(ex);
  338. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(MembersMaintain_UpdService), "", "GetJobTitleDrop(帳號管理(職稱下拉查詢))", "", "", "");
  339. }
  340. finally
  341. {
  342. if (null != sMsg)
  343. {
  344. rm = new ErrorResponseMessage(sMsg, i_crm);
  345. }
  346. }
  347. return rm;
  348. }
  349. #endregion 帳號管理(職稱下拉查詢)
  350. #region 帳號管理(角色下拉查詢)
  351. /// <summary>
  352. /// 帳號管理(角色下拉查詢)
  353. /// </summary>
  354. /// <param name="i_crm"></param>
  355. /// <returns></returns>
  356. public ResponseMessage GetRolesDrop(RequestMessage i_crm)
  357. {
  358. ResponseMessage rm = null;
  359. string sMsg = null;
  360. var db = SugarBase.GetIntance();
  361. try
  362. {
  363. do
  364. {
  365. var sEffective = _fetchString(i_crm, @"Effective");
  366. var oEntity = db.Queryable<OTB_SYS_Rules>()
  367. .Where(x => x.OrgID == i_crm.ORIGID && x.DelStatus == "N")
  368. .ToList();
  369. rm = new SuccessResponseMessage(null, i_crm);
  370. rm.DATA.Add(BLWording.REL, oEntity);
  371. } while (false);
  372. }
  373. catch (Exception ex)
  374. {
  375. sMsg = Util.GetLastExceptionMsg(ex);
  376. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(MembersMaintain_UpdService), "", "GetRolesDrop(帳號管理(角色下拉查詢))", "", "", "");
  377. }
  378. finally
  379. {
  380. if (null != sMsg)
  381. {
  382. rm = new ErrorResponseMessage(sMsg, i_crm);
  383. }
  384. }
  385. return rm;
  386. }
  387. #endregion 帳號管理(角色下拉查詢)
  388. #region 帳號管理(查詢筆數)
  389. /// <summary>
  390. /// 帳號管理(查詢筆數)
  391. /// </summary>
  392. /// <param name="i_crm"></param>
  393. /// <returns></returns>
  394. public ResponseMessage QueryCout(RequestMessage i_crm)
  395. {
  396. ResponseMessage rm = null;
  397. string sMsg = null;
  398. var db = SugarBase.GetIntance();
  399. try
  400. {
  401. do
  402. {
  403. var sMemberID = _fetchString(i_crm, @"MemberID");
  404. var s_MemberID = _fetchString(i_crm, @"_MemberID");
  405. var sEmail = _fetchString(i_crm, @"Email");
  406. var sOutlookAccount = _fetchString(i_crm, @"OutlookAccount");
  407. var iCout = db.Queryable<OTB_SYS_Members>()
  408. .Where(x => x.OrgID == i_crm.ORIGID)
  409. .WhereIF(!string.IsNullOrEmpty(sMemberID), x => x.MemberID == sMemberID)
  410. .WhereIF(!string.IsNullOrEmpty(s_MemberID), x => x.MemberID != s_MemberID)
  411. .WhereIF(!string.IsNullOrEmpty(sEmail), x => x.Email == sEmail)
  412. .WhereIF(!string.IsNullOrEmpty(sOutlookAccount), x => x.OutlookAccount == sOutlookAccount)
  413. .Count();
  414. rm = new SuccessResponseMessage(null, i_crm);
  415. rm.DATA.Add(BLWording.REL, iCout);
  416. } while (false);
  417. }
  418. catch (Exception ex)
  419. {
  420. sMsg = Util.GetLastExceptionMsg(ex);
  421. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ArgumentClassMaintain_UpdService), "", "QueryCout(帳號管理(查詢筆數))", "", "", "");
  422. }
  423. finally
  424. {
  425. if (null != sMsg)
  426. {
  427. rm = new ErrorResponseMessage(sMsg, i_crm);
  428. }
  429. }
  430. return rm;
  431. }
  432. #endregion 帳號管理(查詢筆數)
  433. //newMemberMail
  434. private string NewMember(string MemberID, string MemberName)
  435. {
  436. StringBuilder sb = new StringBuilder();
  437. EmailTemplate mailTemplate = new EmailTemplate();
  438. sb.Append(mailTemplate.MailHeader());
  439. sb.Append(mailTemplate.MailCSS());
  440. //Email Body
  441. sb.Append("<body width = \"100%\" style = \"margin: 0; padding: 0 !important;background: #f8f8f8;mso-line-height-rule: exactly;\"> ");
  442. sb.Append("<center style=\"width: 100 %; background: #f8f8f8\">");
  443. sb.Append("<div class=\"email-container\" style=\"max-width: 500px; margin: 0 auto\">");
  444. sb.Append("<tr>");
  445. sb.Append("<td style=\"padding-top: 20px; padding-bottom: 20px\" class=\"sm-p\">");
  446. sb.Append("<table align = \"left\" border = \"0\" cellpadding = \"0\" cellspacing = \"0\" role = \"presentation\" width = \"100%\"> ");
  447. sb.Append("<tr>");
  448. sb.Append("<td style=\"display: block; color: #232323; text-align: left\">");
  449. sb.Append("<h1 style = \"text-align: left;font-family: 'Arial', 'sans-serif';font-weight: 400;font-size: 18px;line-height: 30px;color: #232323;margin: 0 0 0 0;\"> ");
  450. sb.Append("Hello, " + MemberName + "<br />");
  451. sb.Append("您的帳號已成功建立<br />");
  452. sb.Append("帳號:" + MemberID + "<br />");
  453. sb.Append("預設密碼:123456 (請登入後前往個人設定中更改)<br />");
  454. sb.Append("</h1>");
  455. sb.Append("</td>");
  456. sb.Append("</tr>");
  457. sb.Append("</table>");
  458. sb.Append("</td>");
  459. sb.Append("</tr>");
  460. sb.Append("</table>");
  461. sb.Append("</div>");
  462. sb.Append("</center>");
  463. sb.Append("</body>");
  464. sb.Append("</html>");
  465. return sb.ToString();
  466. }
  467. }
  468. }