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.
 
 

277 lines
10 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 List<CarcassInStore> DoWithPadData(long? workUnitID, long? batchID)
{
var json = RpcFacade.Call<string>(RpcPath + "GetUnSyncPadData");
var data = JsonConvert.DeserializeObject<List<PadCarcassInStore>>(json);
var list = new List<CarcassInStore>();
if (data.Count == 0)
return list;
var goodsBarCode = data.Select(x => new Tuple<long, string>(x.Goods_ID, x.BarCode));
list = InsertOrUpdate(workUnitID, batchID, goodsBarCode);
var backInfo = data.Select(x => new ExtensionObj { LongExt1 = x.ID, DecimalExt1 = x.RowVersion });
RpcFacade.Call<int>(RpcPath + "SetPadDataSync", JsonConvert.SerializeObject(backInfo));
return list;
}
public static List<CarcassInStore> InsertOrUpdate(long? workUnitID, long? batchID, long goodsID, string barCode)
{
var list = new List<Tuple<long, string>> { new Tuple<long, string>(goodsID, barCode) };
return InsertOrUpdate(workUnitID, batchID, list);
}
static List<CarcassInStore> InsertOrUpdate(long? workUnitID, long? batchID, IEnumerable<Tuple<long, string>> data)
{
var list = new List<CarcassInStore>();
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
foreach (var item in data)
{
var id = GetExistWithUpdate(item, session);
if (id.HasValue)
{
var exist = new CarcassInStore();
list.Add(exist);
exist.ID = id.Value;
exist.Goods_ID = item.Item1;
exist.Goods_Name = GetGoodsName(item.Item1, session);
}
else
{
var entity = CreateCarcassInStore(workUnitID, batchID, item);
entity.Goods_Name = GetGoodsName(item.Item1, session);
session.Insert(entity);
list.Add(entity);
}
}
session.Commit();
}
return list;
}
static CarcassInStore CreateCarcassInStore(long? workUnitID, long? batchID, Tuple<long, string> goodsCode)
{
var entity = new CarcassInStore();
entity.FromPad = true;
entity.WorkUnit_ID = workUnitID;
entity.ProductBatch_ID = batchID;
entity.UserID = AppContext.Worker.ID;
entity.BarCode = goodsCode.Item2;
entity.Goods_ID = goodsCode.Item1;
return entity;
}
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 long? GetExistWithUpdate(Tuple<long, string> goodsCode, IDmoSession session)
{
var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore)));
query.Columns.Add(DQSelectColumn.Field("ID"));
query.Where.Conditions.Add(DQCondition.EQ("BarCode", goodsCode.Item2));
var id = (long?)session.ExecuteScalar(query);
if (id == null)
return null;
Update(id.Value, "Goods_ID", goodsCode.Item1, session);
return id.Value;
}
static void Update(long id, string fileName, object value, IDmoSession session)
{
var update = new DQUpdateDom(typeof(CarcassInStore));
update.Where.Conditions.Add(DQCondition.EQ("ID", id));
update.Columns.Add(new DQUpdateColumn(fileName, value));
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 void Update(long id, string fileName, object value)
{
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
Update(id, fileName, value, session);
session.Commit();
}
}
public static BindingList<CarcassInStore> GetLocalDataWithState(bool history)
{
var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore)));
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.IsNotNull(DQExpression.Field("Weight")));
query.Range = SelectRange.Top(30);
}
else
query.Where.Conditions.Add(DQCondition.IsNull(DQExpression.Field("Weight")));
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true));
var result = new BindingList<CarcassInStore>();
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
using (var reader = session.ExecuteReader(query))
{
while (reader.Read())
{
var entity = new CarcassInStore();
result.Add(entity);
entity.ID = (long)reader[0];
entity.BarCode = (string)reader[1];
entity.Goods_Name = (string)reader[2];
if (history)
{
entity.Weight = (decimal)reader[3];
entity.BeforeWeight = (decimal?)reader[4];
}
}
}
}
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)
{
var json = RpcFacade.Call<string>(RpcPath + "GetWeightInfo", codeList);
var list = JsonConvert.DeserializeObject<List<ExtensionObj>>(json);
if (list.Any())
{
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
foreach (var item in list)
SaveWeightInDB(item, session);
session.Commit();
}
}
return list;
}
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("Weight", obj.DecimalExt1));
session.ExecuteNonQuery(update);
}
public static void UploadCarcassInfo()
{
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
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();
}
}
static List<CarcassInStoreObj> GetUnSyncData(IDmoSession session)
{
var query = new DQueryDom(new JoinAlias(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(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));
update.EExecute();
}
}
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; }
}
}