You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

308 lines
11 KiB

using ButcherFactory.BO.Utils;
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.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSingSoft.WebPluginFramework;
namespace ButcherFactory.BO.LocalBL
{
public static class CarcassInStoreBL
{
const string RpcPath = @"/MainSystem/B3ClientService/Rpcs/CarcassInStoreRpc/";
static Dictionary<long, string> _goodsNames = new Dictionary<long, string>();
public static void DoWithPadData(IEnumerable<CarcassInStore> list)
{
try
{
var json = RpcFacade.Call<string>(RpcPath + "GetUnSyncPadData");
var data = JsonConvert.DeserializeObject<List<PadCarcassInStore>>(json);
if (data.Count == 0)
return;
var back = new List<Tuple<long, int>>();
using (var session = DmoSession.New())
{
foreach (var item in list)
{
var first = data.FirstOrDefault();
if (first == null)
break;
if (CheckExist(first.BarCode, session))
continue;
back.Add(new Tuple<long, int>(first.ID, first.RowVersion));
Update(item.ID, session, new Tuple<string, object>("Goods_ID", first.Goods_ID), new Tuple<string, object>("BarCode", first.BarCode), new Tuple<string, object>("FromPad", true));
item.Goods_ID = first.Goods_ID;
item.Goods_Name = GetGoodsName(item.Goods_ID.Value, session);
item.BarCode = first.BarCode;
}
session.Commit();
}
if (back.Count == 0)
return;
var backInfo = back.Select(x => new ExtensionObj { LongExt1 = x.Item1, DecimalExt1 = x.Item2 });
RpcFacade.Call<int>(RpcPath + "SetPadDataSync", JsonConvert.SerializeObject(backInfo));
}
catch
{
#if DEBUG
throw;
#endif
}
}
public static CarcassInStore Insert(long? workUnit, long? productBatch, decimal weight)
{
using (var session = DmoSession.New())
{
var entity = new CarcassInStore();
entity.WorkUnit_ID = workUnit;
if (productBatch.HasValue)
{
entity.ProductBatch_ID = productBatch;
entity.RowIndex = GenerateRowIndex(session, productBatch.Value);
}
entity.UserID = AppContext.Worker.ID;
entity.Weight = weight;
session.Insert(entity);
session.Commit();
return entity;
}
}
static bool CheckExist(string barCode, IDmoSession session)
{
if (string.IsNullOrEmpty(barCode))
return false;
var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore)));
query.Columns.Add(DQSelectColumn.Create(DQExpression.Value(1), "c"));
query.Where.Conditions.Add(DQCondition.EQ("BarCode", barCode));
return query.EExecuteScalar(session) != null;
}
public static void Update(long id, long goodsID, string barCode)
{
using (var session = DmoSession.New())
{
var exist = CheckExist(barCode, session);
if (exist)
throw new Exception("条码已使用过");
Update(id, session, new Tuple<string, object>("Goods_ID", goodsID), new Tuple<string, object>("BarCode", barCode));
session.Commit();
}
}
static int GenerateRowIndex(IDmoSession session, long batchID)
{
var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore)));
query.Columns.Add(DQSelectColumn.Max("RowIndex"));
query.Where.Conditions.Add(DQCondition.EQ("ProductBatch_ID", batchID));
return (query.EExecuteScalar<int?>(session) ?? 0) + 1;
}
static string GetGoodsName(long id, IDmoSession session)
{
if (!_goodsNames.ContainsKey(id))
{
var query = new DQueryDom(new JoinAlias(typeof(Goods)));
query.Columns.Add(DQSelectColumn.Field("Name"));
query.Where.Conditions.Add(DQCondition.EQ("ID", id));
_goodsNames.Add(id, query.EExecuteScalar<string>(session));
}
return _goodsNames[id];
}
static void Update(long id, IDmoSession session, params Tuple<string, object>[] values)
{
var update = new DQUpdateDom(typeof(CarcassInStore));
update.Where.Conditions.Add(DQCondition.EQ("ID", id));
foreach (var item in values)
update.Columns.Add(new DQUpdateColumn(item.Item1, item.Item2));
update.Columns.Add(new DQUpdateColumn("Sync", false));
update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1))));
session.ExecuteNonQuery(update);
}
public static BindingList<CarcassInStore> GetLocalDataWithState(bool history)
{
var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore)));
query.Columns.Add(DQSelectColumn.Field("RowIndex"));
query.Columns.Add(DQSelectColumn.Field("ID"));
query.Columns.Add(DQSelectColumn.Field("BarCode"));
query.Columns.Add(DQSelectColumn.Field("Goods_Name"));
if (history)
{
query.Columns.Add(DQSelectColumn.Field("Weight"));
query.Columns.Add(DQSelectColumn.Field("BeforeWeight"));
query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("Goods_ID")), DQCondition.IsNotNull(DQExpression.Field("Weight"))));
query.Range = SelectRange.Top(30);
}
else
query.Where.Conditions.Add(DQCondition.Or(DQCondition.IsNull(DQExpression.Field("Goods_ID")), DQCondition.IsNull(DQExpression.Field("Weight"))));
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true));
var result = new BindingList<CarcassInStore>();
using (var session = DmoSession.New())
{
using (var reader = session.ExecuteReader(query))
{
while (reader.Read())
{
var entity = new CarcassInStore();
result.Add(entity);
entity.RowIndex = (int?)reader[0];
entity.ID = (long)reader[1];
entity.BarCode = (string)reader[2];
entity.Goods_Name = (string)reader[3];
if (history)
{
entity.Weight = (decimal)reader[4];
entity.BeforeWeight = (decimal?)reader[5];
}
}
}
}
return result;
}
public static IEnumerable<ClientGoodsSet_Detail> GetGoodsList()
{
var main = new JoinAlias(typeof(ClientGoodsSet));
var detail = new JoinAlias(typeof(ClientGoodsSet_Detail));
var query = new DQueryDom(main);
query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID"));
query.Columns.Add(DQSelectColumn.Field("Goods_ID", detail));
query.Columns.Add(DQSelectColumn.Field("Goods_Name", detail));
query.OrderBy.Expressions.Add(DQOrderByExpression.Create(detail, "Goods_ID"));
var list = query.EExecuteList<long, string>();
return list.Select(x => new ClientGoodsSet_Detail { Goods_ID = x.Item1, Goods_Name = x.Item2 });
}
public static List<ExtensionObj> GetBeforeWeight(IEnumerable<string> codeList)
{
try
{
var json = RpcFacade.Call<string>(RpcPath + "GetWeightInfo", codeList);
var list = JsonConvert.DeserializeObject<List<ExtensionObj>>(json);
if (list.Any())
{
using (var session = DmoSession.New())
{
foreach (var item in list)
SaveWeightInDB(item, session);
session.Commit();
}
}
return list;
}
catch
{
#if DEBUG
throw;
#endif
return new List<ExtensionObj>();
}
}
static void SaveWeightInDB(ExtensionObj obj, IDmoSession session)
{
var update = new DQUpdateDom(typeof(CarcassInStore));
update.Where.Conditions.Add(DQCondition.EQ("BarCode", obj.StringExt1));
update.Columns.Add(new DQUpdateColumn("BeforeWeight", obj.DecimalExt1));
session.ExecuteNonQuery(update);
}
public static void UploadCarcassInfo()
{
try
{
using (var session = DmoSession.New())
{
var needUpload = GetUnSyncData(session);
if (needUpload.Count == 0)
return;
var json = JsonConvert.SerializeObject(needUpload);
RpcFacade.Call<int>(RpcPath + "UploadCarcassInfo", json);
foreach (var item in needUpload)
SetLocalAsSyncd(item, session);
session.Commit();
}
}
catch
{
#if DEBUG
throw;
#endif
}
}
static List<CarcassInStoreObj> GetUnSyncData(IDmoSession session)
{
var query = new DQueryDom(new JoinAlias("_main", typeof(CarcassInStore)));
query.Columns.Add(DQSelectColumn.Field("ID"));
query.Columns.Add(DQSelectColumn.Field("RowVersion"));
query.Columns.Add(DQSelectColumn.Field("BarCode"));
query.Columns.Add(DQSelectColumn.Field("UserID"));
query.Columns.Add(DQSelectColumn.Field("WorkUnit_ID"));
query.Columns.Add(DQSelectColumn.Field("ProductBatch_ID"));
query.Columns.Add(DQSelectColumn.Field("Goods_ID"));
query.Columns.Add(DQSelectColumn.Field("Weight"));
query.Columns.Add(DQSelectColumn.Field("CreateTime"));
query.Where.Conditions.Add(DQCondition.And(
//DQExpression.Snippet("[_main].[BarCode] != ''"),
DQCondition.IsNotNull(DQExpression.Field("Goods_ID")),
DQCondition.IsNotNull(DQExpression.Field("Weight")), DQCondition.EQ("Sync", false)));
query.Range = SelectRange.Top(10);
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID"));
var upload = new List<CarcassInStoreObj>();
using (var reader = session.ExecuteReader(query))
{
while (reader.Read())
{
var obj = new CarcassInStoreObj();
obj.ID = (long)reader[0];
obj.RowVersion = (int)reader[1];
obj.BarCode = (string)reader[2];
obj.InStoreWorker_ID = (long)reader[3];
obj.WorkUnit_ID = (long?)reader[4];
obj.ProductBatch_ID = (long?)reader[5];
obj.InStoreGoods_ID = (long)reader[6];
obj.InStoreWeight = (decimal)reader[7];
obj.InStoreTime = (DateTime)reader[8];
upload.Add(obj);
}
}
return upload;
}
static void SetLocalAsSyncd(CarcassInStoreObj obj, IDmoSession session)
{
var update = new DQUpdateDom(typeof(CarcassInStore));
update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ID", obj.ID), DQCondition.EQ("RowVersion", obj.RowVersion)));
update.Columns.Add(new DQUpdateColumn("Sync", true));
session.ExecuteNonQuery(update);
}
}
class CarcassInStoreObj
{
public long ID { get; set; }
public int RowVersion { get; set; }
public string BarCode { get; set; }
public long? InStoreWorker_ID { get; set; }
public long? WorkUnit_ID { get; set; }
public long? ProductBatch_ID { get; set; }
public long? InStoreGoods_ID { get; set; }
public decimal? InStoreWeight { get; set; }
public DateTime? InStoreTime { get; set; }
}
}