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.

201 lines
8.1 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using SqlSugar.Base;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace EasyBL.WEBAPP
  7. {
  8. public class CalendarService : ServiceBase
  9. {
  10. #region 抓去行事曆資料
  11. /// <summary>
  12. /// 函式名稱:GetList
  13. /// 函式說明:抓去行事曆資料
  14. /// </summary>
  15. /// <param name="i_crm">todo: describe i_crm parameter on GetList</param>
  16. /// <returns>
  17. /// 回傳 rm(Object)
  18. ///</returns>
  19. public ResponseMessage GetList(RequestMessage i_crm)
  20. {
  21. ResponseMessage rm = null;
  22. string sMsg = null;
  23. var db = SugarBase.GetIntance();
  24. try
  25. {
  26. do
  27. {
  28. //HttpContext hc = HttpContext.Current;
  29. //string outlook_userName = ClaimsPrincipal.Current.FindFirst("name").Value;
  30. var sStartDate = _fetchString(i_crm, @"StartDate");
  31. var sEndDate = _fetchString(i_crm, @"EndDate");
  32. var sCalType = _fetchString(i_crm, @"CalType");
  33. var sOpenMent = _fetchString(i_crm, @"OpenMent");
  34. var rStartDate = Convert.ToDateTime(sStartDate);
  35. var rEndDate = Convert.ToDateTime(sEndDate);
  36. const string sSQL = @"SELECT DISTINCT MemberID+',' FROM dbo.OTB_SYS_Members WHERE DepartmentID IN (SELECT * FROM [dbo].[OFN_SYS_GetParentDepartmentIdByUserID](@OrgID,@UserID)) FOR XML PATH('')";
  37. var dic_pm = new Dictionary<string, string>
  38. {
  39. { @"OrgID", i_crm.ORIGID },
  40. { @"UserID", i_crm.USERID }
  41. };
  42. var sParentDeptUsers = db.Ado.GetString(sSQL, dic_pm);
  43. var saCalendar = db.Queryable<OTB_SYS_Calendar>()
  44. .OrderBy(x => x.StartDate)
  45. .Where(x => x.OrgID == i_crm.ORIGID && x.StartDate.Date >= rStartDate.Date && x.EndDate.Date <= rEndDate.Date)
  46. .Where(x => x.UserID == i_crm.USERID
  47. || (x.OpenMent == @"G" && x.GroupMembers.Contains(i_crm.USERID))
  48. || (x.OpenMent == @"D" && sParentDeptUsers.Contains(x.UserID))
  49. || x.OpenMent == @"C")
  50. .Where(x => sCalType.Contains(x.CalType))
  51. .Where(x => sOpenMent.Contains(x.OpenMent))
  52. .Where(x => !x.DelStatus)
  53. .ToList();
  54. rm = new SuccessResponseMessage(null, i_crm);
  55. rm.DATA.Add(BLWording.REL, saCalendar);
  56. } while (false);
  57. }
  58. catch (Exception ex)
  59. {
  60. sMsg = Util.GetLastExceptionMsg(ex);
  61. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(CalendarService), @"行事曆", @"GetList(抓去行事曆資料)", @"", @"", @"");
  62. }
  63. finally
  64. {
  65. if (null != sMsg)
  66. {
  67. rm = new ErrorResponseMessage(sMsg, i_crm);
  68. }
  69. }
  70. return rm;
  71. }
  72. #endregion 抓去行事曆資料
  73. #region 拆入一條曆資料
  74. /// <summary>
  75. /// 函式名稱:GetList
  76. /// 函式說明:拆入一條曆資料
  77. /// </summary>
  78. /// <param name="i_crm">todo: describe i_crm parameter on Add</param>
  79. /// <returns>
  80. /// 回傳 rm(Object)
  81. ///</returns>
  82. public ResponseMessage Add(RequestMessage i_crm)
  83. {
  84. ResponseMessage rm = null;
  85. string sMsg = null;
  86. var db = SugarBase.GetIntance();
  87. try
  88. {
  89. do
  90. {
  91. var sCalType = _fetchString(i_crm, @"CalType");
  92. var UserID = _fetchString(i_crm, @"UserID") ?? i_crm.USERID;
  93. var sTitle = _fetchString(i_crm, @"Title");
  94. var sDescription = _fetchString(i_crm, @"Description");
  95. var sStartDate = _fetchString(i_crm, @"StartDate");
  96. var sEndDate = _fetchString(i_crm, @"EndDate");
  97. var sImportment = _fetchString(i_crm, @"Importment");
  98. var sColor = _fetchString(i_crm, @"Color");
  99. var sAllDay = _fetchString(i_crm, @"AllDay");
  100. var sOpenMent = _fetchString(i_crm, @"OpenMent");
  101. var sGroupMembers = _fetchString(i_crm, @"GroupMembers");
  102. var sRelationId = _fetchString(i_crm, @"RelationId");
  103. var sUrl = _fetchString(i_crm, @"Url");
  104. var sMemo = _fetchString(i_crm, @"Memo") ?? "";
  105. var oCalendar = new OTB_SYS_Calendar
  106. {
  107. OrgID = i_crm.ORIGID,
  108. UserID = UserID,
  109. CalType = sCalType,
  110. Title = sTitle,
  111. Description = sDescription,
  112. StartDate = Convert.ToDateTime(sStartDate),
  113. EndDate = Convert.ToDateTime(sEndDate),
  114. Importment = sImportment,
  115. Color = sColor,
  116. AllDay = sAllDay == @"1",
  117. OpenMent = sOpenMent,
  118. GroupMembers = sGroupMembers,
  119. Url = sUrl,
  120. RelationId = sRelationId,
  121. Editable = null,
  122. ClassName = null,
  123. Memo = sMemo,
  124. CreateUser = i_crm.USERID,
  125. CreateDate = DateTime.Now,
  126. ModifyUser = i_crm.USERID,
  127. ModifyDate = DateTime.Now,
  128. DelStatus = false
  129. };
  130. var sNo = db.Insertable<OTB_SYS_Calendar>(oCalendar).ExecuteReturnBigIdentity();
  131. rm = new SuccessResponseMessage(null, i_crm);
  132. rm.DATA.Add(BLWording.REL, sNo);
  133. } while (false);
  134. }
  135. catch (Exception ex)
  136. {
  137. sMsg = Util.GetLastExceptionMsg(ex);
  138. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(CalendarService), @"行事曆", @"Add(拆入一條曆資料)", @"", @"", @"");
  139. }
  140. finally
  141. {
  142. if (null != sMsg)
  143. {
  144. rm = new ErrorResponseMessage(sMsg, i_crm);
  145. }
  146. }
  147. return rm;
  148. }
  149. #endregion 拆入一條曆資料
  150. #region 刪除行事曆
  151. /// <summary>
  152. /// 刪除行事曆
  153. /// </summary>
  154. /// <param name="relationId"></param>
  155. public void DeleteCalendar(string orgID, string userID, string relationId)
  156. {
  157. var sMsg = string.Empty;
  158. string fnName = nameof(DeleteCalendar);
  159. var db = SugarBase.GetIntance();
  160. try
  161. {
  162. do
  163. {
  164. var oTB_SYS_Calendars = db.Queryable<OTB_SYS_Calendar>()
  165. .Where( x => x.OrgID == orgID)
  166. .WhereIF(!string.IsNullOrEmpty(userID), x => x.UserID == userID)
  167. .WhereIF(!string.IsNullOrEmpty(relationId), x => x.RelationId == relationId)
  168. .ToList();
  169. oTB_SYS_Calendars.ForEach(x => { x.DelStatus = true; });
  170. if (oTB_SYS_Calendars.Count > 0)
  171. {
  172. db.Updateable(oTB_SYS_Calendars).ExecuteCommand();
  173. }
  174. } while (false);
  175. }
  176. catch (Exception ex)
  177. {
  178. sMsg = Util.GetLastExceptionMsg(ex);
  179. LogAndSendEmail(sMsg + @"Param:relationId = " + relationId, ex, orgID, userID, nameof(CalendarService), @"行事曆", fnName, @"", @"", @"");
  180. }
  181. }
  182. #endregion
  183. }
  184. }