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.

276 lines
10 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using SqlSugar;
  4. using SqlSugar.Base;
  5. using System;
  6. namespace EasyBL.WEBAPP
  7. {
  8. public class TicketAuthManageService : ServiceBase
  9. {
  10. #region 授權管理(分頁查詢)
  11. /// <summary>
  12. /// 授權管理(分頁查詢)
  13. /// </summary>
  14. /// <param name="i_crm"></param>
  15. /// <returns></returns>
  16. public ResponseMessage QueryPage(RequestMessage i_crm)
  17. {
  18. ResponseMessage rm = null;
  19. string sMsg = null;
  20. var db = SugarBase.GetIntance();
  21. try
  22. {
  23. do
  24. {
  25. var pml = new PageModel
  26. {
  27. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  28. PageSize = _fetchInt(i_crm, @"pageSize")
  29. };
  30. var iPageCount = 0;
  31. var sSortField = _fetchString(i_crm, @"sortField");
  32. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  33. var sUserId = _fetchString(i_crm, @"UserId");
  34. var sUserName = _fetchString(i_crm, @"UserName");
  35. var sIsVerify = _fetchString(i_crm, @"IsVerify");
  36. var bExcel = _fetchBool(i_crm, @"Excel");
  37. pml.DataList = db.Queryable<OTB_SYS_TicketAuth, OTB_SYS_Organization>((t1, t2) => new object[] {
  38. JoinType.Left, t1.OrgID == t2.OrgID
  39. })
  40. .Where((t1, t2) => (t1.OrgID == i_crm.ORIGID || !SqlFunc.HasValue(t1.OrgID)) && sIsVerify.Contains(t1.IsVerify))
  41. .WhereIF(!string.IsNullOrEmpty(sUserId), (t1, t2) => t1.UserID == sUserId)
  42. .WhereIF(!string.IsNullOrEmpty(sUserName), (t1, t2) => t1.UserName == sUserName)
  43. .OrderBy(sSortField, sSortOrder)
  44. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  45. pml.Total = iPageCount;
  46. rm = new SuccessResponseMessage(null, i_crm);
  47. if (bExcel)
  48. {
  49. }
  50. else
  51. {
  52. rm.DATA.Add(BLWording.REL, pml);
  53. }
  54. } while (false);
  55. }
  56. catch (Exception ex)
  57. {
  58. sMsg = Util.GetLastExceptionMsg(ex);
  59. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TicketAuthManageService), "", "QueryPage(授權管理(分頁查詢))", "", "", "");
  60. }
  61. finally
  62. {
  63. if (null != sMsg)
  64. {
  65. rm = new ErrorResponseMessage(sMsg, i_crm);
  66. }
  67. }
  68. return rm;
  69. }
  70. #endregion 授權管理(分頁查詢)
  71. #region 授權管理(新增)
  72. /// <summary>
  73. /// 授權管理(新增)
  74. /// </summary>
  75. /// <param name="i_crm">todo: describe i_crm parameter on InsertImportCustomers</param>
  76. /// <returns></returns>
  77. public ResponseMessage GridInsert(RequestMessage i_crm)
  78. {
  79. ResponseMessage rm = null;
  80. string sMsg = null;
  81. var db = SugarBase.GetIntance();
  82. try
  83. {
  84. do
  85. {
  86. var oEntity = _fetchEntity<OTB_SYS_TicketAuth>(i_crm);
  87. _setEntityBase(oEntity, i_crm);
  88. oEntity.Token = WebApi.Common.SignExtension.CreateToken();
  89. oEntity.OrgID = "";
  90. oEntity.LoginIp = @"";
  91. oEntity.IsVerify = @"N";
  92. oEntity.CreateTime = DateTime.Now;
  93. oEntity.ExpireTime = DateTime.Now.AddDays(2); //兩天过期
  94. var oTicket = db.Queryable<OTB_SYS_TicketAuth>().Single(x => !SqlFunc.HasValue(x.OrgID) && x.UserID == oEntity.UserID);
  95. if (oTicket != null)
  96. {
  97. sMsg = @"當前帳號已被使用";
  98. break;
  99. }
  100. var iRel = db.Insertable(oEntity).ExecuteCommand();
  101. if (iRel <= 0)
  102. {
  103. sMsg = @"新增失敗";
  104. break;
  105. }
  106. rm = new SuccessResponseMessage(null, i_crm);
  107. rm.DATA.Add(BLWording.REL, iRel);
  108. } while (false);
  109. }
  110. catch (Exception ex)
  111. {
  112. sMsg = Util.GetLastExceptionMsg(ex);
  113. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TicketAuthManageService), @"授權管理", @"Add(授權管理(新增))", @"", @"", @"");
  114. }
  115. finally
  116. {
  117. if (null != sMsg)
  118. {
  119. rm = new ErrorResponseMessage(sMsg, i_crm);
  120. }
  121. }
  122. return rm;
  123. }
  124. #endregion 授權管理(新增)
  125. #region 授權管理(修改)
  126. /// <summary>
  127. /// 授權管理(修改)
  128. /// </summary>
  129. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  130. /// <returns></returns>
  131. public ResponseMessage GridUpdate(RequestMessage i_crm)
  132. {
  133. ResponseMessage rm = null;
  134. string sMsg = null;
  135. var db = SugarBase.GetIntance();
  136. try
  137. {
  138. do
  139. {
  140. var oEntity = _fetchEntity<OTB_SYS_TicketAuth>(i_crm);
  141. _setEntityBase(oEntity, i_crm);
  142. var iRel = db.Updateable(oEntity)
  143. .UpdateColumns(x => new
  144. {
  145. x.UserID,
  146. x.UserName,
  147. }).ExecuteCommand();
  148. rm = new SuccessResponseMessage(null, i_crm);
  149. rm.DATA.Add(BLWording.REL, iRel);
  150. } while (false);
  151. }
  152. catch (Exception ex)
  153. {
  154. sMsg = Util.GetLastExceptionMsg(ex);
  155. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TicketAuthManageService), @"授權管理", @"Update(授權管理(修改))", @"", @"", @"");
  156. }
  157. finally
  158. {
  159. if (null != sMsg)
  160. {
  161. rm = new ErrorResponseMessage(sMsg, i_crm);
  162. }
  163. }
  164. return rm;
  165. }
  166. #endregion 授權管理(修改)
  167. #region 授權管理(刪除)
  168. /// <summary>
  169. /// 授權管理(刪除)
  170. /// </summary>
  171. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  172. /// <returns></returns>
  173. public ResponseMessage GridDelete(RequestMessage i_crm)
  174. {
  175. ResponseMessage rm = null;
  176. string sMsg = null;
  177. var db = SugarBase.GetIntance();
  178. try
  179. {
  180. do
  181. {
  182. var iNO = _fetchInt(i_crm, @"NO");
  183. var iRel = db.Deleteable<OTB_SYS_TicketAuth>().Where(x => x.NO == iNO).ExecuteCommand();
  184. rm = new SuccessResponseMessage(null, i_crm);
  185. rm.DATA.Add(BLWording.REL, iRel);
  186. } while (false);
  187. }
  188. catch (Exception ex)
  189. {
  190. sMsg = Util.GetLastExceptionMsg(ex);
  191. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TicketAuthManageService), @"授權管理", @"Delete(授權管理(刪除))", @"", @"", @"");
  192. }
  193. finally
  194. {
  195. if (null != sMsg)
  196. {
  197. rm = new ErrorResponseMessage(sMsg, i_crm);
  198. }
  199. }
  200. return rm;
  201. }
  202. #endregion 授權管理(刪除)
  203. #region 重新產生Token和簽名
  204. /// <summary>
  205. /// 函式名稱:ReSetToken
  206. /// 函式說明:重新產生Token和簽名
  207. /// </summary>
  208. /// <param name="i_crm">todo: describe i_crm parameter on ReSetToken</param>
  209. /// <returns>
  210. /// 回傳 rm(Object)
  211. ///</returns>
  212. public ResponseMessage ReSetToken(RequestMessage i_crm)
  213. {
  214. ResponseMessage rm = null;
  215. string sError = null;
  216. var db = SugarBase.GetIntance();
  217. try
  218. {
  219. do
  220. {
  221. var sNO = _fetchString(i_crm, @"NO");
  222. var ticket = new OTB_SYS_TicketAuth
  223. {
  224. Token = WebApi.Common.SignExtension.CreateToken(),
  225. NO = int.Parse(sNO)
  226. };
  227. var iRel = db.Updateable(ticket).UpdateColumns(it => new { it.Token })
  228. .Where(it => it.NO == ticket.NO).ExecuteCommand();
  229. if (iRel <= 0)
  230. {
  231. sError = @"重新產生Token和簽名失敗";
  232. break;
  233. }
  234. rm = new SuccessResponseMessage(null, i_crm);
  235. } while (false);
  236. }
  237. catch (Exception ex)
  238. {
  239. sError = Util.GetLastExceptionMsg(ex);
  240. LogAndSendEmail(sError + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TicketAuthManageService), nameof(ReSetToken), @"ReSetToken(重新產生Token和簽名)", @"", @"", @"");
  241. }
  242. finally
  243. {
  244. if (null != sError)
  245. {
  246. rm = new ErrorResponseMessage(sError, i_crm);
  247. }
  248. }
  249. return rm;
  250. }
  251. #endregion 重新產生Token和簽名
  252. }
  253. }