颐和api

ScheduleReminderController.cs 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Threading.Tasks;
  6. using MadRunFabric.Common;
  7. using MadRunFabric.Model.MessageApi;
  8. using MessageApi.IRepositories;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Logging;
  12. using MongoDB.Driver;
  13. namespace MessageApi.Controllers
  14. {
  15. [Authorize]
  16. [ApiVersion("6.0")]
  17. [Produces("application/json")]
  18. [Route("api/[controller]")]
  19. public class ScheduleReminderController : BaseController
  20. {
  21. private readonly ILogger<ScheduleReminderController> _logger;
  22. private readonly ISchedule_ReminderRepository _schedule_reminderRepository;
  23. public ScheduleReminderController(
  24. ILogger<ScheduleReminderController> logger,
  25. ISchedule_ReminderRepository schedule_reminderRepository
  26. )
  27. {
  28. _logger = logger;
  29. _schedule_reminderRepository = schedule_reminderRepository;
  30. }
  31. /// <summary>
  32. /// 添加日程提醒
  33. /// </summary>
  34. /// <param name="srdate"></param>
  35. /// <param name="content"></param>
  36. /// <returns></returns>
  37. [HttpPost("add")]
  38. public async Task<IActionResult> AddAsync(string srdate,string enddate, string content)
  39. {
  40. try
  41. {
  42. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  43. //string usercode = "8000";
  44. var model = new Schedule_Reminder();
  45. if (!string.IsNullOrEmpty(srdate))
  46. model.srdate = Convert.ToDateTime(srdate);
  47. if (!string.IsNullOrEmpty(enddate))
  48. model.enddate = Convert.ToDateTime(enddate);
  49. model.content = content;
  50. model.createuser = usercode;
  51. model.createtime = DateTime.Now;
  52. model.isdelete = 0;
  53. model.isread = 0;
  54. bool bl = await _schedule_reminderRepository.Add(model);
  55. if (bl)
  56. {
  57. return Success("添加成功");
  58. }
  59. return Error("添加失败");
  60. }
  61. catch (Exception ex)
  62. {
  63. _logger.LogError(ex, "添加日程提醒异常");
  64. return Error("添加失败");
  65. }
  66. }
  67. /// <summary>
  68. /// 修改日程提醒
  69. /// </summary>
  70. /// <param name="id"></param>
  71. /// <param name="srdate"></param>
  72. /// <param name="content"></param>
  73. /// <param name="isread"></param>
  74. /// <returns></returns>
  75. [HttpPost("update")]
  76. public async Task<IActionResult> UpdateAsync(string id,string srdate, string enddate, string content,int isread)
  77. {
  78. try
  79. {
  80. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  81. var model = await _schedule_reminderRepository.GetSingle(id);
  82. if (model == null)
  83. return Error("操作失败");
  84. if (!string.IsNullOrEmpty(srdate))
  85. model.srdate = Convert.ToDateTime(srdate);
  86. if (!string.IsNullOrEmpty(enddate))
  87. model.enddate = Convert.ToDateTime(enddate);
  88. if (!string.IsNullOrEmpty(content))
  89. {
  90. model.content = content;
  91. }
  92. if (isread > 0)
  93. {
  94. model.isread = isread;
  95. }
  96. bool b = await _schedule_reminderRepository.UpdateOne(model);
  97. if (b)
  98. {
  99. return Success("保存成功");
  100. }
  101. return Error("保存失败");
  102. }
  103. catch (Exception ex)
  104. {
  105. _logger.LogError(ex, "修改日程提醒异常");
  106. return Error("保存失败");
  107. }
  108. }
  109. /// <summary>
  110. /// 删除信息 by ids
  111. /// </summary>
  112. /// <param name="ids"></param>
  113. /// <returns></returns>
  114. [HttpPost("delete")]
  115. public async Task<IActionResult> RemoveAsync(string[] ids)
  116. {
  117. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  118. var res = 0;
  119. if (ids != null && ids.Length > 0)
  120. {
  121. foreach (var item in ids)
  122. {
  123. var ml = await _schedule_reminderRepository.GetSingle(item);
  124. ml.isdelete = 1;
  125. ml.deleteuser = usercode;
  126. ml.deletetime = DateTime.Now.ToLocalTime();
  127. if (_schedule_reminderRepository.UpdateOne(ml).Result)
  128. res += 1;
  129. }
  130. if (res == ids.Length)
  131. return Success("删除成功");
  132. else if (res > 0 && res < ids.Length)
  133. return Error("部分删除失败,请查看后重新操作");
  134. else
  135. return Error("删除失败,请查看后重新操作");
  136. }
  137. else
  138. return Error("请选择要删除的记录");
  139. }
  140. /// <summary>
  141. /// 日程提醒详情 by id
  142. /// </summary>
  143. /// <param name="id">id</param>
  144. /// <returns></returns>
  145. [HttpGet("getdetailes")]
  146. public async Task<IActionResult> GetDetailsAsync(string id)
  147. {
  148. if (string.IsNullOrEmpty(id))
  149. return Error("参数错误");
  150. var model = await _schedule_reminderRepository.GetSingle(id);
  151. // 处理已读
  152. if (model != null) {
  153. if (model.isread != 1)
  154. {
  155. model.isread = 1;
  156. await _schedule_reminderRepository.UpdateOne(model);
  157. }
  158. }
  159. return Success("获取成功!", model);
  160. }
  161. /// <summary>
  162. /// 获取日程提醒列表
  163. /// </summary>
  164. /// <param name="keyword"></param>
  165. /// <param name="pageindex"></param>
  166. /// <param name="pagesize"></param>
  167. /// <returns></returns>
  168. [HttpGet("getlistbypage")]
  169. public async Task<IActionResult> GetListsByPageAsync(string stime, string etime, int isread=-1, int pageindex = 1, int pagesize = 10)
  170. {
  171. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  172. ////排序字段
  173. var sort = Builders<Schedule_Reminder>.Sort.Ascending("srdate");
  174. //根据条件查询集合
  175. var listApp = new List<FilterDefinition<Schedule_Reminder>>();
  176. listApp.Add(Builders<Schedule_Reminder>.Filter.Eq("isdelete", 0));
  177. listApp.Add(Builders<Schedule_Reminder>.Filter.Eq("createuser",usercode));
  178. if (!string.IsNullOrWhiteSpace(stime))
  179. {
  180. DateTime dt2 = new DateTime();
  181. if (DateTime.TryParse(stime.Trim() + " 00:00:00", out dt2))
  182. listApp.Add(Builders<Schedule_Reminder>.Filter.Gt("srdate", stime));
  183. }
  184. if (!string.IsNullOrWhiteSpace(etime))
  185. {
  186. DateTime dt2 = new DateTime();
  187. if (DateTime.TryParse(etime.Trim() + " 23:59:59", out dt2))
  188. listApp.Add(Builders<Schedule_Reminder>.Filter.Lt("srdate", etime));
  189. }
  190. if (isread >= 0)
  191. listApp.Add(Builders<Schedule_Reminder>.Filter.Eq("isread", isread));
  192. int recordCount = 0;
  193. var filter = Builders<Schedule_Reminder>.Filter.And(listApp);
  194. var list = await _schedule_reminderRepository.GetByPage(filter, pageindex, pagesize, sort);
  195. var redCount = await _schedule_reminderRepository.CountAsync(filter); //获取总数
  196. recordCount = int.Parse(redCount.ToString());
  197. var obj = new
  198. {
  199. rows = list,
  200. total = recordCount
  201. };
  202. return Success("成功", obj);
  203. }
  204. /// <summary>
  205. /// 获取到期日程提醒
  206. /// </summary>
  207. /// <returns></returns>
  208. [HttpGet("gettxlist")]
  209. public async Task<IActionResult> GetTXListsAsync()
  210. {
  211. try
  212. {
  213. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  214. //string usercode = "8000";
  215. ////排序字段
  216. var sort = Builders<Schedule_Reminder>.Sort.Ascending("srdate");
  217. //根据条件查询集合
  218. var flist = new List<FilterDefinition<Schedule_Reminder>>();
  219. flist.Add(Builders<Schedule_Reminder>.Filter.Eq("isdelete", false));
  220. flist.Add(Builders<Schedule_Reminder>.Filter.Where(s => s.createuser.Equals(usercode)));
  221. flist.Add(Builders<Schedule_Reminder>.Filter.Lt("srdate", DateTime .Now ));
  222. var filter = Builders<Schedule_Reminder>.Filter.And(flist);
  223. var vlist = await _schedule_reminderRepository.Get(filter, null, sort);
  224. var obj = new
  225. {
  226. list = vlist
  227. };
  228. return Success("根据条件获取日程提醒数据成功", obj);
  229. }
  230. catch (Exception ex)
  231. {
  232. _logger.LogError(ex, "获取到期日程提醒数据异常");
  233. return Error("获取到期日程提醒数据异常");
  234. }
  235. }
  236. }
  237. }