using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CP.Cache;
using CP.Dapper;
using CP.Model;
using CP.Model.ZiXun;
using CP.Model.ZiXun.Entity;
using CP.Model.ZiXun.Query;
using Dapper;
namespace CP.Business.ZiXun
{
public class NewManager : DbHelper
{
/// 缓存
///
static WMCache cache = WMCache.GetCacheService();
///
/// 获取新闻,资讯数据
///
///
///
public List GetNewManagerList(NewManagerTopQuery query)
{
var list = new List();
query.NewsTypeList.ForEach(p =>
{
string key = "NewManagerList" + (int)p;
var data = cache.GetObject>(key);
if (data == null)
{
var listAll = cache.GetObject>(CacheKeys.NewManagerListList);
if (listAll == null)
{
listAll = GetAll(query.TopNumber);
cache.AddObject(CacheKeys.NewManagerListList, listAll, (int)CacheTime.NewManager);
}
data = listAll.Where(q => q.menuId == (int)p).ToList().OrderByDescending(q => q.createTime).ToList();
cache.AddObject(key, data, (int)CacheTime.NewManager);
}
list = list.Concat(data).ToList();
});
return list;
}
private List GetAll(int topNumber)
{
var sql = string.Empty;
DynamicParameters Parameters = new DynamicParameters();
FieldInfo[] enumFields = typeof(NewManagerTypeEnum).GetFields(); //获取字段信息对象集合
var typeList = new List();
//遍历集合
foreach (FieldInfo field in enumFields)
{
if (!field.IsSpecialName)
{
typeList.Add(Convert.ToInt32(field.GetRawConstantValue()));
}
}
typeList.ForEach(p =>
{
sql += $"SELECT * FROM (SELECT TOP {topNumber} * FROM newManagerModels WHERE menuId = @menuId{typeList.IndexOf(p)} ORDER BY createTime DESC ) tem ";
Parameters.Add("menuId" + typeList.IndexOf(p), p);
if (typeList.IndexOf(p) != typeList.Count - 1)
sql += " UNION ";
});
return db.FindList(sql, Parameters).ToList();
}
///
/// 得到公告
///
///
public NewList GetNewList()
{
string key = CacheKeys.NewList;
var data = cache.GetObject(key);
if (data == null || data.ID <= 0)
{
string sql = $"" +
$"SELECT TOP 1 [ID],[title],[description],[menuId],[createTime],[createUserName] FROM [dbo].[newManagerModels] " +
$"WHERE menuId = @menuId AND isDelete = 0 ORDER BY createTime DESC ";
var list = db.FindList(sql, new { menuId = 108 }).ToList();
data = list.Count > 0 ? list[0] : new NewList();
cache.AddObject(key, data, (int)CacheTime.NewManager);
}
return data;
}
}
}