using Nancy;
using Learun.Util;
using System.Collections.Generic;
using Learun.Application.TwoDevelopment.EducationalAdministration;
using System.Linq;
using Learun.Application.Organization;
using System;
using System.Configuration;
using System.Threading.Tasks;
using System.Web;
using Learun.Application.Base.AuthorizeModule;
using Learun.Application.TwoDevelopment.LR_Desktop;
using Learun.Application.TwoDevelopment.PersonnelManagement;
using Learun.Util.Operat;
using Microsoft.AspNet.SignalR.Client;
using Newtonsoft.Json;
namespace Learun.Application.WebApi
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创 建:超级管理员
/// 日 期:2019-06-17 16:28
/// 描 述:日志发送
///
public class MpManagementApi : BaseNoLoginApi
{
private WeChatConfigIBLL weChatConfigIbll = new WeChatConfigBLL();
private WeChatTempletIBLL weChatTempletIbll = new WeChatTempletBLL();
private MP_ManagementPlanIBLL mP_ManagementPlanIBLL = new MP_ManagementPlanBLL();
private RoleIBLL roleIBLL = new RoleBLL();
private UserRelationIBLL userRelationIBLL = new UserRelationBLL();
private UserIBLL userIBLL = new UserBLL();
private Sys_InformationPushIBLL sys_InformationPushIBLL = new Sys_InformationPushBLL();
///
/// 注册接口
///
public MpManagementApi()
: base("/mpManagement")
{
Get["/pushWeixin"] = PushWeixin;
}
public Response PushWeixin(dynamic _)
{
//获取所有到期的内控计划
var mpList = mP_ManagementPlanIBLL.GetMP_ManageMentPlanExpire();
var RoleName = Config.GetValue("MPRoleName");
var roleId = roleIBLL.GetIdByRoleName(RoleName);
var roleUserList = userRelationIBLL.GetUserIdList(roleId).Select(a => a.F_UserId).ToList();
foreach (var mpEntity in mpList)
{
//已经上传了的人员
var hasUploadUsers = mP_ManagementPlanIBLL.GetListByParentId(mpEntity.MPId).Select(a => a.MPUploader).ToList();
var departmentList = mpEntity.MPDepartment.Split(',').ToList();
var UserList = new List();
var title = "";
foreach (var departmentId in departmentList)
{
var entityList = userIBLL.GetUserByDepartmentId(departmentId).Where(a => roleUserList.Contains(a.F_UserId) && !hasUploadUsers.Contains(a.F_UserId)).ToList();
if (entityList.Count() > 0)
{
foreach (var item in entityList)
{
UserList.Add(item);
}
}
}
if (mpEntity.MPType == 0)
{
title = "内控检查";
//读取信息推送管理-内控管理推送(06)的配置
var informationPushEntity = sys_InformationPushIBLL.GetEntityByPushItem("06");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
//飞星推送
FeiXinPush(title, "nk", UserList);
}
}
else if (mpEntity.MPType == 1)
{
title = "绩效跟踪";
//读取信息推送管理-内控管理推送(06)的配置
var informationPushEntity = sys_InformationPushIBLL.GetEntityByPushItem("06");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
//飞星推送
FeiXinPush(title, "jx", UserList);
}
}
else
{
title = "质量目标";
//读取信息推送管理-内控管理推送(06)的配置
var informationPushEntity = sys_InformationPushIBLL.GetEntityByPushItem("06");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
//飞星推送
FeiXinPush(title, "zl", UserList);
}
}
}
return Success("推送成功");
}
public void FeiXinPush(string title, string code, List UserList)
{
//飞星推送
Task.Run(async () =>
{
using (var hubConnection = new HubConnection(ConfigurationManager.AppSettings["CommunicationServeraddress"]))
{
var hubProxy = hubConnection.CreateHubProxy("SignalRHub");
await hubConnection.Start();
await hubProxy.Invoke("PushAnnouncement", "", title + "材料上传", title, code, string.Join(",", UserList.Select(m => m.F_UserId)), "");
}
});
OperateLogModel operateLogModel = new OperateLogModel();
operateLogModel.title = title;
operateLogModel.type = OperationType.Other;
operateLogModel.url = "NoticeController";
operateLogModel.sourceObjectId = "002";
operateLogModel.sourceContentJson = title;
OperatorHelper.Instance.WriteOperateLog(operateLogModel);
}
public void WeiXinPush(List userList, string title)
{
var WeChatConfigentity = weChatConfigIbll.GetEnableEntity();
string appid = WeChatConfigentity.APPId;
string secret = WeChatConfigentity.secret;
var wechatemplete = weChatTempletIbll.GetWeChatTemplateEntityByCodeConfigId(WeChatConfigentity.ID, "task");
string weixintaskurl = wechatemplete.TUrl;
string weixintasktempid = wechatemplete.TempId;
var responsejson = Util.HttpMethods.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
foreach (UserEntity userinfo in userList)
{
if (userinfo != null && !string.IsNullOrEmpty(userinfo.OpenIdForWeixin))
{
//执行推送任务
if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && !string.IsNullOrEmpty(weixintaskurl) && !string.IsNullOrEmpty(weixintasktempid))
{
if (!string.IsNullOrEmpty(responsejson))
{
var weixintokenobj = JsonConvert.DeserializeObject(responsejson);
if (string.IsNullOrEmpty(weixintokenobj.errcode))
{
string access_token = weixintokenobj.access_token;
string jsondata = "{\"touser\":\"" + userinfo.OpenIdForWeixin + "\"," +
"\"template_id\":\"" + weixintasktempid + "\"," +
"\"url\":\"" + weixintaskurl + "\"," +
"\"data\":{" +
"\"first\": {\"value\":\"您有" + title + "需要上传\",\"color\":\"#173177\"}," +
"\"keyword1\":{\"value\":\"内控管理\",\"color\":\"#173177\"}," +
"\"keyword2\": {\"value\":\"" + title + "\",\"color\":\"#173177\"}," +
"\"keyword3\": {\"value\":\"待上传\",\"color\":\"#173177\"}," +
"\"keyword4\": {\"value\":\"您有【" + title + "】需要上传\",\"color\":\"#173177\"}" +
"}" +
"}";
string pushresult = Util.HttpMethods.HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, jsondata);
}
}
}
}
}
}
#region 私有类
///
/// 表单实体类
///
private class ReqFormEntity
{
public string keyValue { get; set; }
public string strEntity { get; set; }
}
#endregion
}
}