diff --git a/B3ClientService/B3ClientService.csproj b/B3ClientService/B3ClientService.csproj index cd58868..bf706cf 100644 --- a/B3ClientService/B3ClientService.csproj +++ b/B3ClientService/B3ClientService.csproj @@ -122,6 +122,7 @@ + diff --git a/B3ClientService/BO/Bill/WeightBill/WeightBill.cs b/B3ClientService/BO/Bill/WeightBill/WeightBill.cs index 2e9a0fe..efbd527 100644 --- a/B3ClientService/BO/Bill/WeightBill/WeightBill.cs +++ b/B3ClientService/BO/Bill/WeightBill/WeightBill.cs @@ -9,8 +9,11 @@ using TSingSoft.WebPluginFramework; namespace BWP.B3ClientService.BO { [Serializable] - public class WeightBill : SyncBill + public class WeightBill : SyncBill, IWithRowVersion { + [DbColumn(DefaultValue = 0)] + public int RowVersion { get; set; } + #region BasicFile public long? AccountingUnit_ID { get; set; } diff --git a/B3ClientService/ClientServiceUtils.cs b/B3ClientService/ClientServiceUtils.cs new file mode 100644 index 0000000..df92ebf --- /dev/null +++ b/B3ClientService/ClientServiceUtils.cs @@ -0,0 +1,41 @@ +using BWP.B3ClientService.BO; +using Forks.EnterpriseServices.DomainObjects2; +using Forks.EnterpriseServices.DomainObjects2.DQuery; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TSingSoft.WebPluginFramework; + +namespace BWP.B3ClientService +{ + public interface IWithRowVersion + { + long ID { get; set; } + int RowVersion { get; set; } + } + + public static class ClientServiceUtils + { + public static bool RowVersionSame(int rowVersion, long id) + where T : IWithRowVersion + { + var query = new DQueryDom(new JoinAlias(typeof(T))); + query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ID", id), DQCondition.EQ("RowVersion", rowVersion))); + return query.EExists(); + } + + public static bool Exist(long id) + where T : SyncBase + { + return Exist("ID", id); + } + + public static bool Exist(string field, object value) + { + var query = new DQueryDom(new JoinAlias(typeof(T))); + query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ(field, value), DQCondition.EQ("DeleteState", false))); + return query.EExists(); + } + } +} diff --git a/B3ClientService/Rpcs/BillRpc/HouseAndSanctionRpc.cs b/B3ClientService/Rpcs/BillRpc/HouseAndSanctionRpc.cs index 144c8b7..c6b96d5 100644 --- a/B3ClientService/Rpcs/BillRpc/HouseAndSanctionRpc.cs +++ b/B3ClientService/Rpcs/BillRpc/HouseAndSanctionRpc.cs @@ -20,20 +20,31 @@ namespace BWP.B3ClientService.Rpcs.BillRpc { static JavaScriptSerializer serializer = new JavaScriptSerializer(); + /* + * //原型 + SELECT w2.id, w1.* FROM B3ClientService_WeightBill w1, (   + +SELECT TOP 100 row_number() OVER (order by (case when Remark is null or Remark='' then 0 else 1 end),ID) n, ID FROM B3ClientService_WeightBill ) w2 WHERE w1.ID = w2.ID AND w2.n > 90 ORDER BY w2.n ASC + + */ [Rpc] - public static string GetHouseAndSanctionList(DateTime date) + public static string GetHouseAndSanctionList(DateTime date, int pageSize, int pageNumber) { + var temp = new JoinAlias(typeof(OrderTemp)); + var query = new DQueryDom(temp); + OrderTemp.Register(query, date, pageSize * pageNumber); + var main = new JoinAlias(typeof(WeightBill)); var detail = new JoinAlias(typeof(WeightBill_Detail)); - var query = new DQueryDom(main); + query.From.AddJoin(JoinType.Left, new DQDmoSource(main), DQCondition.EQ(temp, "ID", main, "ID")); query.From.AddJoin(JoinType.Left, new DQDmoSource(detail), DQCondition.And(DQCondition.EQ(detail, "DeleteState", false), DQCondition.EQ(main, "ID", detail, "WeightBill_ID"))); - query.Columns.Add(DQSelectColumn.Field("ID")); - query.Columns.Add(DQSelectColumn.Field("B3ID")); - query.Columns.Add(DQSelectColumn.Field("Employee_Name")); - query.Columns.Add(DQSelectColumn.Field("Supplier_Name")); - query.Columns.Add(DQSelectColumn.Field("HouseNames")); + query.Columns.Add(DQSelectColumn.Field("ID", main)); + query.Columns.Add(DQSelectColumn.Field("B3ID", main)); + query.Columns.Add(DQSelectColumn.Field("Employee_Name", main)); + query.Columns.Add(DQSelectColumn.Field("Supplier_Name", main)); + query.Columns.Add(DQSelectColumn.Field("HouseNames", main)); query.Columns.Add(DQSelectColumn.Field("Number", detail)); - query.Where.Conditions.Add(DQCondition.And(DQCondition.Between("WeighTime", date, date + new TimeSpan(23, 59, 29)), DQCondition.EQ("DeleteState", false))); + query.Where.Conditions.Add(DQCondition.GreaterThan(temp, "RowNumber", pageSize * (pageNumber - 1))); var list = new List(); using (var session = Dmo.NewSession()) { @@ -55,6 +66,36 @@ namespace BWP.B3ClientService.Rpcs.BillRpc return serializer.Serialize(list); } + class OrderTemp + { + public int RowNumber { get; set; } + + public long ID { get; set; } + + public static void Register(DQueryDom root, DateTime date, int total) + { + var query = new DQueryDom(new JoinAlias("_pageTemp", typeof(WeightBill))); + query.Range = SelectRange.Top(total); + query.Columns.Add(DQSelectColumn.Create(DQExpression.Snippet("row_number() OVER (order by (case when [_pageTemp].[HouseNames] is null or [_pageTemp].[HouseNames]='' then 0 else 1 end),[_pageTemp].[ID])"), "RowNumber")); + query.Columns.Add(DQSelectColumn.Field("ID")); + query.Where.Conditions.Add(DQCondition.And(DQCondition.Between("WeighTime", date, date + new TimeSpan(23, 59, 29)), DQCondition.EQ("DeleteState", false))); + root.RegisterQueryTable(typeof(OrderTemp), new string[] { "RowNumber", "ID" }, query); + } + } + + [Rpc] + public static int GetMaxPageNumber(DateTime date, int pageSize) + { + var query = new DQueryDom(new JoinAlias(typeof(WeightBill))); + query.Where.Conditions.Add(DQCondition.And(DQCondition.Between("WeighTime", date, date + new TimeSpan(23, 59, 29)), DQCondition.EQ("DeleteState", false))); + query.Columns.Add(DQSelectColumn.Count()); + var total = Convert.ToInt32(query.EExecuteScalar()); + var maxPageSize = total / pageSize; + if (total % pageSize != 0) + maxPageSize += 1; + return maxPageSize; + } + [Rpc] public static int GetDetailTotalNumber(DateTime date) { @@ -77,6 +118,9 @@ namespace BWP.B3ClientService.Rpcs.BillRpc var result = new HouseAndSanctionEdit(); using (var session = Dmo.NewSession()) { + var exist = ClientServiceUtils.Exist(id); + if (!exist) + throw new Exception("过磅单已被删除"); result.ID = id; result.HogGrade_ID = InnerBLUtil.GetDmoPropertyByID(session, typeof(WeightBill), "HogGrade_ID", id); result.Number = InnerBLUtil.GetDmoProperty(session, typeof(WeightBill_Detail), "Number", new Tuple("WeightBill_ID", id), new Tuple("DeleteState", false)); @@ -97,6 +141,12 @@ namespace BWP.B3ClientService.Rpcs.BillRpc public static int UpdateInsertHouseAndSanction(string json) { var entity = serializer.Deserialize(json); + var exist = ClientServiceUtils.Exist(entity.ID); + if (!exist) + throw new Exception("过磅单已被删除,提交失败!"); + var detailExist = ClientServiceUtils.Exist("WeightBill_ID", entity.ID); + if (!detailExist) + throw new Exception("过磅记录已被删除,提交失败!"); using (var session = Dmo.NewSession()) { //为了节省ID,把传过来的通过栋舍匹配,并到原有明细里。然后从新的集合里把上传的集合里不存在的剔除掉 @@ -172,6 +222,7 @@ namespace BWP.B3ClientService.Rpcs.BillRpc update.Columns.Add(new DQUpdateColumn("HouseNames", string.Join(",", houseNames))); update.Columns.Add(new DQUpdateColumn("SanctionNumber", entity.SanctionDetails.Sum(x => x.Number ?? 0))); update.Columns.Add(new DQUpdateColumn("Sync", false)); + update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1)))); update.Columns.Add(new DQUpdateColumn("ModifyTime", DateTime.Now)); update.Where.Conditions.Add(DQCondition.EQ("ID", entity.ID)); session.ExecuteNonQuery(update); diff --git a/B3ClientService/Rpcs/BillRpc/OrderDetailRpc.cs b/B3ClientService/Rpcs/BillRpc/OrderDetailRpc.cs index 7ed2a8c..6d76117 100644 --- a/B3ClientService/Rpcs/BillRpc/OrderDetailRpc.cs +++ b/B3ClientService/Rpcs/BillRpc/OrderDetailRpc.cs @@ -153,17 +153,21 @@ namespace BWP.B3ClientService.Rpcs.BillRpc } [Rpc] - public static string GetOrderDetail() + public static string GetOrderDetail(DateTime date, int pageSize, int pageNumber) { - var query = new DQueryDom(new JoinAlias(typeof(OrderDetail))); - query.Columns.Add(DQSelectColumn.Field("ID")); - query.Columns.Add(DQSelectColumn.Field("WeightBill_ID")); - query.Columns.Add(DQSelectColumn.Field("Order")); - query.Columns.Add(DQSelectColumn.Field("PlanNumber")); - query.Columns.Add(DQSelectColumn.Field("LiveColonyHouse_Name")); - query.Columns.Add(DQSelectColumn.Field("IsHurryButcher")); - query.Columns.Add(DQSelectColumn.Field("B3WeighBill_ID")); - query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", DateTime.Today), DQCondition.EQ("DeleteState", false), DQCondition.EQ("SecondarySplit", false))); + var temp = new JoinAlias(typeof(OrderTemp)); + var query = new DQueryDom(temp); + OrderTemp.Register(query, date, pageSize * pageNumber); + var alias = new JoinAlias(typeof(OrderDetail)); + query.From.AddJoin(JoinType.Left, new DQDmoSource(alias), DQCondition.EQ(temp, "ID", alias, "ID")); + query.Columns.Add(DQSelectColumn.Field("ID", alias)); + query.Columns.Add(DQSelectColumn.Field("WeightBill_ID", alias)); + query.Columns.Add(DQSelectColumn.Field("Order", alias)); + query.Columns.Add(DQSelectColumn.Field("PlanNumber", alias)); + query.Columns.Add(DQSelectColumn.Field("LiveColonyHouse_Name", alias)); + query.Columns.Add(DQSelectColumn.Field("IsHurryButcher", alias)); + query.Columns.Add(DQSelectColumn.Field("B3WeighBill_ID", alias)); + query.Where.Conditions.Add(DQCondition.GreaterThan(temp, "RowNumber", pageSize * (pageNumber - 1))); var list = new List(); using (var session = Dmo.NewSession()) { @@ -186,6 +190,36 @@ namespace BWP.B3ClientService.Rpcs.BillRpc return serializer.Serialize(list); } + class OrderTemp + { + public int RowNumber { get; set; } + + public long ID { get; set; } + + public static void Register(DQueryDom root, DateTime date, int total) + { + var query = new DQueryDom(new JoinAlias("_pageTemp", typeof(OrderDetail))); + query.Range = SelectRange.Top(total); + query.Columns.Add(DQSelectColumn.Create(DQExpression.Snippet("row_number() OVER (order by [_pageTemp].[Order] desc)"), "RowNumber")); + query.Columns.Add(DQSelectColumn.Field("ID")); + query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", date), DQCondition.EQ("DeleteState", false), DQCondition.EQ("SecondarySplit", false))); + root.RegisterQueryTable(typeof(OrderTemp), new string[] { "RowNumber", "ID" }, query); + } + } + + [Rpc] + public static int GetMaxPageNumber(DateTime date, int pageSize) + { + var query = new DQueryDom(new JoinAlias(typeof(OrderDetail))); + query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", date), DQCondition.EQ("DeleteState", false), DQCondition.EQ("SecondarySplit", false))); + query.Columns.Add(DQSelectColumn.Count()); + var total = Convert.ToInt32(query.EExecuteScalar()); + var maxPageSize = total / pageSize; + if (total % pageSize != 0) + maxPageSize += 1; + return maxPageSize; + } + [Rpc] public static int GetMaxOrder(DateTime date) { diff --git a/B3ClientService/Rpcs/BillRpc/WeightBillRpc.cs b/B3ClientService/Rpcs/BillRpc/WeightBillRpc.cs index be71524..070c66b 100644 --- a/B3ClientService/Rpcs/BillRpc/WeightBillRpc.cs +++ b/B3ClientService/Rpcs/BillRpc/WeightBillRpc.cs @@ -101,12 +101,17 @@ namespace BWP.B3ClientService.Rpcs.BillRpc { if (dmo.ID != 0) { + bool same = ClientServiceUtils.RowVersionSame(dmo.RowVersion, dmo.ID); + if (!same) + throw new Exception("数据已被更改,请重新打开"); FillServerUpdateFields(session, dmo); + dmo.RowVersion += 1; session.Update(dmo); } else { dmo.ModifyTime = DateTime.Now; + dmo.RowVersion += 1; session.Insert(dmo); } @@ -227,7 +232,6 @@ namespace BWP.B3ClientService.Rpcs.BillRpc return true; } - [Rpc] public static string SyncBillB3Ids(List ids) {