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.
 
 

173 lines
6.3 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? productBatch)
{
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;
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
foreach (var item in data)
{
var id = UpdateIfExist(item, session);
if (id.HasValue)
{
var exist = new CarcassInStore();
list.Add(exist);
exist.ID = id.Value;
exist.Goods_ID = item.Goods_ID;
exist.Goods_Name = GetGoodsName(item.Goods_ID, session);
}
else
{
var entity = CreateCarcassInStore(item);
entity.WorkUnit_ID = workUnitID;
entity.ProductBatch_ID = productBatch;
entity.Goods_Name = GetGoodsName(item.Goods_ID, session);
session.Insert(entity);
list.Add(entity);
}
}
session.Commit();
}
var backInfo = data.Select(x => new IDRowVersion { ID = x.ID, RowVersion = x.RowVersion });
RpcFacade.Call<int>(RpcPath + "SetPadDataSync", JsonConvert.SerializeObject(backInfo));
return list;
}
private static CarcassInStore CreateCarcassInStore(PadCarcassInStore item)
{
var entity = new CarcassInStore();
entity.FromPad = true;
entity.UserID = AppContext.Worker.ID;
entity.BarCode = item.BarCode;
entity.Goods_ID = item.Goods_ID;
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? UpdateIfExist(PadCarcassInStore data, IDmoSession session)
{
var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore)));
query.Columns.Add(DQSelectColumn.Field("ID"));
query.Where.Conditions.Add(DQCondition.EQ("BarCode", data.BarCode));
var id = (long?)session.ExecuteScalar(query);
if (id == null)
return null;
Update(id.Value, "Goods_ID", data.Goods_ID, 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 long Insert(CarcassInStore entity)
{
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
{
session.Insert(entity);
session.Commit();
return entity.ID;
}
}
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 });
}
}
}