颐和api

NoticeInfoController.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.Configuration;
  12. using Microsoft.Extensions.Logging;
  13. using MongoDB.Driver;
  14. namespace MessageApi.Controllers
  15. {
  16. /// <summary>
  17. /// 公告管理
  18. /// </summary>
  19. [Authorize]
  20. [ApiVersion("6.0")]
  21. [Produces("application/json")]
  22. [Route("api/[controller]")]
  23. public class NoticeInfoController : BaseController
  24. {
  25. private readonly ILogger<NoticeInfoController> _logger;
  26. private readonly IConfiguration _configuration;
  27. private readonly ISys_Msg_NoticeInfoRepository _sys_msg_noticeinfoRepository;
  28. private readonly ISys_User_AccountRepository _sys_user_accountRepository;
  29. private readonly ISys_Role_InfoRepository _sys_role_infoRepository;
  30. public NoticeInfoController(
  31. ILogger<NoticeInfoController> logger,
  32. IConfiguration configuration,
  33. ISys_Msg_NoticeInfoRepository sys_msg_noticeinfoRepository,
  34. ISys_User_AccountRepository sys_user_accountRepository,
  35. ISys_Role_InfoRepository sys_role_infoRepository
  36. )
  37. {
  38. _logger = logger;
  39. _configuration = configuration;
  40. _sys_msg_noticeinfoRepository = sys_msg_noticeinfoRepository;
  41. _sys_user_accountRepository = sys_user_accountRepository;
  42. _sys_role_infoRepository = sys_role_infoRepository;
  43. }
  44. /// <summary>
  45. /// 获取公告列表
  46. /// </summary>
  47. /// <param name="keyword"></param>
  48. /// <param name="pageindex"></param>
  49. /// <param name="pagesize"></param>
  50. /// <returns></returns>
  51. [HttpGet("getlistbypage")]
  52. public async Task<IActionResult> GetListsByPageAsync(string keyword, string roleid, string userid,int iscus=-1,int isread=-1, int pageindex = 1, int pagesize = 10)
  53. {
  54. ////排序字段
  55. var sort = Builders<Sys_Msg_NoticeInfo>.Sort.Descending("createon");
  56. //根据条件查询集合
  57. var listApp = new List<FilterDefinition<Sys_Msg_NoticeInfo>>();
  58. listApp.Add(Builders<Sys_Msg_NoticeInfo>.Filter.Eq("isdelete", 0));
  59. if (!string.IsNullOrEmpty(keyword))
  60. listApp.Add(Builders<Sys_Msg_NoticeInfo>.Filter.Where(s => s.title.Contains(keyword) || s.content.Contains(keyword)));
  61. if (!string.IsNullOrEmpty(roleid))
  62. listApp.Add(Builders<Sys_Msg_NoticeInfo>.Filter.Eq("roleid", roleid));
  63. if (!string.IsNullOrEmpty(userid))
  64. listApp.Add(Builders<Sys_Msg_NoticeInfo>.Filter.Eq("typeid", userid));
  65. if (iscus >-1)
  66. listApp.Add(Builders<Sys_Msg_NoticeInfo>.Filter.Eq("iscus", iscus));
  67. if (isread > -1)
  68. listApp.Add(Builders<Sys_Msg_NoticeInfo>.Filter.Eq("isread", isread));
  69. int recordCount = 0;
  70. var filter = Builders<Sys_Msg_NoticeInfo>.Filter.And(listApp);
  71. var list = await _sys_msg_noticeinfoRepository.GetByPage(filter, pageindex, pagesize, sort);
  72. var redCount = await _sys_msg_noticeinfoRepository.CountAsync(filter); //获取总数
  73. recordCount = int.Parse(redCount.ToString());
  74. var obj = new
  75. {
  76. rows = list,
  77. total = recordCount
  78. };
  79. return Success("成功", obj);
  80. }
  81. /// <summary>
  82. /// 获取公告列表 - 关联查询
  83. /// </summary>
  84. /// <param name="keyword"></param>
  85. /// <param name="pageindex"></param>
  86. /// <param name="pagesize"></param>
  87. /// <returns></returns>
  88. [HttpGet("getlistsbypage")]
  89. public IActionResult GetListByPageAsync(string keyword, string stime, string etime,int isread=-1, int pageindex = 1, int pagesize = 10)
  90. {
  91. string rolecode = User.Claims.FirstOrDefault(c => c.Type == "RoleCode").Value;
  92. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  93. int recordCount = 0;
  94. var result = _sys_msg_noticeinfoRepository.GetListByPage(keyword, rolecode, usercode, stime, etime,isread , pageindex, pagesize, out recordCount);
  95. var obj = new
  96. {
  97. rows = result,
  98. total = recordCount,
  99. };
  100. return Success("获取成功", obj);
  101. }
  102. /// <summary>
  103. /// 获取新闻详情 by id - 关联查询
  104. /// </summary>
  105. /// <param name="id"></param>
  106. /// <returns></returns>
  107. [HttpGet("getdetails")]
  108. public async Task<IActionResult> GetDetails(string id)
  109. {
  110. if (string.IsNullOrEmpty(id))
  111. return Error("参数错误");
  112. var model = await _sys_msg_noticeinfoRepository.GetSingle(id);
  113. return Success("获取成功!", model);
  114. }
  115. /// <summary>
  116. /// 添加公告
  117. /// </summary>
  118. /// <param name="title"></param>
  119. /// <param name="content"></param>
  120. /// <param name="usercodes"></param>
  121. /// <param name="rolecodes"></param>
  122. /// <returns></returns>
  123. [HttpPost("add")]
  124. public async Task<IActionResult> AddAsync(string title, string content, string startdate, string enddate, string usercodes, string rolecodes)
  125. {
  126. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  127. #region 验证判断
  128. if (string.IsNullOrEmpty(title))
  129. return Error("公告标题不能为空");
  130. if (string.IsNullOrEmpty(content))
  131. return Error("公告内容不能为空");
  132. #endregion
  133. var model = new Sys_Msg_NoticeInfo();
  134. model.title = title;
  135. model.content = content;
  136. model.usercode = usercodes;
  137. model.rolecode = rolecodes;
  138. model.title = title;
  139. model.content = content;
  140. if(!string.IsNullOrEmpty(startdate))
  141. model.startdate = Convert.ToDateTime(startdate);
  142. if (!string.IsNullOrEmpty(enddate))
  143. model.enddate = Convert.ToDateTime(enddate);
  144. model.createby = usercode;
  145. model.isdelete = 0;
  146. bool bl = await _sys_msg_noticeinfoRepository.Add(model);
  147. if (bl)
  148. {
  149. return Success("添加成功");
  150. }
  151. return Error("添加失败");
  152. }
  153. /// <summary>
  154. /// 修改公告
  155. /// </summary>
  156. /// <param name="id"></param>
  157. /// <param name="title"></param>
  158. /// <param name="content"></param>
  159. /// <param name="usercodes"></param>
  160. /// <param name="rolecodes"></param>
  161. /// <returns></returns>
  162. [HttpPost("update")]
  163. public async Task<IActionResult> UpdateAsync(string id, string title, string content, string startdate, string enddate, string usercodes, string rolecodes,int isread=-1)
  164. {
  165. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  166. #region 验证判断
  167. if (string.IsNullOrEmpty(title))
  168. return Error("公告标题不能为空");
  169. if (string.IsNullOrEmpty(content))
  170. return Error("公告内容不能为空");
  171. #endregion
  172. var model = await _sys_msg_noticeinfoRepository.GetSingle(id);
  173. if (!string.IsNullOrEmpty(title))
  174. {
  175. model.title = title;
  176. }
  177. if (!string.IsNullOrEmpty(content))
  178. {
  179. model.content = content;
  180. }
  181. if (!string.IsNullOrEmpty(startdate))
  182. {
  183. model.startdate = Convert.ToDateTime(startdate);
  184. }
  185. if (!string.IsNullOrEmpty(startdate))
  186. {
  187. model.enddate = Convert.ToDateTime(enddate);
  188. }
  189. if (!string.IsNullOrEmpty(usercodes))
  190. {
  191. model.usercode = usercodes;
  192. }
  193. if (!string.IsNullOrEmpty(rolecodes))
  194. {
  195. model.rolecode = rolecodes;
  196. }
  197. if (!string.IsNullOrEmpty(title))
  198. {
  199. model.title = title;
  200. }
  201. if (!string.IsNullOrEmpty(content))
  202. {
  203. model.content = content;
  204. }
  205. if (!string.IsNullOrEmpty(usercode))
  206. {
  207. model.createby = usercode;
  208. }
  209. model.isdelete = 0;
  210. if (isread > -1)
  211. { model.isread = isread; }
  212. bool bl = await _sys_msg_noticeinfoRepository.UpdateOne(model);
  213. if (bl)
  214. {
  215. return Success("添加成功");
  216. }
  217. return Error("添加失败");
  218. }
  219. /// <summary>
  220. /// 删除信息 by ids
  221. /// </summary>
  222. /// <param name="ids"></param>
  223. /// <returns></returns>
  224. [HttpPost("delete")]
  225. public async Task<IActionResult> RemoveAsync(string[] ids)
  226. {
  227. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  228. var res = 0;
  229. if (ids != null && ids.Length > 0)
  230. {
  231. foreach (var item in ids)
  232. {
  233. var ml = await _sys_msg_noticeinfoRepository.GetSingle(item);
  234. ml.isdelete = 1;
  235. ml.deleteuser = usercode;
  236. ml.deletetime = DateTime.Now.ToLocalTime();
  237. if (_sys_msg_noticeinfoRepository.UpdateOne(ml).Result)
  238. res += 1;
  239. }
  240. if (res == ids.Length)
  241. return Success("删除成功");
  242. else if (res > 0 && res < ids.Length)
  243. return Error("部分删除失败,请查看后重新操作");
  244. else
  245. return Error("删除失败,请查看后重新操作");
  246. }
  247. else
  248. return Error("请选择要删除的记录");
  249. }
  250. #region 获取角色和人员 - 二级
  251. /// <summary>
  252. /// 获取角色和人员列表 - 二级
  253. /// </summary>
  254. [HttpGet("getroleusertree")]
  255. public async Task<IActionResult> GetRoleUserTreeListAsync(string id)
  256. {
  257. var model = new Sys_Msg_NoticeInfo();
  258. if(!string.IsNullOrEmpty(id))
  259. model = await _sys_msg_noticeinfoRepository.GetSingle(id);
  260. var listRole = (await _sys_role_infoRepository.Get(p => p.state_flag == 1)).ToList();
  261. var listUser = (await _sys_user_accountRepository.Get(p => p.delete_flag == false)).ToList();
  262. //string
  263. //if(model!=null && model.rolecode != "")
  264. var list = listRole.Select(p => new
  265. {
  266. id = p.id,
  267. code = p.role_code,
  268. name = p.role_name,
  269. parentid = "",
  270. ischeck = (model != null && model.rolecode != null) ? (model.rolecode.Contains(p.role_code) ? true : false) : false,
  271. children = listUser.Where(x => p.id.Equals(x.role_id)).Select(x => new
  272. {
  273. id = x.id,
  274. code = x.usercode,
  275. name = x.username,
  276. parentid = p.id,
  277. ischeck = (model != null && model.usercode != null) ? (model.usercode.Contains(x.usercode) ? true : false) : false,
  278. children = new List<string> { }
  279. })
  280. });
  281. return Success("获取成功", list);
  282. }
  283. #endregion
  284. }
  285. }