颐和api

OnLineChatController.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Threading.Tasks;
  6. using Api.SignToken;
  7. using AutoMapper;
  8. using MadRunFabric.Common;
  9. using MadRunFabric.Model;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.AspNetCore.SignalR;
  13. using Microsoft.Extensions.Logging;
  14. using MongoDB.Driver;
  15. using OnLineChatApi.Class;
  16. using OnLineChatApi.IRepositories;
  17. namespace OnLineChatApi.Controllers
  18. {
  19. //[Authorize]
  20. [ApiVersion("6.0")]
  21. [Produces("application/json")]
  22. [Route("api/[controller]")]
  23. public class OnLineChatController : BaseController
  24. {
  25. private readonly ILogger<OnLineChatController> _logger;
  26. private readonly IChat_ProcessRepository _chatprocessRepository;
  27. private readonly ISignTokenService _signTokenService;
  28. private readonly IMapper _mapper;
  29. public OnLineChatController(ILogger<OnLineChatController> logger, IChat_ProcessRepository chatprocessRepository,
  30. ISignTokenService signTokenService, IMapper mapper)
  31. {
  32. _logger = logger;
  33. _chatprocessRepository = chatprocessRepository;
  34. _signTokenService = signTokenService;
  35. _mapper = mapper;
  36. }
  37. /// <summary>
  38. /// 获取在线人员
  39. /// </summary>
  40. /// <param name="type"></param>
  41. /// <returns></returns>
  42. [HttpGet("getlist")]
  43. public IActionResult GetList()
  44. {
  45. var obj = new
  46. {
  47. ChatHub.ConnectionIds,
  48. ChatHub.Servicers,
  49. ChatHub.Customers
  50. };
  51. return Success("成功", obj);
  52. }
  53. /// <summary>
  54. /// 获取步骤列表
  55. /// </summary>
  56. /// <param name="keyword"></param>
  57. /// <param name="pageindex"></param>
  58. /// <param name="pagesize"></param>
  59. /// <returns></returns>
  60. [HttpGet("getlistbypage")]
  61. public async Task<IActionResult> GetListsByPageAsync(string keyword,int pageindex = 1, int pagesize = 10)
  62. {
  63. ////排序字段
  64. var sort = Builders<Chat_Process>.Sort.Ascending("step");
  65. //根据条件查询集合
  66. var listApp = new List<FilterDefinition<Chat_Process>>();
  67. listApp.Add(Builders<Chat_Process>.Filter.Eq("isdelete", 0));
  68. if (!string.IsNullOrEmpty(keyword))
  69. listApp.Add(Builders<Chat_Process>.Filter.Where(s => s.content.Contains(keyword)));
  70. int recordCount = 0;
  71. var filter = Builders<Chat_Process>.Filter.And(listApp);
  72. var list = await _chatprocessRepository.GetByPage(filter, pageindex, pagesize, sort);
  73. var redCount = await _chatprocessRepository.CountAsync(filter); //获取总数
  74. recordCount = int.Parse(redCount.ToString());
  75. var obj = new
  76. {
  77. rows = list,
  78. total = recordCount
  79. };
  80. return Success("成功", obj);
  81. }
  82. /// <summary>
  83. /// 获取步骤列表--关联查询
  84. /// </summary>
  85. /// <param name="keyword"></param>
  86. /// <param name="pageIndex"></param>
  87. /// <param name="pageSize"></param>
  88. /// <returns></returns>
  89. [HttpGet("getlistsbypage")]
  90. public IActionResult GetListsByPage(string keyword, int pageIndex = 1, int pageSize = 10)
  91. {
  92. int recordCount = 0;
  93. var result = _chatprocessRepository.GetListsByPage(keyword, pageIndex, pageSize, out recordCount);
  94. var obj = new
  95. {
  96. rows = result,
  97. total = recordCount,
  98. };
  99. return Success("获取成功", obj);
  100. }
  101. /// <summary>
  102. /// 步骤详情 by id
  103. /// </summary>
  104. /// <param name="id">步骤id</param>
  105. /// <returns></returns>
  106. [HttpGet("getdetailes")]
  107. public async Task<IActionResult> GetDetailsAsync(string id)
  108. {
  109. if (string.IsNullOrEmpty(id))
  110. return Error("参数错误");
  111. var model = await _chatprocessRepository.GetSingle(id);
  112. return Success("获取成功!", model);
  113. }
  114. /// <summary>
  115. /// 获取步骤详情 by id - 关联查询
  116. /// </summary>
  117. /// <param name="id"></param>
  118. /// <returns></returns>
  119. [HttpGet("getdetails")]
  120. public IActionResult GetDetails(string id)
  121. {
  122. if (string.IsNullOrEmpty(id))
  123. return Error("参数错误");
  124. var model = _chatprocessRepository.GetDetails(id);
  125. if (model != null)
  126. {
  127. return Success("获取成功!", model);
  128. }
  129. return Error("获取失败");
  130. }
  131. /// <summary>
  132. /// 添加步骤信息
  133. /// </summary>
  134. /// <param name="step"></param>
  135. /// <param name="content"></param>
  136. /// <param name="dictionarycode"></param>
  137. /// <returns></returns>
  138. [HttpPost("add")]
  139. public async Task<IActionResult> AddAsync(int step, string content, int optiontype, string dictionarycode, List<string> options)
  140. {
  141. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  142. #region 验证判断
  143. if (step == 0)
  144. return Error("步骤不能为空");
  145. if (string.IsNullOrEmpty(content))
  146. return Error("步骤内容不能为空");
  147. if (optiontype == 1 && string.IsNullOrEmpty(dictionarycode))
  148. return Error("字典项不能为空");
  149. if (optiontype == 2 && options != null && options.Count == 0)
  150. return Error("选择项不能为空");
  151. long n = await _chatprocessRepository.Count(x => x.step.Equals(step) && x.isdelete == 0);
  152. if (n > 0)
  153. return Error("步骤已存在");
  154. #endregion
  155. var model = new Chat_Process();
  156. model.step = step;
  157. model.content = content;
  158. model.optiontype = optiontype;
  159. model.dictionarycode = dictionarycode;
  160. if (options != null)
  161. {
  162. model.options = options;
  163. }
  164. else
  165. {
  166. model.options = new List<string>();
  167. }
  168. model.isdelete = 0;
  169. model.createuser = usercode;
  170. model.createtime = DateTime.Now;
  171. bool bl = await _chatprocessRepository.Add(model);
  172. if (bl)
  173. {
  174. return Success("添加成功");
  175. }
  176. return Error("添加失败");
  177. }
  178. /// <summary>
  179. /// 修改步骤信息
  180. /// </summary>
  181. /// <param name="id"></param>
  182. /// <param name="step"></param>
  183. /// <param name="content"></param>
  184. /// <param name="dictionarycode"></param>
  185. /// <returns></returns>
  186. [HttpPost("update")]
  187. public async Task<IActionResult> UpdateAsync(string id, int step, string content, int optiontype, string dictionarycode, List<string> options)
  188. {
  189. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  190. #region 验证判断
  191. if (step == 0)
  192. return Error("步骤不能为空");
  193. if (string.IsNullOrEmpty(content))
  194. return Error("步骤内容不能为空");
  195. if (optiontype == 1 && string.IsNullOrEmpty(dictionarycode))
  196. return Error("字典项不能为空");
  197. if (optiontype == 2 && options != null && options.Count == 0)
  198. return Error("选择项不能为空");
  199. long n = await _chatprocessRepository.Count(x => x.step == step && x.isdelete == 0 && x.id != id);
  200. if (n > 0)
  201. return Error("步骤已存在");
  202. #endregion
  203. var model = await _chatprocessRepository.GetSingle(id);
  204. if (model == null)
  205. return Error("操作失败");
  206. model.step = step;
  207. model.optiontype = optiontype;
  208. model.content = content;
  209. model.dictionarycode = dictionarycode;
  210. if (options != null)
  211. {
  212. model.options = options;
  213. }
  214. else
  215. {
  216. model.options = new List<string>();
  217. }
  218. bool b = await _chatprocessRepository.UpdateOne(model);
  219. if (b)
  220. {
  221. return Success("保存成功");
  222. }
  223. return Error("保存失败");
  224. }
  225. /// <summary>
  226. /// 删除信息 by ids
  227. /// </summary>
  228. /// <param name="ids"></param>
  229. /// <returns></returns>
  230. [HttpPost("delete")]
  231. public async Task<IActionResult> RemoveAsync(string[] ids)
  232. {
  233. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  234. var res = 0;
  235. if (ids != null && ids.Length > 0)
  236. {
  237. foreach (var item in ids)
  238. {
  239. var ml = await _chatprocessRepository.GetSingle(item);
  240. ml.isdelete = 1;
  241. ml.deleteuser = usercode;
  242. ml.deletetime = DateTime.Now.ToLocalTime();
  243. if (_chatprocessRepository.UpdateOne(ml).Result)
  244. res += 1;
  245. }
  246. if (res == ids.Length)
  247. return Success("删除成功");
  248. else if (res > 0 && res < ids.Length)
  249. return Error("部分删除失败,请查看后重新操作");
  250. else
  251. return Error("删除失败,请查看后重新操作");
  252. }
  253. else
  254. return Error("请选择要删除的记录");
  255. }
  256. }
  257. }