Commit 356d5f58 by 陶然

优化

parent a8a0e3fc
......@@ -3,3 +3,4 @@
################################################################################
/Src/obj
/.vs
......@@ -45,6 +45,10 @@ namespace Kivii.Samples.Entities
[DefaultEmptyGuid]
public Guid ParentKvid { get; set; }
[ApiMember(Description = "包裹名称")]
[StringLength(200), Default("")]
public string PackageName { get; set; }
[ApiMember(Description = "包裹Kvid,只有在样品打包后会记录当前样品所属的包裹Kvid,包裹也是样品实体,包裹类型")]
[DefaultEmptyGuid]
public Guid PackageKvid { get; set; }
......@@ -89,6 +93,12 @@ namespace Kivii.Samples.Entities
[StringLength(50), Default("")]
public SampleType Type { get; set; }
[ApiMember(Description = "样品受理日期")]
public DateTime? DealTime { get; set; }
[ApiMember(Description = "样品完成日期")]
public DateTime? DeadTime { get; set; }
[ApiMember(Description = "操作日期")]
public DateTime OperateTime { get; set; }
......@@ -180,6 +190,12 @@ namespace Kivii.Samples.Entities
[Ignore]
public DateTime? AssignTime { get; set; }
[Ignore]
public Location CurrentLocation { get; set; }
[Ignore]
public Route CurrentRoute { get; set; }
#region 创建/更新人V1.0
[ApiMember(Description = "创建人Kvid ")]
[IgnoreUpdate]
......
......@@ -36,5 +36,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.2023.4240")]
[assembly: AssemblyFileVersion("5.4.2023.4240")]
[assembly: AssemblyVersion("5.4.2023.4270")]
[assembly: AssemblyFileVersion("5.4.2023.4270")]
using Kivii.Linq;
using Kivii.Linq.Legacy;
using Kivii.Samples.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
......@@ -101,11 +103,13 @@ namespace Kivii.Samples
/// <param name="location"></param>
/// <param name="route">下一地点的路由信息组织好传入,用于创建</param>
/// <returns></returns>
public static Sample Assign(this Sample sample, Location location, Route route = null)
public static Sample Assign(this Sample sample, Location location, Route route = null,IDbConnection conn=null)
{
(sample == null).ThrowIfTrue("请传入需要流转的样品信息!");
(location == null).ThrowIfTrue("缺少目的地点配置信息!");
var conn = KiviiContext.GetOpenedDbConnection<Sample>();
bool useTransaction = conn == null;//是否启用内部事务
if (conn == null) conn = KiviiContext.GetOpenedDbConnection<Sample>();
//var conn = KiviiContext.GetOpenedDbConnection<Sample>();
conn.InitEntityType<Location>();
if (location.Kvid == Guid.Empty)
{
......@@ -116,9 +120,14 @@ namespace Kivii.Samples
var existSample = conn.SingleById<Sample>(sample.Kvid);
existSample.ThrowIfNull("未找到此样品信息!");
//找出当前样品所在路由地点,根据Next为空说明就在此地
var currentRoute = conn.Single<Route>(o => o.SampleKvid == existSample.Kvid && o.NextLocationKvid == Guid.Empty);
var currentRoute = existSample.GetCurrentRoute();//conn.Single<Route>(o => o.SampleKvid == existSample.Kvid && o.NextLocationKvid == Guid.Empty);
var trans = conn.OpenTransaction();
IDbTransaction trans = null;//事务,如果外部来了Connection,不生成事务
if (useTransaction)
{
trans = conn.OpenTransaction();
}
//var trans = conn.OpenTransaction();
try
{
#region 生成新的路由信息,更新上一个路由信息
......@@ -152,11 +161,11 @@ namespace Kivii.Samples
conn.UpdateOnly(currentRoute);
}
#endregion
trans.Commit();
trans?.Commit();
}
catch (Exception ex)
{
trans.Rollback();
trans?.Rollback();
throw ex;
}
return existSample;
......@@ -184,12 +193,22 @@ namespace Kivii.Samples
}
var existSample = conn.SingleById<Sample>(sample.Kvid);
existSample.ThrowIfNull("未找到此样品信息!");
//先获取当前的最后一个路由地址
var currentRoute = existSample.GetCurrentRoute();
var rtns = new List<Sample>();
var trans = conn.OpenTransaction();
try
{
if (currentRoute != null)
{
//判断当前拆分地点和此样品所在地一致 要是不一致要先流转到此地
if (currentRoute.CurrentLocationKvid != location.Kvid)
{
existSample = existSample.Assign(location, null, conn);
}
}
foreach (var item in splitSamples)
{
var sampleKvid = Guid.NewGuid();
......@@ -263,9 +282,9 @@ namespace Kivii.Samples
exists.Exists(o => o.PackageKvid != Guid.Empty).ThrowIfTrue("存在样品已经打包!");
//var local = conn.Single<Location>(o => o.InternalCode == location.InternalCode);
//(local == null).ThrowIfTrue($"未找到{location.InternalCode}目的地点配置信息!");
var currentRoutes = conn.Select<Route>(o => Sql.In(o.SampleKvid, exists.ConvertAll(p => p.Kvid)) && o.NextLocationKvid == Guid.Empty);
var existPackage = conn.SingleById<Sample>(package.Kvid);
if (existPackage != null) package = existPackage;
//var currentRoutes = conn.Select<Route>(o => Sql.In(o.SampleKvid, exists.ConvertAll(p => p.Kvid)) && o.NextLocationKvid == Guid.Empty);
var trans = conn.OpenTransaction();
try
......@@ -311,24 +330,35 @@ namespace Kivii.Samples
foreach (var item in exists)
{
var currentRoute = item.GetCurrentRoute();
if (currentRoute != null)
{
//判断当前拆分地点和此样品所在地一致 要是不一致要先流转到此地
if (currentRoute.CurrentLocationKvid != location.Kvid)
{
item.Assign(location, null, conn);
currentRoute = item.GetCurrentRoute();
}
}
//更新样品的包裹Kvid
item.PackageKvid = package.Kvid;
item.AddOnlyProperties(o => o.PackageKvid);
conn.UpdateOnly(item);
//如果当前样品存在当前路由信息就更新
if (!currentRoutes.IsNullOrEmpty())
//if (!currentRoutes.IsNullOrEmpty())
//{
// var currentRoute = currentRoutes.FirstOrDefault(o => o.SampleKvid == item.Kvid);
if (currentRoute != null)
{
var currentRoute = currentRoutes.FirstOrDefault(o => o.SampleKvid == item.Kvid);
if (currentRoute != null)
{
currentRoute.NextLocationKvid = location.Kvid;
currentRoute.NextLocationTitle = location.Title;
currentRoute.AddOnlyProperties(o => o.NextLocationKvid);
currentRoute.AddOnlyProperties(o => o.NextLocationTitle);
conn.UpdateOnly(currentRoute);
}
currentRoute.NextLocationKvid = location.Kvid;
currentRoute.NextLocationTitle = location.Title;
currentRoute.AddOnlyProperties(o => o.NextLocationKvid);
currentRoute.AddOnlyProperties(o => o.NextLocationTitle);
conn.UpdateOnly(currentRoute);
}
//}
//生成新的路由信息
var route = new Route();
......@@ -388,7 +418,7 @@ namespace Kivii.Samples
//var package = conn.SingleById<Sample>(existSample.PackageKvid);
//var local = conn.Single<Location>(o => o.InternalCode == location.InternalCode);
//(local == null).ThrowIfTrue($"未找到{location.InternalCode}目的地点配置信息!");
var currentRoute = conn.Single<Route>(o => o.SampleKvid == existSample.Kvid && o.NextLocationKvid == Guid.Empty);
var currentRoute = existSample.GetCurrentRoute();//conn.Single<Route>(o => o.SampleKvid == existSample.Kvid && o.NextLocationKvid == Guid.Empty);
var trans = conn.OpenTransaction();
try
......@@ -442,17 +472,45 @@ namespace Kivii.Samples
/// <returns></returns>
public static Location GetCurrentLocation(this Sample sample)
{
sample.ThrowIfNull("请传入要取出的样品信息!");
if(sample== null) return null;
//sample.ThrowIfNull("请传入要取出的样品信息!");
var conn = KiviiContext.GetOpenedDbConnection<Sample>();
var queryRoute = conn.From<Route>();
queryRoute.Where(o => o.SampleKvid == sample.Kvid && o.NextLocationKvid == Guid.Empty);
var routes = conn.Select(queryRoute);
routes.ThrowIfNullOrEmpty("未找到指定样品当前所在地点!");
(routes.Count() > 1).ThrowIfTrue("所在地点不唯一!");
if (routes.IsNullOrEmpty()) return null;
//routes.ThrowIfNullOrEmpty("未找到指定样品当前所在地点!");
//(routes.Count() > 1).ThrowIfTrue("所在地点不唯一!");
if (routes.Count() > 1) return null;
var route = routes[0];
sample.CurrentRoute = route;
var rtns = conn.SingleById<Location>(route.CurrentLocationKvid);
rtns.ThrowIfNull($"未找到所在地({route.CurrentLocationTitle})数据信息!");
if(rtns == null) return null;
sample.CurrentLocation = rtns;
//rtns.ThrowIfNull($"未找到所在地({route.CurrentLocationTitle})数据信息!");
return rtns;
}
/// <summary>
/// 获取当前指定样品所在地
/// </summary>
/// <param name="sample"></param>
/// <returns></returns>
public static Route GetCurrentRoute(this Sample sample)
{
if (sample == null) return null;
//sample.ThrowIfNull("请传入要取出的样品信息!");
var conn = KiviiContext.GetOpenedDbConnection<Sample>();
var queryRoute = conn.From<Route>();
queryRoute.Where(o => o.SampleKvid == sample.Kvid && o.NextLocationKvid == Guid.Empty);
var routes = conn.Select(queryRoute);
if (routes.IsNullOrEmpty()) return null;
//routes.ThrowIfNullOrEmpty("未找到指定样品当前所在地点!");
//(routes.Count() > 1).ThrowIfTrue("所在地点不唯一!");
if (routes.Count() > 1) return null;
var rtns = routes[0];
sample.CurrentRoute = route;
return rtns;
}
}
}
......@@ -16,11 +16,25 @@ namespace Kivii.Samples.Transforms
public class SampleQuery : RestfulQuery<Sample>
{
public List<Guid> BizKvids { get; set; }
public bool IncludeLocation { get; set; }
public override bool OnPreRestfulQuery(IRequest req, IResponse res, IDbConnection dbConnection, IRestfulQueryResponse<Sample> rtns, ref Expression<Func<Sample, bool>> where)
{
if (!BizKvids.IsNullOrEmpty()) where = o => Sql.In(o.BizKvid, BizKvids);
return base.OnPreRestfulQuery(req, res, dbConnection, rtns, ref where);
}
public override bool OnPostRestfulQuery(IRequest req, IResponse res, IDbConnection dbConnection, IRestfulQueryResponse<Sample> rtns)
{
if (IncludeLocation)
{
foreach (var item in rtns.Results)
{
item.CurrentLocation = item.GetCurrentLocation();
}
}
return base.OnPostRestfulQuery(req, res, dbConnection, rtns);
}
}
[RequiresAnyRole(SystemRoles.Everyone)]
public class SampleCreate : RestfulCreate<Sample>
......@@ -39,112 +53,6 @@ namespace Kivii.Samples.Transforms
public class SampleDelete : RestfulDelete<Sample>
{
}
//public class SampleAssigningResponse : RestfulUpdateResponse<Sample>
//{
//}
//[RequiresAnyRole(SystemRoles.Everyone)]
//public class SampleAssigning : RestfulExecution<Sample>
//{
// //public List<Guid> SampleKvids { get; set; }
// public List<Sample> Samples { get; set; }
// public StatusType StatusType { get; set; }
// public override object OnExecution(IRequest req, IResponse res)
// {
// Samples.ThrowIfNullOrEmpty("请传入要流转的样品信息!");
// var conn = KiviiContext.GetOpenedDbConnection<Sample>();
// var samples = conn.SelectByIds<Sample>(Samples.ConvertAll(p => p.Kvid));
// samples.ThrowIfNullOrEmpty("未找到要流转的样品信息!");
// var title = string.Empty;
// var type = StatusType.ToString();
// var from = string.Empty;
// var to = string.Empty;
// var operatorName = string.Empty;
// switch (StatusType)
// {
// case StatusType.Created:
// title = "样品创建";
// //type = "创建";
// from = "委托受理";
// to = "样品受理交接";
// operatorName = "业务发展科";
// break;
// case StatusType.CommissionAssign:
// title = "受理交接";
// //type = "交接";
// from = "样品受理交接";
// to = "实验室领取交接";
// operatorName = "业务发展科";
// break;
// case StatusType.TaskAssign:
// title = "领取交接";
// //type = "交接";
// from = "实验室领取交接";
// to = "任务分派";
// operatorName = "开样组";
// break;
// default:
// break;
// }
// var preUpdateSamples = new List<Sample>();
// foreach (var item in samples)
// {
// var sample = Samples.FirstOrDefault(o => o.Kvid == item.Kvid);
// if (sample != null) item.AssignTime = sample.AssignTime;
// if (StatusType == StatusType.CommissionAssign)
// {
// if (item.Status == (int)StatusType.Created) preUpdateSamples.Add(item);
// }
// else if (StatusType == StatusType.TaskAssign)
// {
// if (item.Status == (int)StatusType.CommissionAssign) preUpdateSamples.Add(item);
// }
// else
// {
// preUpdateSamples.Add(item);
// }
// }
// var rtns = new SampleAssigningResponse();
// rtns.Results = new List<Sample>();
// var trans = conn.OpenTransaction();
// try
// {
// var updateLamda = conn.From<Sample>();
// updateLamda = updateLamda.Update(o => o.Status);
// updateLamda = updateLamda.Where(o => Sql.In(o.Kvid, preUpdateSamples.ConvertAll(p => p.Kvid)));
// conn.UpdateOnly(new Sample { Status = (int)StatusType }, updateLamda);
// preUpdateSamples.ForEach(o => o.Status = (int)StatusType);
// rtns.Results.AddRange(preUpdateSamples);
// foreach (var item in preUpdateSamples)
// {
// var assign = new Route();
// assign.SampleKvid = item.Kvid;
// assign.SampleName = item.Name;
// assign.BizId = item.BizId;
// assign.BizKvid = item.BizKvid;
// assign.BizType = item.BizType;
// assign.Title = title;
// assign.Type = type;
// //assign.From = from;
// //assign.To = to;
// //assign.PlanStartTime = DateTime.Now.AddHours(3);
// assign.OperateTime = item.AssignTime != null ? item.AssignTime.Value : DateTime.Now;
// assign.OperatorName = operatorName;
// conn.Insert(assign);
// }
// trans.Commit();
// }
// catch (Exception ex)
// {
// trans.Rollback();
// throw ex;
// }
// return rtns;
// }
//}
#endregion
[RequiresAnyRole(SystemRoles.Everyone)]
......@@ -250,59 +158,36 @@ namespace Kivii.Samples.Transforms
//[RequiresAnyRole(SystemRoles.Everyone)]
[Api(Description = "所在地样品")]
public class SampleLocationEx : RestfulExecution<Sample>
public class SampleLocationQuery : RestfulQuery<Sample>
{
#region QueryArgs
public virtual int? Skip { get; set; }
public virtual int? Take { get; set; }
public virtual string OrderBy { get; set; }
public string OrderByDesc { get; set; }
public virtual string Include { get; set; }
public virtual string Fields { get; set; }
public string QueryKeys { get; set; }
public string QueryValues { get; set; }
#endregion
public Guid LocationKvid { get; set; }
public string LocationInternalCode { get; set; }
public override object OnExecution(IRequest req, IResponse res)
private Location location;
public override bool OnPreRestfulQuery(IRequest req, IResponse res, IDbConnection dbConnection, IRestfulQueryResponse<Sample> rtns, ref Expression<Func<Sample, bool>> where)
{
(LocationKvid == Guid.Empty && LocationInternalCode.IsNullOrEmpty()).ThrowIfTrue("请传入要查询的地点信息!");
var conn = KiviiContext.GetOpenedDbConnection<Location>();
Location location = null;
if (LocationKvid != Guid.Empty) location = conn.SingleById<Location>(LocationKvid);
//Location location = null;
if (LocationKvid != Guid.Empty) location = dbConnection.SingleById<Location>(LocationKvid);
if (location == null)
{
if (!LocationInternalCode.IsNullOrEmpty()) location = conn.Single<Location>(o => o.InternalCode == LocationInternalCode);
if (!LocationInternalCode.IsNullOrEmpty()) location = dbConnection.Single<Location>(o => o.InternalCode == LocationInternalCode);
location.ThrowIfNull("未找到指定的地点信息!");
}
var queryRoutes = conn.From<Route>();
var queryRoutes = dbConnection.From<Route>();
queryRoutes.Where(o => o.CurrentLocationKvid == location.Kvid && o.NextLocationKvid == Guid.Empty);
queryRoutes.Select(o => o.SampleKvid);
var dynamicParams = Request.GetRequestParams();
var autoQuery = Request.TryResolve<IAutoQueryDb>();
autoQuery.IncludeTotal = true;
var request = new RestfulQuery<Sample>();
request = request.PopulateWith(this);
var sqlExpress = autoQuery.CreateQuery(Request, conn, request, dynamicParams);
sqlExpress.Where(o => Sql.In(o.Kvid, queryRoutes));
var rtns = autoQuery.Execute(Request, conn, request, sqlExpress);
//var samples = conn.Select<Sample>(o => Sql.In(o.Kvid, queryRoutes));
//var rtns = new RestfulQueryResponse<Sample>();
//rtns.Results = new List<Sample>();
//rtns.Results.AddRange(samples);
//rtns.Total = rtns.Results.Count;
return rtns;
where = o => Sql.In(o.Kvid, queryRoutes);
return base.OnPreRestfulQuery(req, res, dbConnection, rtns, ref where);
}
public override object OnRestfulQueryResponse(IRequest req, IResponse res, IDbConnection dbConnection, IRestfulQueryResponse<Sample> rtns)
{
foreach (var item in rtns.Results)
{
item.CurrentLocation = location;
}
return base.OnRestfulQueryResponse(req, res, dbConnection, rtns);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment