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.

465 lines
20 KiB

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