using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using Opsoft.Data;
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Domain.Enums.ReportCodes;
using Opsoft.NetCore.O3App.Domain.Interface;
using Opsoft.NetCore.O3App.Domain.UnitOfWork.IPadReport.IRepositories;
using Opsoft.NetCore.O3App.Domain.UnitOfWork.IPadReport.IServices;
using Opsoft.NetCore.O3App.Domain.UnitOfWork.IPadReport.Model;
using Opsoft.NetCore.O3App.Shared;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Opsoft.NetCore.O3App.Services.IPadReport
{
///
/// 汇报信息查询服务层实现
///
[MapTo(typeof(IReportInfoLoadService), ServiceLifetime.Transient)]
public class ReportInfoLoadService : ServiceBase, IReportInfoLoadService
{
private IMemoryCache _cache;
///
/// 带注入的构造函数
///
///
public ReportInfoLoadService(IReportInfoLoadRepository repository, IMemoryCache memoryCache) : base(repository)
{
Init(repository);
_cache = memoryCache;
}
///
/// 按工作中心代码获取资源信息
///
///
///
public async Task> QueryResourceByWorkCenter(Dictionary paramaters)
{
//工作中心代码集合
if (!paramaters.ContainsKey("data"))
{
throw new O3AppException("【工作中心】传参信息错误,未定义", 400);
}
return await (repository as IReportInfoLoadRepository).QueryResourceByWorkCenter(paramaters);
}
///
/// 依据班组=>员工信息/员工信息=>班组
///
///
///
public async Task> GetTeamEmpTwo(Dictionary paramaters)
{
#region 参数验证
//获取数据的类型
if (!paramaters.ContainsKey("basicType"))
{
throw new O3AppException("【数据类型】传参信息错误,未定义", 400);
}
//判断数据类型
if (!int.TryParse(paramaters["basicType"].ToStringEx(), out int basicType))
{
throw new O3AppException("【数据类型】传参类型错误,值设定有误", 400);
}
//判断手机ID是否传参
if (!paramaters.ContainsKey("basicNO"))
{
throw new O3AppException("【代码】传参信息错误,未定义", 400);
}
if (paramaters["basicNO"].IsNullOrEmpty())
{
throw new O3AppException("【代码】信息为空", 400);
}
#endregion
return await (repository as IReportInfoLoadRepository).GetTeamEmpTwo(paramaters);
}
///
/// 加载历史汇报信息
///
///
///
public async Task> ReloadReportHisReport(Dictionary paramaters)
{
#region 参数验证
//获取数据的类型
if (!paramaters.ContainsKey("basicType"))
{
throw new O3AppException("【数据类型】传参信息错误,未定义", 400);
}
//判断数据类型
if (!int.TryParse(paramaters["basicType"], out int basicType))
{
throw new O3AppException("【数据类型】传参类型错误,值设定有误", 400);
}
//判断手机ID是否传参
if (!paramaters.ContainsKey("phoneID"))
{
throw new O3AppException("【手机ID】传参信息错误,未定义", 400);
}
if (paramaters["phoneID"].IsNullOrEmpty())
{
throw new O3AppException("【手机ID】信息为空", 400);
}
//资源信息验证
if (!paramaters.ContainsKey("data"))
{
throw new O3AppException("【资源】传参信息错误,未定义", 400);
}
#endregion
#region 数据获取
//缓存名称
string cacheName = LoginInfo.DBAias + paramaters["phoneID"];
//选择的资源列表
List resourceList = JsonOperator.JsonDeserialize>(paramaters["data"]);
//需要返回的查询数据
List listPro = new List();
//汇报大类
ReportData reportData = null;
//数据获取方式
switch (basicType)
{
case 1:
#region 从数据库加载汇报信息
try
{
List reportList = await (repository as IReportInfoLoadRepository).GetReportDataFromDB(paramaters["phoneID"], string.Empty);
if (reportList.Count > 0)
{
//缓存做更新
if (_cache.TryGetValue(cacheName, out reportData))
{
reportData.AllReport.Clear();
reportData.AllReport.AddRange(reportList);
_cache.Remove(cacheName);
}
else
{
reportData = new ReportData(paramaters["phoneID"], LoginInfo.UserID, DateTime.Now);
reportData.AllReport.AddRange(reportList);
}
//汇报的数据添加进缓存 默认添加60分钟的缓存
_cache.Set(cacheName, reportData, TimeSpan.FromMinutes(60));
}
else
{
//没有查到数据时,不在需要提示
//throw new O3AppException("当前设备汇报信息为空", 2);
reportData = new ReportData(paramaters["phoneID"], LoginInfo.UserID, DateTime.Now);
}
}
catch (Exception ex)
{
throw ex;
}
break;
#endregion
case 2:
#region 从缓存加载汇报信息
if (!_cache.TryGetValue(cacheName, out reportData))
{
throw new O3AppException("当前设备缓存信息为空,请刷新界面!", 2);
}
break;
#endregion
}
//过滤资源信息
if (resourceList.Count > 0)
{
listPro = (from item in reportData.AllReport
where resourceList.Contains(item.ResourcesNO)
select item).ToList();
}
else
{
//资源为空时,取出缓存中的全部汇报数据返回
listPro = reportData.AllReport;
}
#endregion
return listPro;
}
///
/// 加载历史汇报信息
/// +数据数量
///
///
///
public async Task