using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Text;
namespace Learun.Application.OA
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.04.17
/// 描 述:公告管理
///
public class NoticeService : RepositoryFactory
{
#region 获取数据
///
/// 公告列表
///
/// 分页参数
/// 关键词
///
public IEnumerable GetPageList(Pagination pagination, string keyword)
{
try
{
var user = LoginUserInfo.Get();
var strSql = new StringBuilder();
strSql.Append("SELECT * FROM LR_OA_News t WHERE t.F_TypeId = 2 and F_Status<>'-1' and F_DeleteMark=0");
if (!string.IsNullOrEmpty(keyword))
{
strSql.Append(" AND F_FullHead like @keyword");
}
if (user.Description != "超级管理员")
{
strSql.Append(" AND F_CreateUserName ='" + user.realName + "'");
}
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 GetPageListRevert(Pagination pagination, string keyword)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT * FROM LR_OA_News t WHERE t.F_TypeId = 2 and F_Status='-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 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)
{
var db = this.BaseRepository().BeginTrans();
try
{
var list = keyValue.Split(',');
foreach (var item in list)
{
var entity = db.FindEntity(x => x.F_NewsId == item);
if (entity != null)
{
db.Delete(entity);
}
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public void RecycleForm(string keyValue, string status)
{
var db = this.BaseRepository().BeginTrans();
try
{
var list = keyValue.Split(',');
foreach (var item in list)
{
var entity = db.FindEntity(x => x.F_NewsId == item);
if (entity != null)
{
entity.F_Status = status;
db.Update(entity);
}
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存(新增、修改)
///
/// 主键值
/// 新闻公告实体
///
public void SaveEntity(string keyValue, NewsEntity newsEntity)
{
try
{
newsEntity.F_TypeId = 2;
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
#region 扩展数据
///
/// 公告列表
///
/// 关键词
///
public IEnumerable GetList(string keyword, string categoryId = null)
{
try
{
var userinfo = LoginUserInfo.Get();
var userId = userinfo.userId;
var deptId = userinfo.departmentId;
var postIds = userinfo.postIds;
var strSql = new StringBuilder();
strSql.Append("SELECT t.*,r.RNewsId,r.RTime FROM LR_OA_News t ");
strSql.Append(" left join LR_OA_NewsRead r on t.F_NewsId = r.NewsId and r.RUserId=@userId ");
strSql.Append(" WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1 ");
strSql.Append($@" and (
((t.F_SendDeptId is null or len(t.F_SendDeptId)=0) and (t.F_SendPostId is null or len(t.F_SendPostId)=0))
");
if (!string.IsNullOrEmpty(deptId))
{
strSql.Append($" or (t.F_SendDeptId is not null and t.F_SendDeptId like '%{deptId}%')");
}
if (!string.IsNullOrEmpty(postIds))
{
strSql.Append(" or (t.F_SendPostId is not null and ");
if (postIds.Contains(","))
{
string postidSql = " (";
foreach (var postId in postIds)
{
postidSql += $" t.F_SendPostId like '%{postId}%' or";
}
postidSql = postidSql.Substring(0, postidSql.LastIndexOf("or")) + ")";
strSql.Append(postidSql);
}
else
{
strSql.Append($" t.F_SendPostId like '%{postIds}%'");
}
strSql.Append(")");
}
strSql.Append(") ");
if (!string.IsNullOrEmpty(categoryId))
{
strSql.Append($" AND t.F_CategoryId = '{categoryId}'");
}
if (!string.IsNullOrEmpty(keyword))
{
strSql.Append(" AND t.F_FullHead like @keyword");
}
strSql.Append(" ORDER BY t.F_ReleaseTime DESC ");
return this.BaseRepository().FindList(strSql.ToString(), new { keyword = "%" + keyword + "%", userId = userId });
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
public void ChangeStatusById(string keyValue, int status, string processId)
{
try
{
BaseRepository().ExecuteBySql($"UPDATE dbo.LR_OA_News SET F_Status='{status}',F_ProgressId='{processId}' WHERE F_NewsId='{keyValue}'", null);
}
catch (Exception ex)
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
public NewsEntity GetEntityByProcessId(string processId)
{
try
{
return this.BaseRepository().FindEntity(t => t.F_ProgressId == processId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
}
}