using DirectService.Tools;
using JLHHJSvr.BLL;
using JLHHJSvr.Com.Model;
using LJLib;
using LJLib.DAL.SQL;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
namespace JLHHJSvr.Helper
{
internal sealed class CacheHelper : HelperBase
{
#region 基础资料Mapping
///
/// 弹簧资料
///
public class SpringMapping : ICacheMapping
{
public u_spring GetInstance(SqlCommand cmd, int springid)
{
var instance = new u_spring() { springid = springid };
DbSqlHelper.SelectOne(cmd, instance, "line_diameter,gram_weight,height,center_diameter,caliber,cyclenum,roll_width,roll_length,arrangement_width,arrangement_height,springtypeid");
return instance;
}
}
///
/// 部门资料
///
public class DeptMapping : ICacheMapping
{
public u_dept GetInstance(SqlCommand cmd, int deptid)
{
var instance = new u_dept() { deptid = deptid };
DbSqlHelper.SelectOne(cmd, instance, "pricelistid,profitrate,moneyrate,discount,taxes_rate,managerate,com_profitrate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate");
return instance;
}
}
#endregion
#region 获取数据
private static readonly Dictionary _cacheServiceRegistry = new Dictionary();
public TEntity GetData(object key)
where TEntity : class, new()
where TMapping : new()
{
var mappingType = typeof(TMapping);
var iface = mappingType.GetInterfaces()
.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICacheMapping<,>));
if (iface == null) throw new InvalidOperationException($"{mappingType.Name} must implement ICacheMapping");
var genericArgs = iface.GetGenericArguments();
var keyType = genericArgs[0];
// 找到私有的强类型实现方法并用 MakeGenericMethod 构造
var method = typeof(CacheHelper).GetMethod(nameof(GetDataInternal), BindingFlags.Instance | BindingFlags.NonPublic);
var gm = method.MakeGenericMethod(keyType, typeof(TEntity), mappingType);
// 调用并返回(Invoke 返回 object)
return (TEntity) gm.Invoke(this, new object[] { key });
}
private TValue GetDataInternal(object key)
where TMapping : ICacheMapping, new()
where TValue : class, new()
{
var mapping = new TMapping();
return mapping.GetInstance(cmd, (TKey)key);
}
#endregion
}
}