(#3)添加数据库数据查询模块和数据缓存模块

This commit is contained in:
falcon 2019-03-04 17:02:09 +08:00
parent 97d3514416
commit 7391532639
9 changed files with 258 additions and 14 deletions

View File

@ -7,7 +7,7 @@ namespace Cmdjy.Bll
/// <summary>
/// 缓冲提供器工厂
/// </summary>
public class CacheFactory
public static class CacheFactory
{
/// <summary>
/// 缓冲提供器
@ -63,21 +63,23 @@ namespace Cmdjy.Bll
public RedisHelper() {
if(Connection == null) {
var connectionStr = WebSettings.RedisConnectionString;
Connection = ConnectionMultiplexer.Connect(connectionStr);
if(!string.IsNullOrEmpty(connectionStr)) Connection = ConnectionMultiplexer.Connect(connectionStr);
}
if(Connection != null && Connection.IsConnected) {
this.Database = this.Database ?? Connection.GetDatabase(0);
}
}
public T GetData<T>(string key) where T : class {
public virtual T GetData<T>(string key) where T : class {
if(!Usable) return null;
return fromJson<T>(Database.StringGet(key));
}
public void SetData<T>(string key,T obj) {
public virtual void SetData<T>(string key,T obj) {
SetData(key,obj,new TimeSpan(3,0,0,0));
}
public void SetData<T>(string key,T obj,TimeSpan timeSpan) {
public virtual void SetData<T>(string key,T obj,TimeSpan timeSpan) {
if(!Usable) return;
if(timeSpan == null) {
this.Database.StringSet(key,toJson(obj));

View File

@ -186,6 +186,8 @@
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Bll\DesHelper.cs" />
<Compile Include="Bll\Cache.cs" />
<Compile Include="Dal\Queryes\HisPrescriptionQuery.cs" />
<Compile Include="Dal\Queryes\IQuery.cs" />
<Compile Include="Dal\Wappers\HisDrugInfoWapper.cs" />
<Compile Include="Dal\Wappers\HisPrescriptyInfoWapper.cs" />
<Compile Include="Controllers\HomeController.cs" />

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cmdjy.Dal.Tables;
using System.Linq;
using Cmdjy.Bll;
using System.Data.Entity;
namespace Cmdjy.Dal.Queryes
{
public interface IHisDrugQuery:IDbQuery<HisDrugInfo>
{
IEnumerable<HisDrugInfo> GetDataByPrescriptionId(int pId);
}
public class HisDrugQuery:IHisDrugQuery
{
public Lazy<ICache> Cache { get; set; }
public HisDrugQuery() {
this.Cache = new Lazy<ICache>(() => CacheFactory.Cache);
}
public IEnumerable<HisDrugInfo> GetAll() {
using(var db = new DjyDbContext()) {
return GetQuery(db).ToList();
}
}
public IEnumerable<HisDrugInfo> GetDataByPrescriptionId(int pId) {
var key = getPKey(pId.ToString());
var val = this.Cache.Value.GetData<IEnumerable<HisDrugInfo>>(key);
if(val == null) {
using(var db = new DjyDbContext()) {
val = GetQuery(db).Where(m => m.PrescriptionId == pId).ToList();
if(val != null) this.Cache.Value.SetData(key,val);
}
}
return val;
}
public HisDrugInfo GetOne<TKey>(TKey id) {
var key = getKey(id.ToString());
var val = this.Cache.Value.GetData<HisDrugInfo>(key);
if(val == null) {
using(var db = new DjyDbContext()) {
val = db.DrugInfos.Find(id);
if(val != null) this.Cache.Value.SetData(key,val);
}
}
return val;
}
public IQueryable<HisDrugInfo> GetQuery(DjyDbContext db) {
return db.DrugInfos.AsQueryable();
}
public void Insert(HisDrugInfo obj) {
using(var db = new DjyDbContext()) {
db.Entry(obj).State = EntityState.Added;
db.SaveChanges();
}
var key = getKey(obj.Id.ToString());
this.Cache.Value.SetData(key,obj);
var pkey = getPKey(obj.PrescriptionId.ToString());
var val = this.Cache.Value.GetData<IEnumerable<HisDrugInfo>>(key);
if(val == null) val = new List<HisDrugInfo>();
val.ToList().Add(obj);
this.Cache.Value.SetData(pkey,val);
}
private string getKey(string id) {
return $"Db:HisDrugInfo:Id:{id}";
}
private string getPKey(string pId) {
return $"Db:HisDrugInfo:PrescriptionId:{pId}";
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cmdjy.Dal.Tables;
using System.Data.Entity;
using Cmdjy.Bll;
namespace Cmdjy.Dal.Queryes
{
public interface IHisPrescriptionQuery:IDbQuery<HisPrescriptionInfo> { }
public class HisPrescriptionQuery:IHisPrescriptionQuery
{
public Lazy<ICache> Cache { get; set; }
public HisPrescriptionQuery() {
this.Cache = new Lazy<ICache>(() => CacheFactory.Cache);
}
public IEnumerable<HisPrescriptionInfo> GetAll() {
using(var db = new DjyDbContext()) {
return db.PrescriptionInfos.ToList();
}
}
public HisPrescriptionInfo GetOne<TKey>(TKey id) {
var key = getKey(id.ToString());
var val = this.Cache.Value.GetData<HisPrescriptionInfo>(key);
if(val == null) {
using(var db = new DjyDbContext()) {
val = db.PrescriptionInfos.Find(id);
if(val != null) this.Cache.Value.SetData(key,val);
}
}
return val;
}
public void Insert(HisPrescriptionInfo obj) {
using(var db = new DjyDbContext()) {
db.Entry(obj).State = EntityState.Added;
db.SaveChanges();
}
var key = getKey(obj.Id.ToString());
this.Cache.Value.SetData(key,obj);
}
IQueryable<HisPrescriptionInfo> IDbQuery<HisPrescriptionInfo>.GetQuery(DjyDbContext db) {
return db.PrescriptionInfos.AsQueryable();
}
private string getKey(string id) {
return $"Db:HisPrescriptionInfo:Id:{id}";
}
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Linq;
namespace Cmdjy.Dal.Queryes
{
/// <summary>
/// 数据查询基础接口
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public interface IDbQuery<T>
{
T GetOne<TKey>(TKey id);
IEnumerable<T> GetAll();
void Insert(T obj);
IQueryable<T> GetQuery(DjyDbContext db);
}
}

View File

@ -10,7 +10,7 @@ namespace Cmdjy.Dal.Tables
/// <summary>
/// HIS发送的药品信息
/// </summary>
[Table("HisPrescriptionInfo")]
[Table("HisDrugInfo")]
public class HisDrugInfo
{
/// <summary>

View File

@ -19,6 +19,9 @@
<!--Redis服务器连接字符串,localhost:13919,password=123-->
<add key="RedisConnectionString" value=""/>
<!--万仕达接口一次最大传送处方数-->
<add key="wsd:maxCount" value="100"/>
</appSettings>
<connectionStrings>
<add name="DjyDbContext" connectionString="Data Source=FALCON-PC\SQLSERVER2008R2;Persist Security info=True;Initial Catalog=cmdjy;Integrated Security=True" providerName="System.Data.SqlClient" />

View File

@ -27,5 +27,17 @@ namespace Cmdjy
public static string RedisConnectionString {
get => GetValue("RedisConnectionString");
}
/// <summary>
/// 万仕达接口一次最大传送处方数
/// </summary>
public static int WsdMaxCount {
get {
var v = GetValue("wsd:maxCount");
if(int.TryParse(v,out int iv)) {
return iv;
}
return 100;
}
}
}
}

View File

@ -3,6 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Cmdjy.Bll;
using Newtonsoft.Json;
using Cmdjy.Bll;
namespace Cmdjy.ws
{
@ -18,15 +21,56 @@ namespace Cmdjy.ws
{
[WebMethod(Description = "获取代煎药处方信息")]
public string GetData(WsdPrescriptionInfo info) {
public string GetData(WsdRequest info) {
var result = new WsdResult {
};
result.Prescriptions = new List<WsdPrescriptionInfo>();
//获取数据
//实例化WsdResult
var result = new WsdRequest {
int start = 0;
if(!int.TryParse(info.StartNo,out start)) {
result.Code = EnumCode.Exception;
result.Msg = "Start值错误无法转换为正数";
}
int count = 0;
if(!int.TryParse(info.MaxCount,out count)) {
result.Code = EnumCode.Exception;
result.Msg = "MaxCount值错误无法转换为正数";
}
if(count <= 0) {
result.Code = EnumCode.Exception;
result.Msg = "Count值小于0。";
}
count = count > WebSettings.WsdMaxCount ? WebSettings.WsdMaxCount : count;
var cache = CacheFactory.Cache;
for(int i = start;i < start + count;i++) {
//从缓存获取
var pkey = $"Wsd:Pre:Id:{i}";
var one = cache.GetData<WsdPrescriptionInfo>(pkey);
if(one == null) {
using(var db = new Dal.DjyDbContext()) {
var qu = db.PrescriptionInfos
.Where(m => m.Id == i)
.Where(m => m.Type == Dal.Tables.PrescriptionType.Order || m.Type == Dal.Tables.PrescriptionType.CancelOrder);
if(qu.Any()) {
one = new WsdPrescriptionInfo {
};
cache.SetData(pkey,one);
}
}
}
//从数据库获取
}
//编码json串 + 加密信息
return "Hello World";
try {
var resStr = JsonConvert.SerializeObject(result);
var dsc = DesHelper.GetHelper();
var mw = dsc.Encrypty(resStr);
return mw;
}
catch(Exception ex) {
return ex.ToString();
}
}
}
@ -44,6 +88,14 @@ namespace Cmdjy.ws
public class WsdResult
{
/// <summary>
/// 返回信息代码
/// </summary>
public EnumCode Code { get; set; }
/// <summary>
/// 返回信息
/// </summary>
public string Msg { get; set; }
/// <summary>
/// 本次发送的处方数
/// </summary>
@ -58,6 +110,22 @@ namespace Cmdjy.ws
public List<WsdPrescriptionInfo> Prescriptions { get; set; }
}
/// <summary>
/// 返回信息代码
/// </summary>
[Flags]
public enum EnumCode
{
/// <summary>
/// 执行成功。
/// </summary>
Success = 1,
/// <summary>
/// 发生异常异常信息见msg参数。
/// </summary>
Exception = 2,
}
/// <summary>
/// HIS发送的处方信息
/// </summary>