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.

253 lines
10 KiB

2 years ago
2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using Newtonsoft.Json.Linq;
  4. using SqlSugar;
  5. using SqlSugar.Base;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace EasyBL.WEBAPP.WSM
  9. {
  10. public class RoleMaintain_UpdService : ServiceBase
  11. {
  12. #region 角色管理(單筆查詢)
  13. /// <summary>
  14. /// 角色管理(單筆查詢)
  15. /// </summary>
  16. /// <param name="i_crm"></param>
  17. /// <returns></returns>
  18. public ResponseMessage QueryOne(RequestMessage i_crm)
  19. {
  20. ResponseMessage rm = null;
  21. string sMsg = null;
  22. var db = SugarBase.GetIntance();
  23. try
  24. {
  25. do
  26. {
  27. var sRuleID = _fetchString(i_crm, @"RuleID");
  28. var oEntity = db.Queryable<OTB_SYS_Rules, OTB_SYS_Members, OTB_SYS_Members>
  29. ((t1, t2, t3) =>
  30. new object[] {
  31. JoinType.Left, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  32. JoinType.Left, t1.OrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
  33. }
  34. )
  35. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.RuleID == sRuleID)
  36. .Select((t1, t2, t3) => new OTB_SYS_Rules
  37. {
  38. RuleID = SqlFunc.GetSelfAndAutoFill(t1.RuleID),
  39. CreateUserName = t2.MemberName,
  40. ModifyUserName = t3.MemberName,
  41. ExFeild1 = SqlFunc.MappingColumn(t1.RuleID, "(SELECT b.MemberID+',' FROM OTB_SYS_MembersToRule b left join OTB_SYS_Members c on b.MemberID=c.MemberID and b.OrgID=c.OrgID WHERE c.Effective='Y' and c.OrgID=t1.OrgID and b.OrgID=t1.OrgID and b.RuleID=t1.RuleID FOR XML PATH(''))")
  42. })
  43. .Single();
  44. rm = new SuccessResponseMessage(null, i_crm);
  45. rm.DATA.Add(BLWording.REL, oEntity);
  46. } while (false);
  47. }
  48. catch (Exception ex)
  49. {
  50. sMsg = Util.GetLastExceptionMsg(ex);
  51. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), "", "QueryOne(角色管理(單筆查詢))", "", "", "");
  52. }
  53. finally
  54. {
  55. if (null != sMsg)
  56. {
  57. rm = new ErrorResponseMessage(sMsg, i_crm);
  58. }
  59. }
  60. return rm;
  61. }
  62. #endregion 角色管理(單筆查詢)
  63. #region 角色管理(新增)
  64. /// <summary>
  65. /// 角色管理(新增)
  66. /// </summary>
  67. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  68. /// <returns></returns>
  69. public ResponseMessage Insert(RequestMessage i_crm)
  70. {
  71. ResponseMessage rm = null;
  72. string sMsg = null;
  73. try
  74. {
  75. rm = SugarBase.ExecTran(db =>
  76. {
  77. do
  78. {
  79. var oEntity = _fetchEntity<OTB_SYS_Rules>(i_crm);
  80. _setEntityBase(oEntity, i_crm);
  81. oEntity.DelStatus = "N";
  82. var oEntity_Del = db.Queryable<OTB_SYS_Rules>()
  83. .Single(x => x.OrgID == i_crm.ORIGID && x.RuleID == oEntity.RuleID && x.DelStatus == "Y");
  84. var iRel = oEntity_Del != null ? db.Updateable(oEntity)
  85. .IgnoreColumns(x => new
  86. {
  87. x.CreateUser,
  88. x.CreateDate
  89. }).ExecuteCommand() : db.Insertable(oEntity).ExecuteCommand();
  90. var saRuleInsert = new List<OTB_SYS_MembersToRule>();
  91. //更新角色
  92. if (i_crm.DATA["users"] is JArray saUsers && saUsers.Count > 0)
  93. {
  94. foreach (var userid in saUsers)
  95. {
  96. var oMembersToRule = new OTB_SYS_MembersToRule
  97. {
  98. RuleID = oEntity.RuleID,
  99. MemberID = userid.ToString()
  100. };
  101. _setEntityBase(oMembersToRule, i_crm);
  102. saRuleInsert.Add(oMembersToRule);
  103. }
  104. iRel += db.Insertable(saRuleInsert).ExecuteCommand();
  105. }
  106. rm = new SuccessResponseMessage(null, i_crm);
  107. rm.DATA.Add(BLWording.REL, iRel);
  108. } while (false);
  109. return rm;
  110. });
  111. }
  112. catch (Exception ex)
  113. {
  114. sMsg = Util.GetLastExceptionMsg(ex);
  115. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), @"角色管理", @"Add(角色管理(新增))", @"", @"", @"");
  116. }
  117. finally
  118. {
  119. if (null != sMsg)
  120. {
  121. rm = new ErrorResponseMessage(sMsg, i_crm);
  122. }
  123. }
  124. return rm;
  125. }
  126. #endregion 角色管理(新增)
  127. #region 角色管理(修改)
  128. /// <summary>
  129. /// 角色管理(修改)
  130. /// </summary>
  131. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  132. /// <returns></returns>
  133. public ResponseMessage Update(RequestMessage i_crm)
  134. {
  135. ResponseMessage rm = null;
  136. string sMsg = null;
  137. try
  138. {
  139. rm = SugarBase.ExecTran(db =>
  140. {
  141. do
  142. {
  143. var oNewEntity = _fetchEntity<OTB_SYS_Rules>(i_crm);
  144. _setEntityBase(oNewEntity, i_crm);
  145. var iRel = db.Updateable(oNewEntity)
  146. .IgnoreColumns(x => new
  147. {
  148. x.DelStatus,
  149. x.CreateUser,
  150. x.CreateDate
  151. }).ExecuteCommand();
  152. iRel += db.Deleteable<OTB_SYS_MembersToRule>().Where(x => x.OrgID == i_crm.ORIGID && x.RuleID == oNewEntity.RuleID).ExecuteCommand();
  153. var saRuleInsert = new List<OTB_SYS_MembersToRule>();
  154. //更新角色
  155. if (i_crm.DATA["users"] is JArray saUsers && saUsers.Count > 0)
  156. {
  157. foreach (var userid in saUsers)
  158. {
  159. var oMembersToRule = new OTB_SYS_MembersToRule
  160. {
  161. RuleID = oNewEntity.RuleID,
  162. MemberID = userid.ToString()
  163. };
  164. _setEntityBase(oMembersToRule, i_crm);
  165. saRuleInsert.Add(oMembersToRule);
  166. }
  167. iRel += db.Insertable(saRuleInsert).ExecuteCommand();
  168. }
  169. rm = new SuccessResponseMessage(null, i_crm);
  170. rm.DATA.Add(BLWording.REL, iRel);
  171. } while (false);
  172. return rm;
  173. });
  174. }
  175. catch (Exception ex)
  176. {
  177. sMsg = Util.GetLastExceptionMsg(ex);
  178. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), @"角色管理", @"Update(角色管理(修改))", @"", @"", @"");
  179. }
  180. finally
  181. {
  182. if (null != sMsg)
  183. {
  184. rm = new ErrorResponseMessage(sMsg, i_crm);
  185. }
  186. }
  187. return rm;
  188. }
  189. #endregion 角色管理(修改)
  190. #region 角色管理(刪除)
  191. /// <summary>
  192. /// 角色管理(刪除)
  193. /// </summary>
  194. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  195. /// <returns></returns>
  196. public ResponseMessage Delete(RequestMessage i_crm)
  197. {
  198. ResponseMessage rm = null;
  199. string sMsg = null;
  200. try
  201. {
  202. rm = SugarBase.ExecTran(db =>
  203. {
  204. do
  205. {
  206. var sRuleID = _fetchString(i_crm, @"RuleID");
  207. var iRel = db.Updateable<OTB_SYS_Rules>()
  208. .UpdateColumns(x => new OTB_SYS_Rules { DelStatus = "Y" })
  209. .Where(x => x.OrgID == i_crm.ORIGID && x.RuleID == sRuleID).ExecuteCommand();
  210. iRel += db.Deleteable<OTB_SYS_MembersToRule>().Where(x => x.OrgID == i_crm.ORIGID && x.RuleID == sRuleID).ExecuteCommand();
  211. rm = new SuccessResponseMessage(null, i_crm);
  212. rm.DATA.Add(BLWording.REL, iRel);
  213. } while (false);
  214. return rm;
  215. });
  216. }
  217. catch (Exception ex)
  218. {
  219. sMsg = Util.GetLastExceptionMsg(ex);
  220. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(RoleMaintain_UpdService), @"角色管理", @"Delete(角色管理(刪除))", @"", @"", @"");
  221. }
  222. finally
  223. {
  224. if (null != sMsg)
  225. {
  226. rm = new ErrorResponseMessage(sMsg, i_crm);
  227. }
  228. }
  229. return rm;
  230. }
  231. #endregion 角色管理(刪除)
  232. }
  233. }