using BO.BO.BaseInfo;
|
|
using BO.BO.Bill;
|
|
using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using Forks.EnterpriseServices.SqlDoms;
|
|
using Forks.JsonRpc.Client;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Script.Serialization;
|
|
using TSingSoft.WebPluginFramework;
|
|
|
|
namespace BO.Utils.BillRpc
|
|
{
|
|
public static class GradeAndWeightRpc
|
|
{
|
|
static JavaScriptSerializer serializer = new JavaScriptSerializer();
|
|
|
|
public static List<GradeAndWeight> GetGradeAndWeightList(DateTime date, bool isTang)
|
|
{
|
|
const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/GetGradeAndWeightList";
|
|
var result = RpcFacade.Call<string>(method, date, isTang);
|
|
var list = serializer.Deserialize<List<GradeAndWeight>>(result);
|
|
short type = 1;
|
|
if (isTang)
|
|
type = 0;
|
|
var sum = GetAlreadyNumber(date, type);
|
|
var finish = GetFinishRelate(date, type);
|
|
foreach (var item in list)
|
|
{
|
|
var first = sum.FirstOrDefault(x => x.Item1 == item.Order);
|
|
if (first != null)
|
|
item.Already = first.Item2;
|
|
item.Finish = finish.Contains(item.Order);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
static List<int> GetFinishRelate(DateTime date, short type)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(OrderGradeFinishRelate)));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", date), DQCondition.EQ("Technics", type)));
|
|
query.Columns.Add(DQSelectColumn.Field("Order"));
|
|
var list = new List<int>();
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
list.Add((int)reader[0]);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
static List<Tuple<int, int>> GetAlreadyNumber(DateTime date, short type)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date), DQCondition.EQ("Technics", type), DQCondition.IsNotNull(DQExpression.Field("Order"))));
|
|
query.Columns.Add(DQSelectColumn.Field("Order"));
|
|
query.Columns.Add(DQSelectColumn.Count());
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Order"));
|
|
var list = new List<Tuple<int, int>>();
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
list.Add(new Tuple<int, int>((int)reader[0], Convert.ToInt32(reader[1])));
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
|
|
public static List<BodyDiscontItem> GetBodyDiscontItem()
|
|
{
|
|
const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/GetBodyDiscontItemSetting";
|
|
var json = RpcFacade.Call<string>(method);
|
|
return serializer.Deserialize<List<BodyDiscontItem>>(json);
|
|
}
|
|
|
|
public static void SaveBodyDiscontItem(List<CTuple<long, decimal?>> list)
|
|
{
|
|
const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/SaveBodyDiscontItemSetting";
|
|
RpcFacade.Call<int>(method, serializer.Serialize(list));
|
|
}
|
|
|
|
public static List<DataConfirmList> GetDataConfirmList(DateTime date, int? order)
|
|
{
|
|
const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/GetDataConfirmList";
|
|
try
|
|
{
|
|
var result = RpcFacade.Call<string>(method, date, order);
|
|
return serializer.Deserialize<List<DataConfirmList>>(result);
|
|
}
|
|
catch
|
|
{
|
|
return new List<DataConfirmList>();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class LocalGradeAndWeightBL
|
|
{
|
|
static JavaScriptSerializer serializer = new JavaScriptSerializer();
|
|
|
|
public static void SetGradeFinish(int order, DateTime date, short technics)
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
if (Finished(session, date, order, technics))
|
|
return;
|
|
session.Insert(new OrderGradeFinishRelate(order, date, technics));
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static bool Finished(IDmoSession session, DateTime date, int order, short technics)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(OrderGradeFinishRelate)));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Order", order), DQCondition.EQ("Date", date), DQCondition.EQ("Technics", technics)));
|
|
query.Columns.Add(DQSelectColumn.Create(DQExpression.Value("1"), "c"));
|
|
return session.ExecuteScalar(query) != null;
|
|
}
|
|
|
|
public static void DeleteBySID(long sid)
|
|
{
|
|
var updateDom = new DQUpdateDom(typeof(GradeAndWeight_Detail));
|
|
updateDom.Where.Conditions.Add(DQCondition.EQ("SID", sid));
|
|
updateDom.Columns.Add(new DQUpdateColumn("IsDeleted", true));
|
|
updateDom.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
session.ExecuteNonQuery(updateDom);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static BindingList<GradeAndWeight_Detail> GetDetails(DateTime date, int top = 15)
|
|
{
|
|
var bindList = new BindingList<GradeAndWeight_Detail>();
|
|
var list = new List<GradeAndWeight_Detail>();
|
|
|
|
var query = new DmoQuery(typeof(GradeAndWeight_Detail));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date)));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("Index", true));
|
|
query.Range = SelectRange.Top(top);
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
list = session.ExecuteList(query).Cast<GradeAndWeight_Detail>().ToList();
|
|
}
|
|
foreach (GradeAndWeight_Detail detail in list)
|
|
{
|
|
bindList.Add(detail);
|
|
}
|
|
return bindList;
|
|
}
|
|
|
|
public static List<Tuple<short, int, decimal>> GetSumNumber(DateTime date, int? order, short? type)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date), DQCondition.IsNotNull(DQExpression.Field("Technics"))));
|
|
if (order.HasValue)
|
|
query.Where.Conditions.Add(DQCondition.EQ("Order", order));
|
|
if (type.HasValue)
|
|
query.Where.Conditions.Add(DQCondition.EQ("Technics", type));
|
|
query.Columns.Add(DQSelectColumn.Field("Technics"));
|
|
query.Columns.Add(DQSelectColumn.Count());
|
|
query.Columns.Add(DQSelectColumn.Sum("Weight"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Technics"));
|
|
var list = new List<Tuple<short, int, decimal>>();
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
decimal w = 0;
|
|
if (reader[2] != null)
|
|
w = Convert.ToDecimal(reader[2]);
|
|
list.Add(new Tuple<short, int, decimal>((short)reader[0], Convert.ToInt32(reader[1]), w));
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
|
|
public static void Insert(GradeAndWeight_Detail detail)
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
if (detail.SID != 0)
|
|
throw new Exception("Insert时要保证SID不为0");
|
|
session.Insert(detail);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static void Update(GradeAndWeight_Detail detail, params string[] properties)
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
if (detail.SID == 0)
|
|
throw new Exception("Update时要保证SID不能为0");
|
|
if (properties.Contains("ID"))
|
|
throw new Exception("ID不能通过该方法维护");
|
|
if (properties.Length == 0)
|
|
throw new Exception("Update时要给出属性数组");
|
|
|
|
var type = typeof(GradeAndWeight_Detail);
|
|
var update = new DQUpdateDom(type);
|
|
update.Where.Conditions.Add(DQCondition.EQ("SID", detail.SID));
|
|
foreach (var p in properties)
|
|
{
|
|
update.Columns.Add(new DQUpdateColumn(p, type.GetProperty(p).GetValue(detail)));
|
|
}
|
|
update.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
session.ExecuteNonQuery(update);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
//public static void Update(GradeAndWeight_Detail detail, GradeAndWeight toEntity)
|
|
//{
|
|
// if (toEntity == null)
|
|
// {
|
|
// throw new Exception(string.Format("请选择左侧的{0}顺序", detail.Technics_Name));
|
|
// }
|
|
// detail.Order = toEntity.Order;
|
|
// detail.OrderDetail_ID = toEntity.OrderDetail_ID;
|
|
// Update(detail, "OrderDetail_ID", "Order", "Livestock_ID", "Livestock_Name", "Technics", "Technics_Name");
|
|
//}
|
|
|
|
public static void Sync()
|
|
{
|
|
ClearUnSyncDelete();
|
|
var syncs = GetAllNeedSyncDetails();
|
|
foreach (var detail in syncs)
|
|
{
|
|
const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/GradeAndWeightRpc/UpdateOrInsertDetailOrDelete";
|
|
var id = RpcFacade.Call<long>(method, JsonConvert.SerializeObject(detail));
|
|
if (detail.IsDeleted)
|
|
Delete(detail.SID);
|
|
else
|
|
SetDetailSynced(detail, id);
|
|
}
|
|
}
|
|
|
|
static void ClearUnSyncDelete()
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
var delete = new DQDeleteDom(typeof(GradeAndWeight_Detail));
|
|
delete.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", true), DQCondition.EQ("ID", 0)));
|
|
session.ExecuteNonQuery(delete);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static void Delete(long sid)
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
var delete = new DQDeleteDom(typeof(GradeAndWeight_Detail));
|
|
delete.Where.Conditions.Add(DQCondition.EQ("SID", sid));
|
|
session.ExecuteNonQuery(delete);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static IEnumerable<GradeAndWeight_Detail> GetAllNeedSyncDetails()
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
var query = new DmoQuery(typeof(GradeAndWeight_Detail));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("SID"));
|
|
query.Range = SelectRange.Top(1);
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Sync", false),
|
|
DQCondition.IsNotNull(DQExpression.Field("Technics"))));
|
|
return session.ExecuteList(query).Cast<GradeAndWeight_Detail>();
|
|
}
|
|
}
|
|
|
|
static void SetDetailSynced(GradeAndWeight_Detail detail, long id)
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
var update = new DQUpdateDom(typeof(GradeAndWeight_Detail));
|
|
update.Columns.Add(new DQUpdateColumn("Sync", true));
|
|
if (detail.ID == 0)
|
|
{
|
|
detail.ID = id;
|
|
update.Columns.Add(new DQUpdateColumn("ID", id));
|
|
}
|
|
update.Where.Conditions.Add(DQCondition.EQ("SID", detail.SID));
|
|
session.ExecuteNonQuery(update);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static Dictionary<int, int> GetOrderIdx(DateTime date)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
|
|
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", date), DQCondition.IsNotNull(DQExpression.Field("Order")), DQCondition.IsNotNull(DQExpression.Field("OrderIndex"))));
|
|
query.Columns.Add(DQSelectColumn.Field("Order"));
|
|
query.Columns.Add(DQSelectColumn.Max("OrderIndex"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Order"));
|
|
var dic = new Dictionary<int, int>();
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
dic.Add((int)reader[0], (int)reader[1]);
|
|
}
|
|
}
|
|
return dic;
|
|
}
|
|
|
|
public static List<GradeAndWeight_Detail> GetGradeAndWeightDetails(DateTime date, int? order, int? index, short? type)
|
|
{
|
|
var query = new DmoQuery(typeof(GradeAndWeight_Detail));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("IsDeleted", false), DQCondition.EQ("Date", date)));
|
|
if (order.HasValue)
|
|
query.Where.Conditions.Add(DQCondition.Or(DQCondition.IsNull(DQExpression.Field("Order")), DQCondition.EQ("Order", order)));
|
|
if (index.HasValue)
|
|
query.Where.Conditions.Add(DQCondition.EQ("Index", index));
|
|
if (type.HasValue)
|
|
query.Where.Conditions.Add(DQCondition.Or(DQCondition.IsNull(DQExpression.Field("Technics")), DQCondition.EQ("Technics", type)));
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
return session.ExecuteList(query).Cast<GradeAndWeight_Detail>().ToList();
|
|
}
|
|
}
|
|
|
|
public static void InsertOrUpdate(GradeAndWeight_Detail entity)
|
|
{
|
|
if (entity.SID == 0)
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
session.Insert(entity);
|
|
session.Commit();
|
|
}
|
|
}
|
|
else
|
|
Update(entity, "Technics", "Technics_Name", "Index", "Order", "Weight", "Livestock_ID", "Livestock_Name");
|
|
}
|
|
|
|
public static void UpdateHistory(DateTime dateTime)
|
|
{
|
|
var lastOrder = 0;
|
|
var orderIdx = 1;
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
if (CheckTodayUpdated(dateTime, session))
|
|
return;
|
|
|
|
var list = Get20180411Data(dateTime, session);
|
|
foreach (var item in list)
|
|
{
|
|
if (item.Item1 == lastOrder)
|
|
{
|
|
orderIdx++;
|
|
UpdateOrderIdx(item.Item2, orderIdx, session);
|
|
}
|
|
else
|
|
{
|
|
lastOrder = item.Item1;
|
|
orderIdx = 1;
|
|
UpdateOrderIdx(item.Item2, orderIdx, session);
|
|
}
|
|
}
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static bool CheckTodayUpdated(DateTime dateTime, IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
|
|
query.Columns.Add(DQSelectColumn.Field("SID"));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", dateTime), DQCondition.IsNotNull(DQExpression.Field("OrderIndex"))));
|
|
return session.ExecuteScalar(query) != null;
|
|
}
|
|
|
|
private static void UpdateOrderIdx(long sid, int orderIdx, IDmoSession session)
|
|
{
|
|
var update = new DQUpdateDom(typeof(GradeAndWeight_Detail));
|
|
update.Where.Conditions.Add(DQCondition.EQ("SID", sid));
|
|
update.Columns.Add(new DQUpdateColumn("OrderIndex", orderIdx));
|
|
session.ExecuteNonQuery(update);
|
|
}
|
|
|
|
static List<Tuple<int, long>> Get20180411Data(DateTime dateTime, IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(GradeAndWeight_Detail)));
|
|
query.Columns.Add(DQSelectColumn.Field("Order"));
|
|
query.Columns.Add(DQSelectColumn.Field("SID"));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("Order"));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("SID"));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Date", dateTime), DQCondition.IsNotNull(DQExpression.Field("Order"))));
|
|
var list = new List<Tuple<int, long>>();
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
list.Add(new Tuple<int, long>((int)reader[0], (long)reader[1]));
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static List<ProductBatch> GetProductBatch()
|
|
{
|
|
var query = new DmoQuery(typeof(ProductBatch));
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
return session.ExecuteList(query).Cast<ProductBatch>().ToList();
|
|
}
|
|
}
|
|
|
|
const string baseInfoRpcPath = @"/MainSystem/B3ClientService/Rpcs/SyncBaseInfoRpc/";
|
|
public static void LoadProductBatchFromServer()
|
|
{
|
|
using (var session = LocalDmoSession.New())
|
|
{
|
|
var local = GetLocalVersion(session);
|
|
var json = RpcFacade.Call<string>(baseInfoRpcPath + "SyncProductBatchByType", 0, JsonConvert.SerializeObject(local));
|
|
|
|
var result = JsonConvert.DeserializeObject<Tuple<List<ProductBatch>, List<ProductBatch>, List<long>>>(json);//insert,update,delete
|
|
foreach (var item in result.Item1)
|
|
session.Insert(item);
|
|
foreach (var item in result.Item2)
|
|
session.Update(item);
|
|
if (result.Item3.Any())
|
|
Delete(session, result.Item3);
|
|
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static List<Tuple<long, int>> GetLocalVersion(IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(ProductBatch)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("RowVersion"));
|
|
//query.Where.Conditions.Add(DQCondition.GreaterThanOrEqual("Date", DateTime.Today.AddDays(-1)));
|
|
var list = new List<Tuple<long, int>>();
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
list.Add(new Tuple<long, int>((long)reader[0], (int)reader[1]));
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
static void Delete(IDmoSession session, List<long> ids)
|
|
{
|
|
var delete = new DQDeleteDom(typeof(ProductBatch));
|
|
delete.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray()));
|
|
session.ExecuteNonQuery(delete);
|
|
}
|
|
}
|
|
}
|