From d8aa65ff8ed65bf59c0e69b692f84e18f195fea4 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Thu, 14 Mar 2019 14:12:09 +0800
Subject: [PATCH] =?UTF-8?q?(#1)(#2)=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0?=
=?UTF-8?q?=E4=BB=A3=E7=85=8E=E8=8D=AF=E5=8E=82=E5=95=86=E7=BC=96=E7=A0=81?=
=?UTF-8?q?=E5=92=8C=E5=90=8D=E7=A7=B0=E6=95=B0=E6=8D=AE,=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E5=8E=82=E5=95=86=E5=AE=9A=E4=B9=89=E7=AD=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs | 28 +++++------
WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj | 2 +
.../Cmdjy/Cmdjy/Dal/DbContextFactory.cs | 15 ++++++
WebSiteCode/Cmdjy/Cmdjy/Dal/DjyDbContext.cs | 10 +++-
.../Cmdjy/Cmdjy/Dal/Queryes/HisDrugQuery.cs | 8 +--
.../Cmdjy/Dal/Queryes/HisPrescriptionQuery.cs | 6 +--
.../Cmdjy/Cmdjy/Dal/Tables/CompanyInfo.cs | 49 +++++++++++++++++++
.../Cmdjy/Dal/Tables/HisPrescriptionInfo.cs | 8 +++
.../Cmdjy/Cmdjy/Models/HisInfoModels.cs | 4 +-
.../Cmdjy/Cmdjy/ws/HisInterface.asmx.cs | 12 ++++-
.../Cmdjy/Cmdjy/ws/WsdInterface.asmx.cs | 45 ++++++++++++-----
.../Cmdjy/CmdjyTests/Bll/DesHelperTests.cs | 28 +++++------
12 files changed, 163 insertions(+), 52 deletions(-)
create mode 100644 WebSiteCode/Cmdjy/Cmdjy/Dal/DbContextFactory.cs
create mode 100644 WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/CompanyInfo.cs
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs b/WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs
index 3d97b50..b5b5ab7 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs
@@ -23,15 +23,14 @@ namespace Cmdjy.Bll
///
public byte[] IV { get; set; }
///
- /// 加密key
- ///
- public string Key { get; set; }
- ///
/// 加密字符串获取Base64密文
///
/// 要加密的字符串
- public string Encrypty(string str) {
- byte[] bKey = Encoding.UTF8.GetBytes(Key.Substring(0,8));
+ public string Encrypty(string key,string str) {
+ if(key.Length < 8) {
+ throw new Exception("加密用的key长度为8位");
+ }
+ byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8));
byte[] bStr = Encoding.UTF8.GetBytes(str);
var desc = createProvider();
using(var mStream = new MemoryStream()) {
@@ -46,9 +45,12 @@ namespace Cmdjy.Bll
/// 从Base64密文解密,获取字符串
///
/// 密文
- public string DesCrypty(string str) {
+ public string DesCrypty(string key,string str) {
+ if(key.Length < 8) {
+ throw new Exception("加密用的key长度为8位");
+ }
var bStr = Convert.FromBase64String(str);
- byte[] bKey = Encoding.UTF8.GetBytes(Key.Substring(0,8));
+ byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8));
var desc = createProvider();
using(var mStream = new MemoryStream()) {
using(var cStream = new CryptoStream(mStream,desc.CreateDecryptor(bKey,IV),CryptoStreamMode.Write)) {
@@ -75,9 +77,8 @@ namespace Cmdjy.Bll
///
public static DesHelper GetHelper() {
if(_deshelper == null) {
- var key = WebSettings.DesKey;
byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
- _deshelper = GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV,key);
+ _deshelper = GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV);
}
return _deshelper;
@@ -89,12 +90,9 @@ namespace Cmdjy.Bll
/// 加密算法模式
/// 初始化向量
/// 加密关键字
- public static DesHelper GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV,string key) {
- if(key.Length < 8) {
- throw new Exception("加密用的key长度为8位");
- }
+ public static DesHelper GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV) {
return new DesHelper {
- Cipher = cipher,IV = IV,Key = key,Padding = padding,
+ Cipher = cipher,IV = IV,Padding = padding,
};
}
}
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj b/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj
index 3435ef1..69fefe3 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj
+++ b/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj
@@ -189,9 +189,11 @@
+
+
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Dal/DbContextFactory.cs b/WebSiteCode/Cmdjy/Cmdjy/Dal/DbContextFactory.cs
new file mode 100644
index 0000000..757051d
--- /dev/null
+++ b/WebSiteCode/Cmdjy/Cmdjy/Dal/DbContextFactory.cs
@@ -0,0 +1,15 @@
+namespace Cmdjy.Dal
+{
+ ///
+ /// 数据上下文工厂
+ ///
+ public class DbContextFactory
+ {
+ ///
+ /// 获取代煎药数据库
+ ///
+ public static DjyDbContext CreateDbContext() {
+ return DjyDbContext.GetContext();
+ }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Dal/DjyDbContext.cs b/WebSiteCode/Cmdjy/Cmdjy/Dal/DjyDbContext.cs
index 75c22d1..26913ba 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Dal/DjyDbContext.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/Dal/DjyDbContext.cs
@@ -12,7 +12,11 @@ namespace Cmdjy.Dal
///
public partial class DjyDbContext:DbContext
{
- public DjyDbContext() : base("DjyDbContext") {
+ public static DjyDbContext GetContext() {
+ return new DjyDbContext();
+ }
+
+ private DjyDbContext() : base("DjyDbContext") {
}
}
///
@@ -32,5 +36,9 @@ namespace Cmdjy.Dal
/// 万仕达下载日志
///
public DbSet WsdRequestLogs { get; set; }
+ ///
+ /// 代煎药厂商信息
+ ///
+ public DbSet CompanyInfos { get; set; }
}
}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisDrugQuery.cs b/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisDrugQuery.cs
index 176ceaf..4684744 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisDrugQuery.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisDrugQuery.cs
@@ -26,7 +26,7 @@ namespace Cmdjy.Dal.Queryes
public IEnumerable GetAll() {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
return GetQuery(db).ToList();
}
}
@@ -35,7 +35,7 @@ namespace Cmdjy.Dal.Queryes
var key = getPKey(pId.ToString());
var val = this.Cache.Value.GetData>(key);
if(val == null) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
val = GetQuery(db).Where(m => m.PrescriptionId == pId).ToList();
if(val != null) this.Cache.Value.SetData(key,val);
}
@@ -47,7 +47,7 @@ namespace Cmdjy.Dal.Queryes
var key = getKey(id.ToString());
var val = this.Cache.Value.GetData(key);
if(val == null) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
val = db.DrugInfos.Find(id);
if(val != null) this.Cache.Value.SetData(key,val);
}
@@ -60,7 +60,7 @@ namespace Cmdjy.Dal.Queryes
}
public void Insert(HisDrugInfo obj) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
db.Entry(obj).State = EntityState.Added;
db.SaveChanges();
}
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisPrescriptionQuery.cs b/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisPrescriptionQuery.cs
index b9cbfee..9bb8b38 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisPrescriptionQuery.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/Dal/Queryes/HisPrescriptionQuery.cs
@@ -19,7 +19,7 @@ namespace Cmdjy.Dal.Queryes
}
public IEnumerable GetAll() {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
return db.PrescriptionInfos.ToList();
}
}
@@ -28,7 +28,7 @@ namespace Cmdjy.Dal.Queryes
var key = getKey(id.ToString());
var val = this.Cache.Value.GetData(key);
if(val == null) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
val = db.PrescriptionInfos.Find(id);
if(val != null) this.Cache.Value.SetData(key,val);
}
@@ -37,7 +37,7 @@ namespace Cmdjy.Dal.Queryes
}
public void Insert(HisPrescriptionInfo obj) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
db.Entry(obj).State = EntityState.Added;
db.SaveChanges();
}
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/CompanyInfo.cs b/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/CompanyInfo.cs
new file mode 100644
index 0000000..d7a63bf
--- /dev/null
+++ b/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/CompanyInfo.cs
@@ -0,0 +1,49 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Cmdjy.Dal.Tables
+{
+ ///
+ /// 代煎药厂商信息
+ ///
+ [Table("CompanyInfo")]
+ public class CompanyInfo
+ {
+ ///
+ /// 厂商编号
+ ///
+ [Key]
+ public int Id { get; set; }
+ ///
+ /// 厂商名称
+ ///
+ public string Name { get; set; }
+ ///
+ /// 厂商地址
+ ///
+ public string Address { get; set; }
+ ///
+ /// 厂商电话
+ ///
+ public string Phone { get; set; }
+ ///
+ /// 安全关键字
+ ///
+ public string SecurityKey { get; set; }
+ ///
+ /// 厂商状态
+ ///
+ public EnumComanyInfo Status { get; set; }
+ }
+
+ ///
+ /// 厂家状态
+ ///
+ [Flags]
+ public enum EnumComanyInfo
+ {
+ Enable = 0,
+ Disable = 1,
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/HisPrescriptionInfo.cs b/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/HisPrescriptionInfo.cs
index e445206..efdb486 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/HisPrescriptionInfo.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/Dal/Tables/HisPrescriptionInfo.cs
@@ -147,6 +147,14 @@ namespace Cmdjy.Dal.Tables
/// 用法
///
public string Usage { get; set; }
+ ///
+ /// 代煎药厂商代码
+ ///
+ public string CompanyCode { get; set; }
+ ///
+ /// 代煎药厂商名称
+ ///
+ public string CompanyName { get; set; }
}
///
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Models/HisInfoModels.cs b/WebSiteCode/Cmdjy/Cmdjy/Models/HisInfoModels.cs
index 9b0cdfc..f7063b5 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Models/HisInfoModels.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/Models/HisInfoModels.cs
@@ -11,7 +11,7 @@ namespace Cmdjy.Models
public static class HisInfo
{
public static IEnumerable GetPreInfos(PreFilter filter) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
var qu = db.PrescriptionInfos.AsQueryable();
if(filter == null) return qu.ToList();
if(!string.IsNullOrEmpty(filter.Start) && int.TryParse(filter.Start,out var s)) {
@@ -25,7 +25,7 @@ namespace Cmdjy.Models
}
public static IEnumerable GetDrugInfos(int preId) {
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
var qu = db.DrugInfos.Where(m => m.PrescriptionId == preId);
return qu.ToList();
}
diff --git a/WebSiteCode/Cmdjy/Cmdjy/ws/HisInterface.asmx.cs b/WebSiteCode/Cmdjy/Cmdjy/ws/HisInterface.asmx.cs
index be97077..ae47d48 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/ws/HisInterface.asmx.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/ws/HisInterface.asmx.cs
@@ -24,7 +24,7 @@ namespace Cmdjy.ws
[WebMethod(Description = "医疗机构上传处方信息")]
public string PostPrescriptionInfo(HisPrescriptionInfo info) {
HisPrescriptionResult result = null;
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
var en = new HisPrescriptyInfoWapper(info) as Dal.Tables.HisPrescriptionInfo;
en.RawAddress = this.Context.Request.UserHostAddress;
en.CreateDatetime = DateTime.Now;
@@ -49,7 +49,7 @@ namespace Cmdjy.ws
[WebMethod(Description = "医疗机构上传药品信息")]
public string PostDrugInfo(HisDrugInfo info) {
HisDrugResult result = null;
- using(var db = new DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
var en = new HisDrugInfoWapper(info) as Dal.Tables.HisDrugInfo;
en.RawAddress = this.Context.Request.UserHostAddress;
en.CreateDatetime = DateTime.Now;
@@ -213,6 +213,14 @@ namespace Cmdjy.ws
/// 收费费用序号
///
public string Sffyxh { get; set; }
+ ///
+ /// 代煎药厂商代码
+ ///
+ public string CompanyCode { get; set; }
+ ///
+ /// 代煎药厂商名称
+ ///
+ public string CompanyName { get; set; }
}
///
/// HIS发送处方信息应答
diff --git a/WebSiteCode/Cmdjy/Cmdjy/ws/WsdInterface.asmx.cs b/WebSiteCode/Cmdjy/Cmdjy/ws/WsdInterface.asmx.cs
index b1d3bab..eae6a1d 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/ws/WsdInterface.asmx.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/ws/WsdInterface.asmx.cs
@@ -5,6 +5,7 @@ using System.Web.Services;
using Cmdjy.Bll;
using Newtonsoft.Json;
using System.Threading.Tasks;
+using Cmdjy.Dal;
namespace Cmdjy.ws
{
@@ -22,8 +23,19 @@ namespace Cmdjy.ws
[WebMethod(Description = "获取代煎药处方信息")]
public string GetData(WsdRequest info) {
var result = new WsdResult {
+ Code = EnumCode.Success,
};
- result.Prescriptions = new List();
+ string cKey = null;
+ using(var db = DbContextFactory.CreateDbContext()) {
+ var companyInfo = db.CompanyInfos.Where(m => m.Id.ToString() == info.CompanyCode);
+ if(companyInfo.Any()) {
+ cKey = companyInfo.First().SecurityKey;
+ }
+ }
+ if(string.IsNullOrEmpty(cKey)) {
+ result.Code = EnumCode.Exception;
+ result.Msg = "CompanyCode值错误";
+ }
//获取数据
int start = 0;
if(!int.TryParse(info.StartNo,out start)) {
@@ -37,23 +49,26 @@ namespace Cmdjy.ws
}
if(count <= 0) {
result.Code = EnumCode.Exception;
- result.Msg = "Count值小于0。";
+ result.Msg = "Count值错误。";
}
if(result.Code != EnumCode.Exception) {
+ result.Prescriptions = new List();
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 pkey = $"Company:Id:{info.CompanyCode}:Pre:Id:{i}";
var one = cache.GetData(pkey);
if(one == null) {
//从数据库获取处方信息
- one = getPrescriptionInfoFromDb(i);
+ one = getPrescriptionInfoFromDb(info.CompanyCode,i);
+ //判断是否为
//获取药品信息
if(one != null) one.Drugs = getDrugInfoFromDb(one.Id);
//存缓存
if(one != null) cache.SetData(pkey,one);
}
+ //对应代煎药厂商
if(one == null) {
one = new WsdPrescriptionInfo {
Type = WsdPrescriptionType.NullOrder,Id = i,
@@ -64,14 +79,14 @@ namespace Cmdjy.ws
result.Code = EnumCode.Success;
result.Count = result.Prescriptions.Where(m => m.Type != WsdPrescriptionType.NullOrder).Count();
result.Msg = "";
- using(var db = new Dal.DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
result.More = db.PrescriptionInfos.Max(m => m.Id) > (start + count);
}
}
//编码:json串 + 加密信息
try {
var resStr = JsonConvert.SerializeObject(result);
- var mw = DesHelper.GetHelper().Encrypty(resStr);
+ var mw = DesHelper.GetHelper().Encrypty(cKey,resStr);
logWsdRequest(info,result,resStr,mw);
return mw;
}
@@ -87,7 +102,7 @@ namespace Cmdjy.ws
/// 响应字符串
/// 响应密文
private void logWsdRequest(WsdRequest info,WsdResult result,string resStr,string mw) {
- using(var db = new Dal.DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
if(result != null && result.Prescriptions != null) {
foreach(var p in result.Prescriptions) {
if(p.Drugs != null) {
@@ -113,7 +128,7 @@ namespace Cmdjy.ws
/// 处方号
private static List getDrugInfoFromDb(int id) {
List result = null;
- using(var db = new Dal.DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
var qu = db.DrugInfos.Where(m => m.PrescriptionId == id);
if(qu.Any()) {
result = new List();
@@ -130,11 +145,11 @@ namespace Cmdjy.ws
/// 从数据库获取处方
///
/// 处方号
- private static WsdPrescriptionInfo getPrescriptionInfoFromDb(int i) {
+ private static WsdPrescriptionInfo getPrescriptionInfoFromDb(string cId,int i) {
WsdPrescriptionInfo result = null;
- using(var db = new Dal.DjyDbContext()) {
+ using(var db = DbContextFactory.CreateDbContext()) {
var qu = db.PrescriptionInfos
- .Where(m => m.Id == i)
+ .Where(m => m.Id == i && m.CompanyCode == cId)
.Where(m => m.Type == Dal.Tables.PrescriptionType.Order || m.Type == Dal.Tables.PrescriptionType.CancelOrder);
if(qu.Any()) {
var fir = qu.First();
@@ -151,6 +166,14 @@ namespace Cmdjy.ws
///
public class WsdRequest
{
+ ///
+ /// 代煎药厂商编码。
+ ///
+ public string CompanyCode { get; set; }
+ ///
+ /// 代煎药厂商名称
+ ///
+ public string CompanyName { get; set; }
///
/// 处方起始流水号。按照数字从1开始累加。返回结果包含此编号的处方。
///
diff --git a/WebSiteCode/Cmdjy/CmdjyTests/Bll/DesHelperTests.cs b/WebSiteCode/Cmdjy/CmdjyTests/Bll/DesHelperTests.cs
index 17ee87e..8ac9e7b 100644
--- a/WebSiteCode/Cmdjy/CmdjyTests/Bll/DesHelperTests.cs
+++ b/WebSiteCode/Cmdjy/CmdjyTests/Bll/DesHelperTests.cs
@@ -1,11 +1,7 @@
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using Cmdjy.Bll;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System;
using System.Security.Cryptography;
+using System.Text;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Cmdjy.Bll.Tests
{
@@ -14,21 +10,25 @@ namespace Cmdjy.Bll.Tests
{
[TestMethod()]
public void EncryptyTest() {
- byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
- var des = DesHelper.GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV,"12365478");
- Console.WriteLine($"Key:{des.Key}");
+ byte[] IV = { 0x01,0x02,0x03,0x14,0x05,0x06,0x17,0x08 };
+ string key = "12365478";
+ var des = DesHelper.GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV);
+ Console.WriteLine($"Key:{key}");
string mingwen = getMingWen();
Console.WriteLine($"明文:{mingwen}");
- var miwen = des.Encrypty(mingwen);
+ var miwen = des.Encrypty(key,mingwen);
Console.WriteLine($"密文:{miwen}");
- var mingwen2 = des.DesCrypty(miwen);
+ var miwen2 = des.Encrypty(key,mingwen);
+ Console.WriteLine($"密文2:{miwen2}");
+ Assert.AreEqual(miwen,miwen2);
+ var mingwen2 = des.DesCrypty(key,miwen);
Assert.AreEqual(mingwen,mingwen2);
}
public string getMingWen() {
- string str = "abcdefghijklmnopqrstuvwxyz123456789,./*-+";
+ string str = "abcdefghijklmnopqrstuvwxyz1234567890,./*-+";
var result = new StringBuilder();
- int len = 1000;
+ int len = 100;
var rean = new Random();
for(int i = 0;i < len;i++) {
var p = rean.Next(str.Length);