using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Nancy.Json;
using Spire.Pdf.General.Render.Decode.Jpeg2000.Icc;
using static Learun.Application.OA.NewsEntity;
using Nancy.Helpers;
using System.Configuration;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Learun.Application.OA
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.04.17
/// 描 述:新闻管理
///
public class NewsService : RepositoryFactory
{
#region 获取数据
///
/// 新闻列表
///
/// 分页参数
/// 类型
/// 关键词
///
public IEnumerable GetPageList(Pagination pagination, string keyword)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT * FROM LR_OA_News t WHERE t.F_TypeId = 1 ");
if (!string.IsNullOrEmpty(keyword))
{
strSql.Append(" AND F_FullHead like @keyword");
}
return this.BaseRepository().FindList(strSql.ToString(), new { keyword = "%" + keyword + "%" }, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public IEnumerable GetAboutSchool()
{
try
{
return this.BaseRepository().FindList(a => a.F_Category == "关于学院");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public IEnumerable GetNews()
{
try
{
return this.BaseRepository().FindList(a => a.F_Category == "新闻公告");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 新闻公告实体
///
/// 主键值
///
public NewsEntity GetEntity(string keyValue)
{
try
{
return this.BaseRepository().FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除
///
/// 主键
public void DeleteEntity(string keyValue)
{
try
{
NewsEntity entity = new NewsEntity()
{
F_NewsId = keyValue,
};
this.BaseRepository().Delete(entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存(新增、修改)
///
/// 主键值
/// 新闻公告实体
///
public void SaveEntity(string keyValue, NewsEntity newsEntity)
{
try
{
newsEntity.F_TypeId = 1;
if (!string.IsNullOrEmpty(keyValue))
{
newsEntity.Modify(keyValue);
this.BaseRepository().Update(newsEntity);
}
else
{
newsEntity.Create();
this.BaseRepository().Insert(newsEntity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
public void ChangeStatusByProcessId(string parameterProcessId, int status)
{
try
{
var newEntity = this.BaseRepository().FindEntity(a => a.F_ProgressId == parameterProcessId);
if (null != newEntity)
{
if (status == 2)
{
newEntity.F_Status = "2";
newEntity.F_EnabledMark = 1;
}
else
{
newEntity.F_Status = "0";
}
}
this.BaseRepository().Update(newEntity);
if (newEntity.IsSend == "1" && status == 2)
{
BaseRepository()
.ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'准备开始run task') ");
Task.Run(async () => { sendNew(newEntity); });
}
}
catch (Exception ex)
{
BaseRepository()
.ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'run task 报错"+ex.Message+"') ");
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public void sendNew(NewsEntity newEntity)
{
try
{
if (newEntity.IsSend == "1") //判断是否下发
{
#region 获取配置文件
string siteId = ConfigurationManager.AppSettings["SiteId"];
string channelId = ConfigurationManager.AppSettings["ChannelId"];
string ApiKey = ConfigurationManager.AppSettings["ApiKey"];
string ports = ConfigurationManager.AppSettings["Ports"];
#endregion
BaseRepository()
.ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'准备开始组装实体') ");
SemdNewList SendNew = new SemdNewList
{
Title = newEntity.F_FullHead,
SubTitle = newEntity.F_BriefHead,
Content = HttpUtility.HtmlDecode(newEntity.F_NewsContent),
Author = newEntity.F_AuthorName,
Source = newEntity.F_SourceName,
AddDate = DateTime.Now,
Tags = newEntity.F_TagWord,
AddUserName = newEntity.F_CreateUserName,
};
WebHeaderCollection ApiId = new WebHeaderCollection
{
{ "X-SS-API-KEY", ApiKey }
};
BaseRepository()
.ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'准备开始post cms') ");
string responses = HttpMethods.HttpPosts("http://" + ports + "/api/v1/contents/" + siteId + "/" + channelId, SendNew.ToJson(), ApiId);
BaseRepository()
.ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'"+HttpUtility.UrlEncode(responses)+"') ");
//#region 修改审核状态
//string Nid = JsonConvert.DeserializeObject(responses).value.id;
//AuditList AList = new AuditList();
//if (Nid != null)
//{
// AList.siteId = Convert.ToInt32(siteId);
// List listCon = new List();
// //这里应该循环,如果多个
// contents con = new contents();
// con.channelId = Convert.ToInt32(channelId);
// con.id = Convert.ToInt32(Nid);
// listCon.Add(con);
// AList.contents = listCon;
// //AList.reasons = "终审通过";
// HttpMethods.HttpPosts("http://" + ports + "/api/v1/contents/check", AList.ToJson(), ApiId);
//}
//#endregion
}
}
catch (Exception ex)
{
BaseRepository()
.ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'"+ex.Message + "') ");
}
}
}
//public class Root
//{
// public RootValue value { get; set; }
//}
//public class RootValue
//{
// public string id { get; set; }
//}
}