using Castle.Core.Internal; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Api.Controllers; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Service; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; namespace Ropin.Inspection.Api { public class TmtnPushMsgToController : BaseController { public ILogger _logger { get; } private readonly ITmtnPushMsgToService _TmtnPushMsgToService; private readonly IHttpClientFactory _httpClientFactory; /// /// 构造函数 /// /// /// /// public TmtnPushMsgToController(ITmtnPushMsgToService TmtnPushMsgToService, IHttpClientFactory httpClientFactory, ILogger logger) { _TmtnPushMsgToService = TmtnPushMsgToService; _logger = logger; _httpClientFactory = httpClientFactory; } /// /// 通过id获取推送消息配置信息 /// /// /// [HttpGet("GetPushMsgToAsync/{id}")] public async Task GetPushMsgToAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TmtnPushMsgToService.GetConditionAsync(new TmtnPushMsgToSearchModel { C_ID = id }); return new ApiResult(content.FirstOrDefault()); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 获取所有推送消息配置 /// /// [HttpGet("GetPushMsgTosAsync")] public async Task GetPushMsgTosAsync() { try { var contentList = await _TmtnPushMsgToService.GetAllAsync(); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过推送消息配置名称条件查询 /// /// /// [HttpPost("GetPushMsgTosByAsync")] public async Task GetPushMsgTosByAsync(TmtnPushMsgToSearchModel searchModel) { if (searchModel == null) { return new ApiResult(ReturnCode.ArgsError); } searchModel.IsPagination = false; try { var contentList = await _TmtnPushMsgToService.GetConditionAsync(searchModel); return new ApiResult>(new PagesModel(contentList, searchModel)); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过设备ID获取消息分配人员信息 /// /// /// [HttpGet("GetPushMsgToUsersByAsync/{devStoreCode}")] public async Task GetPushMsgToUsersByAsync(string devStoreCode) { if (string.IsNullOrEmpty(devStoreCode)) { return new ApiResult(ReturnCode.GeneralError); } try { var contentList = await _TmtnPushMsgToService.GetPushMsgToUsersByAsync(devStoreCode); return new ApiResult>(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 通过设备ID和人员ID获取消息分配人员信息 /// /// /// /// [HttpGet("GetPushMsgToUsersByAsync/{devStoreCode}/{userCode}")] public async Task GetPushMsgTosByAsync(string devStoreCode, string userCode) { if (string.IsNullOrEmpty(devStoreCode)) { return new ApiResult(ReturnCode.GeneralError); } try { var content = await _TmtnPushMsgToService.GetPushMsgTosByAsync(devStoreCode, userCode); return new ApiResult(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 创建推送消息配置 /// /// /// [HttpPost("CreatePushMsgToAsync")] public async Task CreatePushMsgToAsync(TmtnPushMsgToViewModel content) { if (content == null) { return new ApiResult(ReturnCode.ArgsError); } try { await _TmtnPushMsgToService.CreateOneAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 创建推送消息数组配置 /// /// /// [HttpPost("CreatePushMsgToListAsync")] public async Task CreatePushMsgToListAsync(IEnumerable contentList) { if (contentList == null ||string.IsNullOrEmpty(contentList.FirstOrDefault().C_DevStoreCode)) { return new ApiResult(ReturnCode.ArgsError); } try { await _TmtnPushMsgToService.CreatePushMsgToListAsync(contentList); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 单用户消息数组配置 /// /// /// [HttpPost("CreatePushMsgToHaveListAsync")] public async Task CreatePushMsgToHaveListAsync(TmtnPushMsgToListCreateModel content) { if (content == null || string.IsNullOrEmpty(content.C_DevStoreCode) || string.IsNullOrEmpty(content.C_PushPersonCode)) { return new ApiResult(ReturnCode.ArgsError); } try { await _TmtnPushMsgToService.CreatePushMsgToHaveListAsync(content); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除推送消息配置 /// /// /// [HttpDelete("DeletePushMsgToAsync/{id}")] public async Task DeletePushMsgToAsync(string id) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TmtnPushMsgToService.DeleteAsync(id); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 删除推送消息配置根据【C_DevStoreCode】和【C_PushPersonCode】 /// /// /// [HttpPost("DeleteByDevPersonIdAsync")] public async Task DeleteByDevPersonIdAsync(TmtnPushMsgToListDelete model) { if (model==null|| model.pushPersonList==null || model.pushPersonList.Count==0) { return new ApiResult(ReturnCode.ArgsError); } try { bool resulu= await _TmtnPushMsgToService.DeleteByDevPersonIdAsync(model); if (resulu) { return new ApiResult(ReturnCode.Success); } else { return new ApiResult(ReturnCode.GeneralError); } } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 更新推送消息配置 /// /// /// /// [HttpPut("UpdatePushMsgToAsync/{id}")] public async Task UpdatePushMsgToAsync(string id, TmtnPushMsgToUpdateModel updateModel) { if (string.IsNullOrEmpty(id)) { return new ApiResult(ReturnCode.GeneralError); } try { await _TmtnPushMsgToService.UpdateAsync(id, updateModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 测试推送消息 /// /// [HttpPost("PushMessageAsync/{userWechatId}/{msg}")] [AllowAnonymous] public async Task PushMessageAsync(string userWechatId, string msg) { try { var user = await _TmtnPushMsgToService.GetConditionAsync(new TmtnPushMsgToSearchModel { C_DevStoreCode = "",C_PushTypeCode = "" }); var content = new { thing2 = new { value = "niu" ?? "" }, time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm") }, thing5 = new { value = "异常" ?? "" }, thing6 = new { value = "大门损坏" ?? "" }, thing9 = new { value = msg ?? "" } }; new WeChatHelper(_httpClientFactory).PushMessageToUser(new List() { userWechatId }, content); return new ApiResult(ReturnCode.Success); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 测试推送消息 /// /// [HttpGet("PushMsgTestAsync")] [AllowAnonymous] public ApiResult PushMsgTestAsync() { try { //EmailHelper.SendEmail("154817501@qq.com", "环保数字","测试主题", "测试推送消息"); _TmtnPushMsgToService.PushAlarmMsgAsync(new TpushMsgModel { C_DevStoreCode = "f59ce998-6fd7-4f19-99eb-4fe567433d48", C_MsgTypeCode = "MSG_TYPE_001", Msg = "压力表要维修", Subject = "压力表维修通知" }); return new ApiResult(ReturnCode.Success, "chenggong"); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } } }