| @ -0,0 +1,271 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using BWP.B3CowButcherManage.BO; | |||
| using BWP.B3Frameworks.Utils; | |||
| using BWP.B3Sale.BO; | |||
| using Forks.EnterpriseServices.DomainObjects2; | |||
| using Forks.EnterpriseServices.DomainObjects2.DQuery; | |||
| using Forks.EnterpriseServices.JsonRpc; | |||
| using Forks.EnterpriseServices.SqlDoms; | |||
| using Forks.Utils; | |||
| using Newtonsoft.Json; | |||
| using TSingSoft.WebPluginFramework; | |||
| namespace BWP.B3CowButcherManageToSale.Rpcs | |||
| { | |||
| [Rpc] | |||
| public static class SaleOutStoreRpc | |||
| { | |||
| class WeightRecord | |||
| { | |||
| public long ID { get; set; } | |||
| public string BarCode { get; set; } | |||
| public DateTime WeightTime { get; set; } | |||
| public decimal? MainUnitNum { get; set; } | |||
| public decimal? SecondNumber { get; set; } | |||
| public bool? IsPackage { get; set; } | |||
| public bool? HasPWeight { get; set; } | |||
| public string Product_Batch { get; set; } | |||
| } | |||
| static Tuple<long, long?, long, string> GetSaleOutInfo(IDmoSession session, long detailID) | |||
| { | |||
| var main = new JoinAlias(typeof(SaleOutStore)); | |||
| var detail = new JoinAlias(typeof(SaleOutStore_Detail)); | |||
| var query = new DQueryDom(main); | |||
| query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "SaleOutStore_ID")); | |||
| query.Columns.Add(DQSelectColumn.Field("ID", main)); | |||
| query.Columns.Add(DQSelectColumn.Field("Customer_ID", main)); | |||
| query.Columns.Add(DQSelectColumn.Field("SaleGoods_ID", detail)); | |||
| query.Columns.Add(DQSelectColumn.Field("Unit", detail)); | |||
| query.Where.Conditions.Add(DQCondition.EQ(detail, "ID", detailID)); | |||
| var result = query.EExecuteList<long, long?, long, string>(session); | |||
| if (result == null) | |||
| throw new Exception("销售出库单不存在"); | |||
| return result.FirstOrDefault(); | |||
| } | |||
| private class ExtObj | |||
| { | |||
| public long? LongExt1 { get; set; } | |||
| public List<long?> LongListExt1 { get; set; } | |||
| public List<long?> LongListExt2 { get; set; } | |||
| } | |||
| [Rpc] | |||
| public static string SaveWeightRecord(string json, long detailID) | |||
| { | |||
| var weightRecord = JsonConvert.DeserializeObject<WeightRecord>(json); | |||
| var result = new List<ExtObj>(); | |||
| Tuple<Money<decimal>?, Money<decimal>?> assignInfo; | |||
| var longListExt1 = new List<long?>(); | |||
| var longListExt2 = new List<long?>(); | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| var saleInfo = GetSaleOutInfo(session, detailID); | |||
| var billType = DmoTypeIDAttribute.GetID(typeof(SaleOutStore)); | |||
| if (weightRecord.IsPackage == true) | |||
| { | |||
| if (weightRecord.HasPWeight == true) | |||
| { | |||
| var record = CreateWeightingInfo(detailID, saleInfo, billType, weightRecord.MainUnitNum, weightRecord.SecondNumber, weightRecord.WeightTime, weightRecord.Product_Batch, weightRecord.ID); | |||
| session.Insert(record); | |||
| longListExt1.Add(record.ID); | |||
| var barCodes = GetBarCodeInfo(weightRecord.BarCode); | |||
| foreach (var code in barCodes) | |||
| { | |||
| var scan = CreateSanDetail(detailID, saleInfo, code.Weight, 1m, weightRecord.ID, record.ID, code.BarCode); | |||
| session.Insert(scan); | |||
| longListExt2.Add(scan.ID); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| var barCodes = GetBarCodeInfo(weightRecord.BarCode); | |||
| foreach (var code in barCodes) | |||
| { | |||
| var wInfo = CreateWeightingInfo(detailID, saleInfo, billType, code.Weight, 1m, weightRecord.WeightTime, code.Product_Batch, weightRecord.ID); | |||
| session.Insert(wInfo); | |||
| longListExt1.Add(wInfo.ID); | |||
| var scan = CreateSanDetail(detailID, saleInfo, code.Weight, 1m, weightRecord.ID, wInfo.ID, code.BarCode); | |||
| session.Insert(scan); | |||
| longListExt2.Add(scan.ID); | |||
| } | |||
| } | |||
| } | |||
| else | |||
| { | |||
| var record = CreateWeightingInfo(detailID, saleInfo, billType, weightRecord.MainUnitNum, weightRecord.SecondNumber, weightRecord.WeightTime, weightRecord.Product_Batch, weightRecord.ID); | |||
| session.Insert(record); | |||
| longListExt1.Add(record.ID); | |||
| var scan = CreateSanDetail(detailID, saleInfo, weightRecord.MainUnitNum, weightRecord.SecondNumber.Value, weightRecord.ID, record.ID, weightRecord.BarCode); | |||
| session.Insert(scan); | |||
| longListExt2.Add(scan.ID); | |||
| } | |||
| result.Add(new ExtObj { LongExt1 = weightRecord.ID, LongListExt1 = longListExt1, LongListExt2 = longListExt2 }); | |||
| assignInfo = UpdateAssignNumber(session, saleInfo.Item1, detailID); | |||
| session.Commit(); | |||
| } | |||
| return JsonConvert.SerializeObject(new Tuple<decimal?, decimal?, List<ExtObj>>(assignInfo.Item1.EToDecimal(), assignInfo.Item2.EToDecimal(), result)); | |||
| } | |||
| [Rpc] | |||
| public static int DeleteAndUpdate(long clientId, long detailId) | |||
| { | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| var weightIDs = GetWeightingInforID(session, detailId, clientId); | |||
| if(weightIDs.Count > 0) | |||
| DeleteByID<WeightingInfor>(session, weightIDs); | |||
| var scanIds = GetScanDetailID(session, detailId, clientId); | |||
| if (scanIds.Count > 0) | |||
| DeleteByID<WeightingInfo_ScanDetail>(session, scanIds); | |||
| var billID = InnerBLUtil.GetDmoPropertyByID<long>(session, typeof(SaleOutStore_Detail), "SaleOutStore_ID", detailId); | |||
| UpdateAssignNumber(session, billID, detailId, false); | |||
| session.Commit(); | |||
| } | |||
| return 1; | |||
| } | |||
| static List<long> GetWeightingInforID(IDmoSession session, long detailID, long clientId) | |||
| { | |||
| var query = new DQueryDom(new JoinAlias(typeof(WeightingInfor))); | |||
| query.Columns.Add(DQSelectColumn.Field("ID")); | |||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ClientID", clientId), DQCondition.EQ("DetailID", detailID), DQCondition.EQ("BillType", DmoTypeIDAttribute.GetID(typeof(SaleOutStore))))); | |||
| return query.EExecuteList<long>(session); | |||
| } | |||
| static List<long> GetScanDetailID(IDmoSession session, long detailID, long clientId) | |||
| { | |||
| var query = new DQueryDom(new JoinAlias(typeof(WeightingInfo_ScanDetail))); | |||
| query.Columns.Add(DQSelectColumn.Field("ID")); | |||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ClientID", clientId), DQCondition.EQ("Detail_ID", detailID))); | |||
| return query.EExecuteList<long>(session); | |||
| } | |||
| static void DeleteByID<T>(IDmoSession session, IEnumerable<long> ids) | |||
| { | |||
| var delete = new DQDeleteDom(typeof(T)); | |||
| delete.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray())); | |||
| session.ExecuteNonQuery(delete); | |||
| } | |||
| private static Tuple<Money<decimal>?, Money<decimal>?> UpdateAssignNumber(IDmoSession session, long billID, long detailID, bool isAdd = true) | |||
| { | |||
| var weightingInfo = GetWeightingInfor(session, detailID); | |||
| var update = new DQUpdateDom(typeof(SaleOutStore_Detail)); | |||
| update.Columns.Add(new DQUpdateColumn("AssignUnitNum", DQExpression.Value(weightingInfo.Item1))); | |||
| update.Columns.Add(new DQUpdateColumn("AssignMainUnitNum", DQExpression.Value(weightingInfo.Item1))); | |||
| update.Columns.Add(new DQUpdateColumn("AssignSecondaryUnit1Num", DQExpression.Value(weightingInfo.Item2))); | |||
| if (isAdd) | |||
| update.Columns.Add(new DQUpdateColumn("PackageNum", DQExpression.Add(DQExpression.IfNull(DQExpression.Field("PackageNum"), DQExpression.Value(0)), DQExpression.Value(1)))); | |||
| else | |||
| update.Columns.Add(new DQUpdateColumn("PackageNum", DQExpression.Add(DQExpression.IfNull(DQExpression.Field("PackageNum"), DQExpression.Value(0)), DQExpression.Value(-1)))); | |||
| update.Where.Conditions.Add(DQCondition.EQ("ID", detailID)); | |||
| session.ExecuteNonQuery(update); | |||
| var update2 = new DQUpdateDom(typeof(SaleOutStore)); | |||
| update2.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1)))); | |||
| update2.Where.Conditions.Add(DQCondition.EQ("ID", billID)); | |||
| session.ExecuteNonQuery(update2); | |||
| return weightingInfo; | |||
| } | |||
| static Tuple<Money<decimal>?, Money<decimal>?> GetWeightingInfor(IDmoSession session, long detailID) | |||
| { | |||
| var query = new DQueryDom(new JoinAlias(typeof(WeightingInfor))); | |||
| query.Columns.Add(DQSelectColumn.Sum("MainUnitNum")); | |||
| query.Columns.Add(DQSelectColumn.Sum("SecondNumber")); | |||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DetailID", detailID), DQCondition.EQ("BillType", DmoTypeIDAttribute.GetID(typeof(SaleOutStore))))); | |||
| return query.EExecuteScalar<Money<decimal>?, Money<decimal>?>(session); | |||
| } | |||
| private static WeightingInfo_ScanDetail CreateSanDetail(long detailID, Tuple<long, long?, long, string> saleInfo, decimal? mainUnitNum, decimal secondNumber, long? clientID, long? weightRecordID, string code) | |||
| { | |||
| var scan = new WeightingInfo_ScanDetail(); | |||
| scan.BarCode = code; | |||
| scan.BillID = saleInfo.Item1; | |||
| scan.Detail_ID = detailID; | |||
| scan.Goods_ID = saleInfo.Item3; | |||
| scan.Weight = mainUnitNum; | |||
| scan.Number = secondNumber; | |||
| scan.ClientID = clientID; | |||
| scan.WeightRecordID = weightRecordID; | |||
| return scan; | |||
| } | |||
| private static WeightingInfor CreateWeightingInfo(long detailID, Tuple<long, long?, long, string> saleInfo, short billType, decimal? mainUnitNum, decimal? secondNumber, DateTime? weightTime, string product_Batch, long clientID) | |||
| { | |||
| var record = new WeightingInfor(); | |||
| record.BillID = saleInfo.Item1; | |||
| record.BillType = billType; | |||
| record.Customer_ID = saleInfo.Item2; | |||
| record.DetailID = detailID; | |||
| record.Goods_ID = saleInfo.Item3; | |||
| record.MainUnitNum = mainUnitNum; | |||
| record.SecondNumber = secondNumber; | |||
| record.NetWeight = mainUnitNum; | |||
| record.Weight = mainUnitNum; | |||
| record.WeightTime = weightTime; | |||
| record.Unit = saleInfo.Item4; | |||
| record.Type = "客户端"; | |||
| record.Product_Batch = product_Batch; | |||
| record.ClientID = clientID; | |||
| return record; | |||
| } | |||
| static List<MinBarCode> GetBarCodeInfo(string barCode) | |||
| { | |||
| var left = new JoinAlias("_l", typeof(BarCodeProductionInfo)); | |||
| var right = new JoinAlias("_r", typeof(BarCodeProductionInfo)); | |||
| var query = new DQueryDom(left); | |||
| query.From.AddJoin(JoinType.Inner, new DQDmoSource(right), DQCondition.EQ(left, "ID", right, "PackageID")); | |||
| query.Where.Conditions.Add(DQCondition.EQ(left, "BarCode", barCode)); | |||
| var arr = new string[] { "Product_Batch", "BarCode", "Goods_ID", "Weight" }; | |||
| var list = new List<MinBarCode>(); | |||
| foreach (var f in arr) | |||
| query.Columns.Add(DQSelectColumn.Create(DQExpression.Field(right, f), f)); | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| using (var reader = session.ExecuteReader(query)) | |||
| { | |||
| while (reader.Read()) | |||
| { | |||
| var obj = new MinBarCode() | |||
| { | |||
| Product_Batch = Convert.ToString(reader[0]), | |||
| BarCode = Convert.ToString(reader[1]), | |||
| Goods_ID = (long?)reader[2], | |||
| Weight = (decimal?)reader[3] | |||
| }; | |||
| list.Add(obj); | |||
| } | |||
| } | |||
| } | |||
| return list; | |||
| } | |||
| class MinBarCode | |||
| { | |||
| public string Product_Batch { get; set; } | |||
| public string BarCode { get; set; } | |||
| public long? Goods_ID { get; set; } | |||
| public decimal? Weight { get; set; } | |||
| } | |||
| } | |||
| } | |||