| @ -0,0 +1,220 @@ | |||
| using BWP.B3ClientService.BO; | |||
| using BWP.B3Frameworks.Utils; | |||
| using Forks.EnterpriseServices.DomainObjects2; | |||
| using Forks.EnterpriseServices.DomainObjects2.DQuery; | |||
| using Forks.EnterpriseServices.JsonRpc; | |||
| using Forks.EnterpriseServices.SqlDoms; | |||
| using Newtonsoft.Json; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using TSingSoft.WebPluginFramework; | |||
| namespace BWP.B3ClientService.Rpcs | |||
| { | |||
| [Rpc] | |||
| public static class SelfHelpRpc | |||
| { | |||
| [Rpc(RpcFlags.SkipAuth)] | |||
| public static string GetViewEntity(string idCard) | |||
| { | |||
| var q1 = GetWeightQueryDom(idCard); | |||
| q1.UnionNext.Select = GetSendPigQueryDom(idCard); | |||
| var lst = new List<ViewEntity>(); | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| using (var reader = session.ExecuteReader(q1)) | |||
| { | |||
| while (reader.Read()) | |||
| { | |||
| var id = (long)reader[0]; | |||
| var typeID = Convert.ToInt32(reader[1]); | |||
| var first = lst.FirstOrDefault(x => x.ID == id && x.BillType == typeID); | |||
| if (first == null) | |||
| { | |||
| first = new ViewEntity() { ID = id, BillType = typeID }; | |||
| first.CarNumber = (string)reader[2]; | |||
| first.Date = (DateTime)reader[4]; | |||
| lst.Add(first); | |||
| } | |||
| first.Number += (int?)reader[3] ?? 0; | |||
| } | |||
| } | |||
| } | |||
| return JsonConvert.SerializeObject(lst); | |||
| } | |||
| static DQueryDom GetWeightQueryDom(string idCard) | |||
| { | |||
| var m1 = new JoinAlias(typeof(WeightBill)); | |||
| var d1 = new JoinAlias(typeof(WeightBill_FarmerDetail)); | |||
| var car1 = new JoinAlias("car1", typeof(Car)); | |||
| var supper1 = new JoinAlias("supper1", typeof(Supplier)); | |||
| var employee1 = new JoinAlias("emp1", typeof(MinEmployee)); | |||
| var q1 = new DQueryDom(m1); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(d1), DQCondition.EQ(m1, "ID", d1, "WeightBill_ID")); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(car1), DQCondition.EQ(car1, "ID", m1, "Car_ID")); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(supper1), DQCondition.EQ(supper1, "ID", m1, "Supplier_ID")); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(employee1), DQCondition.EQ(employee1, "ID", m1, "Employee_ID")); | |||
| q1.Columns.Add(DQSelectColumn.Field("ID")); | |||
| q1.Columns.Add(DQSelectColumn.Create(DQExpression.Value(1), "BillType")); | |||
| q1.Columns.Add(DQSelectColumn.Field("Name",car1)); | |||
| q1.Columns.Add(DQSelectColumn.Field("Number", d1)); | |||
| q1.Columns.Add(DQSelectColumn.Field("WeighTime")); | |||
| q1.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("PrintNumber", 0), DQCondition.EQ("DeleteState", false))); | |||
| q1.Where.Conditions.Add(DQCondition.Or(DQCondition.EQ(car1, "Driver_IDCard", idCard), DQCondition.EQ(supper1, "IDCardNumber", idCard), DQCondition.EQ(employee1, "IDCardNumber", idCard))); | |||
| return q1; | |||
| } | |||
| static DQueryDom GetSendPigQueryDom(string idCard) | |||
| { | |||
| var m1 = new JoinAlias(typeof(SendPigRecord)); | |||
| var d1 = new JoinAlias(typeof(FarmerRecord)); | |||
| var car2 = new JoinAlias("car2", typeof(Car)); | |||
| var supper2 = new JoinAlias("supper2", typeof(Supplier)); | |||
| var employee2 = new JoinAlias("emp2", typeof(MinEmployee)); | |||
| var q1 = new DQueryDom(m1); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(d1), DQCondition.EQ(m1, "ID", d1, "SendPigRecord_ID")); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(car2), DQCondition.EQ(m1, "carID", car2, "ID")); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(supper2), DQCondition.EQ(m1, "supplierID", supper2, "ID")); | |||
| q1.From.AddJoin(JoinType.Left, new DQDmoSource(employee2), DQCondition.EQ(m1, "employeeID", employee2, "ID")); | |||
| q1.Columns.Add(DQSelectColumn.Field("ID")); | |||
| q1.Columns.Add(DQSelectColumn.Create(DQExpression.Value(0), "BillType")); | |||
| q1.Columns.Add(DQSelectColumn.Field("Name",car2)); | |||
| q1.Columns.Add(DQSelectColumn.Field("number", d1)); | |||
| q1.Columns.Add(DQSelectColumn.Field("BillTime")); | |||
| q1.Where.Conditions.Add(DQCondition.IsNull(DQExpression.Field("WeightBill_ID"))); | |||
| q1.Where.Conditions.Add(DQCondition.Or(DQCondition.EQ(car2, "Driver_IDCard", idCard), DQCondition.EQ(supper2, "IDCardNumber", idCard), DQCondition.EQ(employee2, "IDCardNumber", idCard))); | |||
| return q1; | |||
| } | |||
| [Rpc(RpcFlags.SkipAuth)] | |||
| public static string CreateWeightBill(long id) | |||
| { | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| var entity = Load<SendPigRecord>(session, new Tuple<string, object>("ID", id)); | |||
| var details = LoadList<FarmerRecord>(session, new Tuple<string, object>("SendPigRecord_ID", id)); | |||
| var weight = new WeightBill(); | |||
| weight.ModifyTime = DateTime.Now; | |||
| weight.AccountingUnit_ID = 1; | |||
| weight.Department_ID = 2; | |||
| weight.Employee_Name = entity.employee; | |||
| weight.Employee_ID = entity.employeeID; | |||
| weight.WeighTime = DateTime.Now; | |||
| weight.Supplier_ID = entity.supplierID; | |||
| weight.Supplier_Name = entity.supplier; | |||
| weight.BankAccount = entity.supplierIDCard; | |||
| weight.PurchaseType_ID = entity.bType + 1; | |||
| weight.PurchaseType_Name = entity.bType == 0 ? "社会" : "业务"; | |||
| weight.Car_Name = entity.carNum; | |||
| weight.Car_ID = entity.carID; | |||
| weight.JingJianFee = entity.jjf; | |||
| weight.DiscontMoney = entity.discont; | |||
| weight.ShackWeight = details.Sum(x => x.weight ?? 0); | |||
| weight.ShackMoney = details.Sum(x => x.money ?? 0); | |||
| if (weight.ShackWeight != 0) | |||
| { | |||
| weight.ShackPrice = (weight.ShackMoney + (weight.JingJianFee ?? 0) - (weight.DiscontMoney ?? 0)) / weight.ShackWeight; | |||
| if (weight.ShackPrice.HasValue) | |||
| weight.ShackPrice = decimal.Round(weight.ShackPrice.Value, 2); | |||
| } | |||
| weight.AnimalTestNumber = entity.testNo; | |||
| weight.AnimalTestMan = entity.testMan; | |||
| if (!string.IsNullOrEmpty(entity.testDate)) | |||
| weight.AnimalTestDate = DateTime.Parse(entity.testDate); | |||
| if (details.Any()) | |||
| weight.Farmer_ID = details.First().farmerID; | |||
| session.Insert(weight); | |||
| foreach (var item in details) | |||
| { | |||
| var d = new WeightBill_FarmerDetail(); | |||
| d.WeightBill_ID = weight.ID; | |||
| d.Index = item.fidx; | |||
| d.Farmer_ID = item.farmerID; | |||
| d.Farmer_Name = item.fName; | |||
| d.Number = item.number; | |||
| d.Weight = item.weight; | |||
| d.Money = item.money; | |||
| d.Farmer_IDCard = item.fIDCard; | |||
| d.Farmer_Tel = item.fTel; | |||
| d.Farmer_BankAccount = item.bankNum; | |||
| d.Farmer_Address = item.fAddress; | |||
| session.Insert(d); | |||
| weight.FarmerDetails.Add(d); | |||
| } | |||
| FillSendPigWeightID(session, id, weight.ID); | |||
| session.Commit(); | |||
| return JsonConvert.SerializeObject(weight); | |||
| } | |||
| } | |||
| private static void FillSendPigWeightID(IDmoSession session, long id, long weightID) | |||
| { | |||
| var update = new DQUpdateDom(typeof(SendPigRecord)); | |||
| update.Columns.Add(new DQUpdateColumn("WeightBill_ID", weightID)); | |||
| update.Where.Conditions.Add(DQCondition.EQ("ID", id)); | |||
| session.ExecuteNonQuery(update); | |||
| } | |||
| static T Load<T>(IDmoSession session, params Tuple<string, object>[] parameters) | |||
| where T : class | |||
| { | |||
| var query = new DmoQuery(typeof(T)); | |||
| foreach (var item in parameters) | |||
| query.Where.Conditions.Add(DQCondition.EQ(item.Item1, item.Item2)); | |||
| query.Range = SelectRange.Top(1); | |||
| var r = session.ExecuteScalar(query); | |||
| if (r != null) | |||
| return r as T; | |||
| return null; | |||
| } | |||
| static IEnumerable<T> LoadList<T>(IDmoSession session, params Tuple<string, object>[] parameters) | |||
| { | |||
| var query = new DmoQuery(typeof(T)); | |||
| foreach (var item in parameters) | |||
| query.Where.Conditions.Add(DQCondition.EQ(item.Item1, item.Item2)); | |||
| return session.ExecuteList(query).Cast<T>(); | |||
| } | |||
| [Rpc(RpcFlags.SkipAuth)] | |||
| public static string GetWeightBill(long id) | |||
| { | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| var dmo = Load<WeightBill>(session, new Tuple<string, object>("ID", id)); | |||
| var w = LoadList<WeightBill_Detail>(session, new Tuple<string, object>("WeightBill_ID", id), new Tuple<string, object>("DeleteState", false)); | |||
| var f = LoadList<WeightBill_FarmerDetail>(session, new Tuple<string, object>("WeightBill_ID", id), new Tuple<string, object>("DeleteState", false)); | |||
| dmo.Details.AddRange(w); | |||
| dmo.FarmerDetails.AddRange(f); | |||
| return JsonConvert.SerializeObject(dmo); | |||
| } | |||
| } | |||
| [Rpc(RpcFlags.SkipAuth)] | |||
| public static string GetWeightDetail(long id) | |||
| { | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| var f = LoadList<WeightBill_Detail>(session, new Tuple<string, object>("WeightBill_ID", id), new Tuple<string, object>("DeleteState", false)); | |||
| return JsonConvert.SerializeObject(f); | |||
| } | |||
| } | |||
| class ViewEntity | |||
| { | |||
| public long ID { get; set; } | |||
| public int BillType { get; set; } | |||
| public string CarNumber { get; set; } | |||
| public int Number { get; set; } | |||
| public DateTime Date { get; set; } | |||
| } | |||
| } | |||
| } | |||