|
|
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 Forks.JsonRpc.Client.Data;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using TSingSoft.WebPluginFramework;
|
|
|
|
|
|
namespace BWP.B3ClientService.Rpcs.BillRpc
|
|
|
{
|
|
|
[Rpc]
|
|
|
public static class WeightBillRpc
|
|
|
{
|
|
|
[Rpc]
|
|
|
public static BackRpcObj UploadBill(WeightBill dmo)
|
|
|
{
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
if (dmo.ID == 0)
|
|
|
session.Insert(dmo);
|
|
|
else
|
|
|
{
|
|
|
foreach (var item in dmo.FarmerDetails)
|
|
|
{
|
|
|
if (item.ID == 0)
|
|
|
session.Insert(item);
|
|
|
else
|
|
|
session.Update(item);
|
|
|
}
|
|
|
|
|
|
foreach (var item in dmo.Details)
|
|
|
{
|
|
|
if (item.ID == 0)
|
|
|
session.Insert(item);
|
|
|
else
|
|
|
session.Update(item);
|
|
|
}
|
|
|
dmo.FarmerDetails = new WeightBill_FarmerDetailCollection();
|
|
|
dmo.Details = new WeightBill_DetailCollection();
|
|
|
FillServerUpdateFields(session, dmo);
|
|
|
session.Update(dmo);
|
|
|
|
|
|
}
|
|
|
session.Commit();
|
|
|
}
|
|
|
var result = new BackRpcObj();
|
|
|
result.ID = dmo.ID;
|
|
|
var details = new BackRpcObj() { Flag = "Details" };
|
|
|
foreach (var d in dmo.Details)
|
|
|
details.DetailBack.Add(new BackRpcObj() { Flag = d.Index.ToString(), ID = d.ID });
|
|
|
result.DetailBack.Add(details);
|
|
|
var farmerDetails = new BackRpcObj() { Flag = "FarmerDetails" };
|
|
|
foreach (var d in dmo.FarmerDetails)
|
|
|
farmerDetails.DetailBack.Add(new BackRpcObj() { Flag = d.Index.ToString(), ID = d.ID });
|
|
|
result.DetailBack.Add(farmerDetails);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
static void FillServerUpdateFields(IDmoSession session, WeightBill dmo)
|
|
|
{
|
|
|
var min = InnerBLUtil.GetSingleDmo<WeightBill>(session, "ID", dmo.ID, "B3ID", "DeleteState", "AlreadyHouse", "Inspector_ID", "Inspector_Name");
|
|
|
dmo.B3ID = min.B3ID;
|
|
|
dmo.DeleteState = min.DeleteState;
|
|
|
dmo.AlreadyHouse = min.AlreadyHouse;
|
|
|
dmo.Inspector_ID = min.Inspector_ID;
|
|
|
dmo.Inspector_Name = min.Inspector_Name;
|
|
|
dmo.Sync = false;
|
|
|
dmo.ModifyTime = DateTime.Now;
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static bool DeleteBill(long id)
|
|
|
{
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
DeleteEntity<WeightBill>(session, "ID", id);
|
|
|
DeleteEntity<WeightBill_Detail>(session, "WeightBill_ID", id);
|
|
|
DeleteEntity<WeightBill_FarmerDetail>(session, "WeightBill_ID", id);
|
|
|
DeleteEntity<WeightBill_HouseDetail>(session, "WeightBill_ID", id);
|
|
|
DeleteEntity<WeightBill_SanctionDetail>(session, "WeightBill_ID", id);
|
|
|
session.Commit();
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
static void DeleteEntity<T>(IDmoSession session, string property, object value)
|
|
|
where T : SyncBase
|
|
|
{
|
|
|
var update = new DQUpdateDom(typeof(T));
|
|
|
update.Columns.Add(new DQUpdateColumn("DeleteState", true));
|
|
|
update.Where.Conditions.Add(DQCondition.EQ(property, value));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static List<WeightBill> GetWeightBillList(DateTime date)
|
|
|
{
|
|
|
var query = new DmoQuery(typeof(WeightBill));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.Between("WeighTime", date.Date, date.Date + new TimeSpan(23, 59, 29))));
|
|
|
return query.EExecuteList().Cast<WeightBill>().ToList();
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static List<Tuple<long, long>> SyncBillB3Ids(List<long> ids)
|
|
|
{
|
|
|
return GetSyncBaseB3Ids<WeightBill>(ids);
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static List<Tuple<long, long>> SyncWeightDetailB3Ids(List<long> ids)
|
|
|
{
|
|
|
return GetSyncBaseB3Ids<WeightBill_Detail>(ids);
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static List<Tuple<long, long>> SyncFarmerDetailB3Ids(List<long> ids)
|
|
|
{
|
|
|
return GetSyncBaseB3Ids<WeightBill_FarmerDetail>(ids);
|
|
|
}
|
|
|
|
|
|
static List<Tuple<long, long>> GetSyncBaseB3Ids<T>(List<long> ids)
|
|
|
where T : SyncBase
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias(typeof(T)));
|
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("B3ID"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("B3ID")), DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray())));
|
|
|
return query.EExecuteList<long, long>();
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static List<Tuple<long, long, int>> SyncWeightDetailFirstNumber(List<long> detailIds)
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias(typeof(WeightBill_Detail)));
|
|
|
query.Columns.Add(DQSelectColumn.Field("WeightBill_ID"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("Number"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("Number")), DQCondition.InList(DQExpression.Field("ID"), detailIds.Select(x => DQExpression.Value(x)).ToArray())));
|
|
|
return query.EExecuteList<long, long, int>();
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static List<WeightWithHouseDetail> GetNoHouseInfoWeightBills(DateTime date)
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias(typeof(WeightBill)));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.Between("WeighTime", date.Date, date.Date + new TimeSpan(23, 59, 29)))));
|
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("B3ID"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("Supplier_Name"));
|
|
|
query.Columns.Add(DQSelectColumn.Field("AlreadyHouse"));
|
|
|
var result = query.EExecuteList<long, long?, string, bool>();
|
|
|
var list = new List<WeightWithHouseDetail>();
|
|
|
foreach (var item in result)
|
|
|
{
|
|
|
var bill = new WeightWithHouseDetail();
|
|
|
var entity = new WeightBill();
|
|
|
bill.Bill = entity;
|
|
|
list.Add(bill);
|
|
|
entity.ID = item.Item1;
|
|
|
entity.B3ID = item.Item2;
|
|
|
entity.Supplier_Name = item.Item3;
|
|
|
entity.AlreadyHouse = item.Item4;
|
|
|
entity.FirstWeightNumber = GetWeightDetailFirstNumber(entity.ID);
|
|
|
}
|
|
|
var details = GetWeightHouseDetail(list.Select(x => x.Bill.ID));
|
|
|
foreach (var item in list)
|
|
|
{
|
|
|
var tags = details.Where(x => x.WeightBill_ID == item.Bill.ID);
|
|
|
if (tags.Any())
|
|
|
item.Detail = tags.ToArray();
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
static int? GetWeightDetailFirstNumber(long id)
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias(typeof(WeightBill_Detail)));
|
|
|
query.Columns.Add(DQSelectColumn.Field("Number"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("WeightBill_ID", id)));
|
|
|
query.Range = SelectRange.Top(1);
|
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID"));
|
|
|
return query.EExecuteScalar<int?>();
|
|
|
}
|
|
|
|
|
|
static IEnumerable<WeightBill_HouseDetail> GetWeightHouseDetail(IEnumerable<long> billIDs)
|
|
|
{
|
|
|
var query = new DmoQuery(typeof(WeightBill_HouseDetail));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.InList(DQExpression.Field("WeightBill_ID"), billIDs.Select(x => DQExpression.Value(x)).ToArray())));
|
|
|
return query.EExecuteList().Cast<WeightBill_HouseDetail>();
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static int UpdateInsertWeightBillHouseDetail(WeightBill bo)
|
|
|
{
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
var houseDetail = GetList<WeightBill_HouseDetail>(new Tuple<string, object>("WeightBill_ID", bo.ID), new Tuple<string, object>("DeleteState", false));
|
|
|
|
|
|
foreach (var item in bo.HouseDetails)
|
|
|
{
|
|
|
var first = houseDetail.FirstOrDefault(x => x.LiveColonyHouse_ID == item.LiveColonyHouse_ID);
|
|
|
if (first == null)
|
|
|
houseDetail.Add(item);
|
|
|
else
|
|
|
first.Index = item.Index;
|
|
|
}
|
|
|
var delete = new List<long>();
|
|
|
foreach (var item in houseDetail)
|
|
|
{
|
|
|
if (bo.HouseDetails.Any(x => x.LiveColonyHouse_ID == item.LiveColonyHouse_ID))
|
|
|
UpdateOrInsertHouseDetail(session, item);
|
|
|
else
|
|
|
delete.Add(item.LiveColonyHouse_ID ?? 0);
|
|
|
}
|
|
|
|
|
|
if (delete.Any())
|
|
|
{
|
|
|
Delete<WeightBill_HouseDetail>(session, (dom) =>
|
|
|
{
|
|
|
dom.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("WeightBill_ID", bo.ID)));
|
|
|
dom.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("LiveColonyHouse_ID"), delete.Select(x => DQExpression.Value(x)).ToArray()));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
var sanctionDetail = GetList<WeightBill_SanctionDetail>(new Tuple<string, object>("WeightBill_ID", bo.ID), new Tuple<string, object>("DeleteState", false));
|
|
|
|
|
|
foreach (var item in bo.SanctionDetails)
|
|
|
{
|
|
|
var first = sanctionDetail.FirstOrDefault(x => x.AbnormalItem_ID == item.AbnormalItem_ID);
|
|
|
if (first == null)
|
|
|
sanctionDetail.Add(item);
|
|
|
else
|
|
|
{
|
|
|
first.Index = item.Index;
|
|
|
first.Number = item.Number;
|
|
|
}
|
|
|
}
|
|
|
delete.Clear();
|
|
|
foreach (var item in sanctionDetail)
|
|
|
{
|
|
|
if (bo.SanctionDetails.Any(x => x.AbnormalItem_ID == item.AbnormalItem_ID))
|
|
|
UpdateOrInsertSanctionDetail(session, item);
|
|
|
else
|
|
|
delete.Add(item.AbnormalItem_ID ?? 0);
|
|
|
}
|
|
|
|
|
|
if (delete.Any())
|
|
|
{
|
|
|
Delete<WeightBill_SanctionDetail>(session, (dom) =>
|
|
|
{
|
|
|
dom.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("WeightBill_ID", bo.ID)));
|
|
|
dom.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("AbnormalItem_ID"), delete.Select(x => DQExpression.Value(x)).ToArray()));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var firstID = GetWeightDetailFirstID(bo.ID);
|
|
|
if (firstID.HasValue)
|
|
|
{
|
|
|
var dUpdate = new DQUpdateDom(typeof(WeightBill_Detail));
|
|
|
dUpdate.Columns.Add(new DQUpdateColumn("Number", bo.FirstWeightNumber));
|
|
|
dUpdate.Where.Conditions.Add(DQCondition.EQ("ID", firstID));
|
|
|
session.ExecuteNonQuery(dUpdate);
|
|
|
}
|
|
|
var update = new DQUpdateDom(typeof(WeightBill));
|
|
|
update.Columns.Add(new DQUpdateColumn("HogGrade_ID", bo.HogGrade_ID));
|
|
|
update.Columns.Add(new DQUpdateColumn("HogGrade_Name", bo.HogGrade_Name));
|
|
|
update.Columns.Add(new DQUpdateColumn("Inspector_ID", bo.Inspector_ID));
|
|
|
update.Columns.Add(new DQUpdateColumn("Inspector_Name", bo.Inspector_Name));
|
|
|
update.Columns.Add(new DQUpdateColumn("AlreadyHouse", true));
|
|
|
update.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
|
update.Columns.Add(new DQUpdateColumn("ModifyTime", DateTime.Now));
|
|
|
update.Where.Conditions.Add(DQCondition.EQ("ID", bo.ID));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
session.Commit();
|
|
|
}
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
static List<T> GetList<T>(params Tuple<string, object>[] condition)
|
|
|
{
|
|
|
var query = new DmoQuery(typeof(T));
|
|
|
if (condition.Any())
|
|
|
query.Where.Conditions.Add(DQCondition.And(condition.Select(x => DQCondition.EQ(x.Item1, x.Item2))));
|
|
|
return query.EExecuteList().Cast<T>().ToList();
|
|
|
}
|
|
|
|
|
|
static void Delete<T>(IDmoSession session, Action<DQUpdateDom> conditions)
|
|
|
where T : SyncBase
|
|
|
{
|
|
|
var delete = new DQUpdateDom(typeof(T));
|
|
|
delete.Columns.Add(new DQUpdateColumn("DeleteState", true));
|
|
|
conditions(delete);
|
|
|
session.ExecuteNonQuery(delete);
|
|
|
}
|
|
|
|
|
|
static void UpdateOrInsertHouseDetail(IDmoSession session, WeightBill_HouseDetail detail)
|
|
|
{
|
|
|
if (detail.ID == 0)
|
|
|
{
|
|
|
session.Insert(detail);
|
|
|
return;
|
|
|
}
|
|
|
var update = new DQUpdateDom(typeof(WeightBill_HouseDetail));
|
|
|
update.Columns.Add(new DQUpdateColumn("Index", detail.Index));
|
|
|
update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("LiveColonyHouse_ID", detail.LiveColonyHouse_ID)));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
}
|
|
|
|
|
|
static void UpdateOrInsertSanctionDetail(IDmoSession session, WeightBill_SanctionDetail detail)
|
|
|
{
|
|
|
if (detail.ID == 0)
|
|
|
{
|
|
|
session.Insert(detail);
|
|
|
return;
|
|
|
}
|
|
|
var update = new DQUpdateDom(typeof(WeightBill_SanctionDetail));
|
|
|
update.Columns.Add(new DQUpdateColumn("Index", detail.Index));
|
|
|
update.Columns.Add(new DQUpdateColumn("Number", detail.Number));
|
|
|
update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("AbnormalItem_ID", detail.AbnormalItem_ID)));
|
|
|
session.ExecuteNonQuery(update);
|
|
|
}
|
|
|
|
|
|
static long? GetWeightDetailFirstID(long id)
|
|
|
{
|
|
|
var query = new DQueryDom(new JoinAlias(typeof(WeightBill_Detail)));
|
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("DeleteState", false), DQCondition.EQ("WeightBill_ID", id)));
|
|
|
query.Range = SelectRange.Top(1);
|
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID"));
|
|
|
return query.EExecuteScalar<long?>();
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static int GetDetailTotalNumber(DateTime date)
|
|
|
{
|
|
|
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(detail), DQCondition.EQ(main, "ID", detail, "WeightBill_ID"));
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.And(DQCondition.EQ(detail, "DeleteState", false), DQCondition.EQ("DeleteState", false), DQCondition.Between("WeighTime", date.Date, date.Date + new TimeSpan(23, 59, 29)))));
|
|
|
query.Columns.Add(DQSelectColumn.Sum(detail, "Number"));
|
|
|
var result = query.EExecuteScalar();
|
|
|
|
|
|
if (result == null)
|
|
|
return 0;
|
|
|
return Convert.ToInt32(result);
|
|
|
}
|
|
|
|
|
|
[Rpc]
|
|
|
public static WeightWithHouseAndSanction GetWeightBillOnHousePage(long id)
|
|
|
{
|
|
|
var result = new WeightWithHouseAndSanction();
|
|
|
using (var session = Dmo.NewSession())
|
|
|
{
|
|
|
var entity = new WeightBill();
|
|
|
entity.HogGrade_ID = InnerBLUtil.GetDmoPropertyByID<long?>(session, typeof(WeightBill), "HogGrade_ID", id);
|
|
|
entity.FirstWeightNumber = GetWeightDetailFirstNumber(id);
|
|
|
result.Bill = entity;
|
|
|
result.Detail = InnerBLUtil.GetPartialFieldsDmoList<WeightBill_HouseDetail>(session, (query) =>
|
|
|
{
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("WeightBill_ID", id), DQCondition.EQ("DeleteState", false)));
|
|
|
}, "ID", "LiveColonyHouse_ID").ToArray();
|
|
|
|
|
|
result.SanctionDetail = InnerBLUtil.GetPartialFieldsDmoList<WeightBill_SanctionDetail>(session, (query) =>
|
|
|
{
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("WeightBill_ID", id), DQCondition.EQ("DeleteState", false)));
|
|
|
}, "ID", "AbnormalItem_ID", "Number").ToArray();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[RpcObject]
|
|
|
public class WeightWithHouseDetail
|
|
|
{
|
|
|
public WeightBill Bill { get; set; }
|
|
|
|
|
|
public WeightBill_HouseDetail[] Detail { get; set; }
|
|
|
}
|
|
|
|
|
|
[RpcObject]
|
|
|
public class WeightWithHouseAndSanction : WeightWithHouseDetail
|
|
|
{
|
|
|
public WeightBill_SanctionDetail[] SanctionDetail { get; set; }
|
|
|
}
|
|
|
}
|