TmtnPushMsgToController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using Castle.Core.Internal;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using Ropin.Inspection.Api.Common;
  6. using Ropin.Inspection.Api.Controllers;
  7. using Ropin.Inspection.Common.Helper;
  8. using Ropin.Inspection.Model;
  9. using Ropin.Inspection.Service;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Net.Http;
  14. using System.Threading.Tasks;
  15. namespace Ropin.Inspection.Api
  16. {
  17. public class TmtnPushMsgToController : BaseController
  18. {
  19. public ILogger<TmtnPushMsgToController> _logger { get; }
  20. private readonly ITmtnPushMsgToService _TmtnPushMsgToService;
  21. private readonly IHttpClientFactory _httpClientFactory;
  22. /// <summary>
  23. /// 构造函数
  24. /// </summary>
  25. /// <param name="TmtnPushMsgToService"></param>
  26. /// <param name="logger"></param>
  27. /// <param name="httpClientFactory"></param>
  28. public TmtnPushMsgToController(ITmtnPushMsgToService TmtnPushMsgToService, IHttpClientFactory httpClientFactory, ILogger<TmtnPushMsgToController> logger)
  29. {
  30. _TmtnPushMsgToService = TmtnPushMsgToService;
  31. _logger = logger;
  32. _httpClientFactory = httpClientFactory;
  33. }
  34. /// <summary>
  35. /// 通过id获取推送消息配置信息
  36. /// </summary>
  37. /// <param name="id"></param>
  38. /// <returns></returns>
  39. [HttpGet("GetPushMsgToAsync/{id}")]
  40. public async Task<ApiResult> GetPushMsgToAsync(string id)
  41. {
  42. if (string.IsNullOrEmpty(id))
  43. {
  44. return new ApiResult(ReturnCode.GeneralError);
  45. }
  46. try
  47. {
  48. var content = await _TmtnPushMsgToService.GetConditionAsync(new TmtnPushMsgToSearchModel { C_ID = id });
  49. return new ApiResult<TmtnPushMsgToViewModel>(content.FirstOrDefault());
  50. }
  51. catch (Exception ex)
  52. {
  53. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  54. }
  55. }
  56. /// <summary>
  57. /// 获取所有推送消息配置
  58. /// </summary>
  59. /// <returns></returns>
  60. [HttpGet("GetPushMsgTosAsync")]
  61. public async Task<ApiResult> GetPushMsgTosAsync()
  62. {
  63. try
  64. {
  65. var contentList = await _TmtnPushMsgToService.GetAllAsync();
  66. return new ApiResult<IEnumerable<TmtnPushMsgToViewModel>>(contentList);
  67. }
  68. catch (Exception ex)
  69. {
  70. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  71. }
  72. }
  73. /// <summary>
  74. /// 通过推送消息配置名称条件查询
  75. /// </summary>
  76. /// <param name="searchModel"></param>
  77. /// <returns></returns>
  78. [HttpPost("GetPushMsgTosByAsync")]
  79. public async Task<ApiResult> GetPushMsgTosByAsync(TmtnPushMsgToSearchModel searchModel)
  80. {
  81. if (searchModel == null)
  82. {
  83. return new ApiResult(ReturnCode.ArgsError);
  84. }
  85. searchModel.IsPagination = false;
  86. try
  87. {
  88. var contentList = await _TmtnPushMsgToService.GetConditionAsync(searchModel);
  89. return new ApiResult<PagesModel<TmtnPushMsgToViewModel>>(new PagesModel<TmtnPushMsgToViewModel>(contentList, searchModel));
  90. }
  91. catch (Exception ex)
  92. {
  93. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  94. }
  95. }
  96. /// <summary>
  97. /// 通过设备ID获取消息分配人员信息
  98. /// </summary>
  99. /// <param name="devStoreCode"></param>
  100. /// <returns></returns>
  101. [HttpGet("GetPushMsgToUsersByAsync/{devStoreCode}")]
  102. public async Task<ApiResult> GetPushMsgToUsersByAsync(string devStoreCode)
  103. {
  104. if (string.IsNullOrEmpty(devStoreCode))
  105. {
  106. return new ApiResult(ReturnCode.GeneralError);
  107. }
  108. try
  109. {
  110. var contentList = await _TmtnPushMsgToService.GetPushMsgToUsersByAsync(devStoreCode);
  111. return new ApiResult<IEnumerable<TmtnPushMsgToUserViewModel>>(contentList);
  112. }
  113. catch (Exception ex)
  114. {
  115. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  116. }
  117. }
  118. /// <summary>
  119. /// 通过设备ID和人员ID获取消息分配人员信息
  120. /// </summary>
  121. /// <param name="devStoreCode"></param>
  122. /// <param name="userCode"></param>
  123. /// <returns></returns>
  124. [HttpGet("GetPushMsgToUsersByAsync/{devStoreCode}/{userCode}")]
  125. public async Task<ApiResult> GetPushMsgTosByAsync(string devStoreCode, string userCode)
  126. {
  127. if (string.IsNullOrEmpty(devStoreCode))
  128. {
  129. return new ApiResult(ReturnCode.GeneralError);
  130. }
  131. try
  132. {
  133. var content = await _TmtnPushMsgToService.GetPushMsgTosByAsync(devStoreCode, userCode);
  134. return new ApiResult<TmtnUserPushMsgToViewModel>(content);
  135. }
  136. catch (Exception ex)
  137. {
  138. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  139. }
  140. }
  141. /// <summary>
  142. /// 创建推送消息配置
  143. /// </summary>
  144. /// <param name="content"></param>
  145. /// <returns></returns>
  146. [HttpPost("CreatePushMsgToAsync")]
  147. public async Task<ApiResult> CreatePushMsgToAsync(TmtnPushMsgToViewModel content)
  148. {
  149. if (content == null)
  150. {
  151. return new ApiResult(ReturnCode.ArgsError);
  152. }
  153. try
  154. {
  155. await _TmtnPushMsgToService.CreateOneAsync(content);
  156. }
  157. catch (Exception ex)
  158. {
  159. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  160. }
  161. return new ApiResult(ReturnCode.Success);
  162. }
  163. /// <summary>
  164. /// 创建推送消息数组配置
  165. /// </summary>
  166. /// <param name="contentList"></param>
  167. /// <returns></returns>
  168. [HttpPost("CreatePushMsgToListAsync")]
  169. public async Task<ApiResult> CreatePushMsgToListAsync(IEnumerable<TmtnPushMsgToViewModel> contentList)
  170. {
  171. if (contentList == null ||string.IsNullOrEmpty(contentList.FirstOrDefault().C_DevStoreCode))
  172. {
  173. return new ApiResult(ReturnCode.ArgsError);
  174. }
  175. try
  176. {
  177. await _TmtnPushMsgToService.CreatePushMsgToListAsync(contentList);
  178. }
  179. catch (Exception ex)
  180. {
  181. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  182. }
  183. return new ApiResult(ReturnCode.Success);
  184. }
  185. /// <summary>
  186. /// 单用户消息数组配置
  187. /// </summary>
  188. /// <param name="content"></param>
  189. /// <returns></returns>
  190. [HttpPost("CreatePushMsgToHaveListAsync")]
  191. public async Task<ApiResult> CreatePushMsgToHaveListAsync(TmtnPushMsgToListCreateModel content)
  192. {
  193. if (content == null || string.IsNullOrEmpty(content.C_DevStoreCode) || string.IsNullOrEmpty(content.C_PushPersonCode))
  194. {
  195. return new ApiResult(ReturnCode.ArgsError);
  196. }
  197. try
  198. {
  199. await _TmtnPushMsgToService.CreatePushMsgToHaveListAsync(content);
  200. }
  201. catch (Exception ex)
  202. {
  203. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  204. }
  205. return new ApiResult(ReturnCode.Success);
  206. }
  207. /// <summary>
  208. /// 删除推送消息配置
  209. /// </summary>
  210. /// <param name="id"></param>
  211. /// <returns></returns>
  212. [HttpDelete("DeletePushMsgToAsync/{id}")]
  213. public async Task<ApiResult> DeletePushMsgToAsync(string id)
  214. {
  215. if (string.IsNullOrEmpty(id))
  216. {
  217. return new ApiResult(ReturnCode.GeneralError);
  218. }
  219. try
  220. {
  221. await _TmtnPushMsgToService.DeleteAsync(id);
  222. }
  223. catch (Exception ex)
  224. {
  225. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  226. }
  227. return new ApiResult(ReturnCode.Success);
  228. }
  229. /// <summary>
  230. /// 删除推送消息配置根据【C_DevStoreCode】和【C_PushPersonCode】
  231. /// </summary>
  232. /// <param name="model"></param>
  233. /// <returns></returns>
  234. [HttpPost("DeleteByDevPersonIdAsync")]
  235. public async Task<ApiResult> DeleteByDevPersonIdAsync(TmtnPushMsgToListDelete model)
  236. {
  237. if (model==null|| model.pushPersonList==null || model.pushPersonList.Count==0)
  238. {
  239. return new ApiResult(ReturnCode.ArgsError);
  240. }
  241. try
  242. {
  243. bool resulu= await _TmtnPushMsgToService.DeleteByDevPersonIdAsync(model);
  244. if (resulu)
  245. {
  246. return new ApiResult(ReturnCode.Success);
  247. }
  248. else
  249. {
  250. return new ApiResult(ReturnCode.GeneralError);
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  256. }
  257. }
  258. /// <summary>
  259. /// 更新推送消息配置
  260. /// </summary>
  261. /// <param name="id"></param>
  262. /// <param name="updateModel"></param>
  263. /// <returns></returns>
  264. [HttpPut("UpdatePushMsgToAsync/{id}")]
  265. public async Task<ApiResult> UpdatePushMsgToAsync(string id, TmtnPushMsgToUpdateModel updateModel)
  266. {
  267. if (string.IsNullOrEmpty(id))
  268. {
  269. return new ApiResult(ReturnCode.GeneralError);
  270. }
  271. try
  272. {
  273. await _TmtnPushMsgToService.UpdateAsync(id, updateModel);
  274. }
  275. catch (Exception ex)
  276. {
  277. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  278. }
  279. return new ApiResult(ReturnCode.Success);
  280. }
  281. /// <summary>
  282. /// 测试推送消息
  283. /// </summary>
  284. /// <returns></returns>
  285. [HttpPost("PushMessageAsync/{userWechatId}/{msg}")]
  286. [AllowAnonymous]
  287. public async Task<ApiResult> PushMessageAsync(string userWechatId, string msg)
  288. {
  289. try
  290. {
  291. var user = await _TmtnPushMsgToService.GetConditionAsync(new TmtnPushMsgToSearchModel { C_DevStoreCode = "",C_PushTypeCode = "" });
  292. var content = new
  293. {
  294. thing2 = new { value = "niu" ?? "" },
  295. time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm") },
  296. thing5 = new { value = "异常" ?? "" },
  297. thing6 = new { value = "大门损坏" ?? "" },
  298. thing9 = new { value = msg ?? "" }
  299. };
  300. new WeChatHelper(_httpClientFactory).PushMessageToUser(new List<string>() { userWechatId }, content);
  301. return new ApiResult(ReturnCode.Success);
  302. }
  303. catch (Exception ex)
  304. {
  305. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  306. }
  307. }
  308. /// <summary>
  309. /// 测试推送消息
  310. /// </summary>
  311. /// <returns></returns>
  312. [HttpGet("PushMsgTestAsync")]
  313. [AllowAnonymous]
  314. public ApiResult PushMsgTestAsync()
  315. {
  316. try
  317. {
  318. //EmailHelper.SendEmail("154817501@qq.com", "环保数字","测试主题", "测试推送消息");
  319. _TmtnPushMsgToService.PushAlarmMsgAsync(new TpushMsgModel
  320. {
  321. C_DevStoreCode = "f59ce998-6fd7-4f19-99eb-4fe567433d48",
  322. C_MsgTypeCode = "MSG_TYPE_001",
  323. Msg = "压力表要维修",
  324. Subject = "压力表维修通知"
  325. });
  326. return new ApiResult(ReturnCode.Success, "chenggong");
  327. }
  328. catch (Exception ex)
  329. {
  330. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  331. }
  332. }
  333. }
  334. }