using Dapper;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.EducationalAdministration;
using Learun.Application.TwoDevelopment.LR_Desktop;
using Learun.DataBase.Repository;
using Learun.Util;
using Microsoft.AspNet.SignalR.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Learun.Application.TwoDevelopment.PersonnelManagement
{
///
/// 版 本 Learun-ADMS V7.0.6 力软敏捷开发框架
/// Copyright (c) 2013-2020 力软信息技术(苏州)有限公司
/// 创 建:超级管理员
/// 日 期:2021-02-21 10:07
/// 描 述:会议管理
///
public class MeetingManagementService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表分页数据
///
/// 分页参数
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.*,r.Name as ConferenceRoomName ");
strSql.Append(" FROM MeetingManagement t left join ConferenceRoom r on t.MeetingPlace=r.ID ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["MeetingTitle"].IsEmpty())
{
dp.Add("MeetingTitle", "%" + queryParam["MeetingTitle"].ToString() + "%", DbType.String);
strSql.Append(" AND t.MeetingTitle Like @MeetingTitle ");
}
if (!queryParam["MeetingPlace"].IsEmpty())
{
dp.Add("MeetingPlace", queryParam["MeetingPlace"].ToString(), DbType.String);
strSql.Append(" AND t.MeetingPlace = @MeetingPlace ");
}
if (!queryParam["CreateUser"].IsEmpty())
{
dp.Add("CreateUser", queryParam["CreateUser"].ToString(), DbType.String);
strSql.Append(" AND t.CreateUser = @CreateUser ");
}
if (!queryParam["InternalParticipants"].IsEmpty())
{
dp.Add("InternalParticipants", "%" + queryParam["InternalParticipants"].ToString() + "%", DbType.String);
strSql.Append(" AND t.InternalParticipants Like @InternalParticipants ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取页面显示列表数据
///
/// 查询参数
///
public IEnumerable GetList(string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.*");
strSql.Append(" FROM MeetingManagement t ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["MeetingTitle"].IsEmpty())
{
dp.Add("MeetingTitle", "%" + queryParam["MeetingTitle"].ToString() + "%", DbType.String);
strSql.Append(" AND t.MeetingTitle Like @MeetingTitle ");
}
if (!queryParam["MeetingPlace"].IsEmpty())
{
dp.Add("MeetingPlace", queryParam["MeetingPlace"].ToString(), DbType.String);
strSql.Append(" AND t.MeetingPlace = @MeetingPlace ");
}
if (!queryParam["CreateUser"].IsEmpty())
{
dp.Add("CreateUser", queryParam["CreateUser"].ToString(), DbType.String);
strSql.Append(" AND t.CreateUser = @CreateUser ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取MeetingManagement表实体数据
/// 主键
///
///
public MeetingManagementEntity GetMeetingManagementEntity(string keyValue)
{
try
{
var data = this.BaseRepository("CollegeMIS").FindEntity(keyValue);
if (data != null)
{
data.ConferenceRoomName = this.BaseRepository("CollegeMIS").FindEntity(data.MeetingPlace).Name;
}
return data;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取MeetingManagement表实体数据
/// 主键
///
///
public MeetingManagementEntity GetMeetingManagementEntityByProcessId(string processId)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(x => x.ProcessId == processId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除实体数据
/// 主键
///
///
public void DeleteEntity(string keyValue)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
//会议签到记录表中删除参会人员记录
db.Delete(x => x.MeetID == keyValue);
//会议工作管理表删除会议
db.Delete(t => t.Id == keyValue);
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(UserInfo userInfo, string keyValue, MeetingManagementEntity entity)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue, userInfo);
db.Update(entity);
}
else
{
entity.Create(userInfo);
db.Insert(entity);
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 审核实体数据
/// 主键
///
///
public void DoCheck(UserInfo userInfo, string keyValue, string status)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
//修改会议工作管理表的审核字段;
db.ExecuteBySql("update MeetingManagement set CheckStatus='" + status + "',CheckUser='" + userInfo.userId + "',CheckTime='" + DateTime.Now + "' where Id='" + keyValue + "' ");
if (status == "1")//审核通过:会议签到记录表中增加参会人员;
{
var entity = db.FindEntity(x => x.Id == keyValue);
if (entity != null)
{
//增加校内参入人员:
if (!string.IsNullOrEmpty(entity.InternalParticipants))
{
if (entity.InternalParticipants.IndexOf(',') == -1)
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantID = entity.InternalParticipants,
ParticipantName = this.BaseRepository().FindEntity(x => x.F_UserId == entity.InternalParticipants)?.F_RealName,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
else
{
foreach (var item in entity.InternalParticipants.Split(','))
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantID = item,
ParticipantName = this.BaseRepository().FindEntity(x => x.F_UserId == item)?.F_RealName,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
}
}
//增加校外参入人员:
if (!string.IsNullOrEmpty(entity.ExternalParticipants))
{
if (entity.ExternalParticipants.IndexOf(',') == -1)
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantName = entity.ExternalParticipants,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
else
{
foreach (var item in entity.ExternalParticipants.Split(','))
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantName = item,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
}
}
}
}
else if (status == "2") //审核不通过:会议签到记录表中删除参会人员记录;
{
db.Delete(x => x.MeetID == keyValue);
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 提交实体数据
/// 主键
///
///
public void DoSubmit(string keyValue, string status, string processId)
{
try
{
this.BaseRepository("CollegeMIS").ExecuteBySql("update MeetingManagement set CheckStatus='" + status + "',ProcessId='" + processId + "' where Id='" + keyValue + "' ");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 审核实体数据
/// 主键
///
///
public void ChangeStatusByProcessId(string processId, string status, string userId)
{
UserInfo userInfo = LoginUserInfo.Get();
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
//修改会议工作管理表的审核字段;
db.ExecuteBySql("update MeetingManagement set CheckStatus='" + status + "',CheckUser='" + userId + "',CheckTime='" + DateTime.Now + "' where ProcessId='" + processId + "' ");
var entity = db.FindEntity(x => x.ProcessId == processId);
if (entity != null)
{
if (status == "1")//审核通过:会议签到记录表中增加参会人员;
{
//增加校内参入人员:
if (!string.IsNullOrEmpty(entity.InternalParticipants))
{
if (entity.InternalParticipants.IndexOf(',') == -1)
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantID = entity.InternalParticipants,
ParticipantName = this.BaseRepository().FindEntity(x => x.F_UserId == entity.InternalParticipants)?.F_RealName,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
else
{
foreach (var item in entity.InternalParticipants.Split(','))
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantID = item,
ParticipantName = this.BaseRepository().FindEntity(x => x.F_UserId == item)?.F_RealName,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
}
}
//增加校外参入人员:
if (!string.IsNullOrEmpty(entity.ExternalParticipants))
{
if (entity.ExternalParticipants.IndexOf(',') == -1)
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantName = entity.ExternalParticipants,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
else
{
foreach (var item in entity.ExternalParticipants.Split(','))
{
var model = new MeetingSignInRecordEntity()
{
MeetID = entity.Id,
ParticipantName = item,
IsSignIn = false
};
model.Create(userInfo);
db.Insert(model);
}
}
}
}
else if (status == "2") //审核不通过:会议签到记录表中删除参会人员记录;
{
db.Delete(x => x.MeetID == entity.Id);
}
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public void ReceivedList(string processId)
{
var Received = GetMeetingManagementEntityByProcessId(processId);
List userInfos = new List();
foreach (var rid in Received.InternalParticipants.Split(','))
{
var user = this.BaseRepository().FindEntity(m => m.F_UserId == rid);
if (!userInfos.Contains(user))
{
userInfos.Add(user);
}
}
if (!string.IsNullOrEmpty(Received.RecordPerson))
{
foreach (var rid in Received.RecordPerson.Split(','))
{
var user = this.BaseRepository().FindEntity(m => m.F_UserId == rid);
if (!userInfos.Contains(user))
{
userInfos.Add(user);
}
}
}
//foreach (var uitem in userInfos)
//{
// SYS_ReceiveMessageEntity receiveMessageEntity = new SYS_ReceiveMessageEntity();
// receiveMessageEntity.Create();
// receiveMessageEntity.SENDERID = Received.SENDERID;
// receiveMessageEntity.SENDER = messageentity.SENDER;
// receiveMessageEntity.RECEIVERID = uitem.F_UserId;
// receiveMessageEntity.RECEIVER = uitem.F_RealName;
// receiveMessageEntity.TITLE = messageentity.TITLE;
// receiveMessageEntity.CONTENTS = messageentity.CONTENTS;
// receiveMessageEntity.URL = messageentity.URL;
// receiveMessageEntity.READFLAG = 0;
// receiveMessageEntity.SENDTIME = DateTime.Now;
// receiveMessageEntity.DelFlag = false;
// this.BaseRepository().Insert(receiveMessageEntity);
//}
//读取信息推送管理-会议申请推送(03)的配置
var informationPushEntity = this.BaseRepository().FindEntity(x => x.PushItem == "03");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
//微信推送
try
{
PushWeixin(userInfos, Received.MeetingTitle);
}
catch (Exception e)
{
}
//飞星推送
Task.Run(async () =>
{
using (var hubConnection = new HubConnection(ConfigurationManager.AppSettings["CommunicationServeraddress"]))
{
var hubProxy = hubConnection.CreateHubProxy("SignalRHub");
await hubConnection.Start();
await hubProxy.Invoke("PushAnnouncement", Received.InternalParticipants, Received.MeetingTitle, Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(Received.Content)).Length < 20 ? Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(Received.Content)) : Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(Received.Content)).Substring(0, 20), "mail", string.Join(",", userInfos.Select(m => m.F_UserId)), "");
}
});
}
}
public void PushWeixin(List needpostuserlist, string title)
{
var WeChatConfigentity = BaseRepository().FindEntity(m => m.IsEnable == true);
string appid = WeChatConfigentity.APPId;
string secret = WeChatConfigentity.secret;
var wechatemplete = BaseRepository()
.FindEntity(m => m.WeID == WeChatConfigentity.ID && m.TCode == "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 needpostuserlist)
{
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\":\"您有新的会议消息\",\"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);
}
}
}
}
}
}
#endregion
}
}