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;
|
|
|
|
namespace ButcherFactory.BO.LocalBL
|
|
{
|
|
public static class SegmentInStoreBL
|
|
{
|
|
const string RpcPath = @"/MainSystem/B3ClientService/Rpcs/SegmentInStoreRpc/";
|
|
|
|
public static BindingList<SegmentInStore> GetInStoreByStateList(int state)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias("_main", typeof(SegmentInStore)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("RowIndex"));
|
|
query.Columns.Add(DQSelectColumn.Field("BarCode"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Code"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Name"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Spec"));
|
|
query.Columns.Add(DQSelectColumn.Field("Weight"));
|
|
if (state == 0)
|
|
{
|
|
query.Columns.Add(DQSelectColumn.Field("InStoreTime"));
|
|
query.Range = SelectRange.Top(30);
|
|
}
|
|
else
|
|
query.Columns.Add(DQSelectColumn.Field("ProductTime"));
|
|
query.Where.Conditions.Add(DQCondition.EQ("State", state));
|
|
if (state == 0)
|
|
query.Where.Conditions.Add(DQCondition.EQ(DQExpression.Snippet("CAST([_main].[InStoreTime] AS DATE)"), DQExpression.Value(DateTime.Today)));
|
|
query.Where.Conditions.Add(DQCondition.EQ("Delete", false));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("RowIndex", true));
|
|
var list = new List<SegmentInStore>();
|
|
using (var session = DmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var entity = new SegmentInStore();
|
|
entity.ID = (long)reader[0];
|
|
entity.RowIndex = (int?)reader[1];
|
|
entity.BarCode = (string)reader[2];
|
|
entity.Goods_Code = (string)reader[3];
|
|
entity.Goods_Name = (string)reader[4];
|
|
entity.Goods_Spec = (string)reader[5];
|
|
entity.Weight = (decimal?)reader[6];
|
|
if (state == 0)
|
|
entity.InStoreTime = (DateTime)reader[7];
|
|
else
|
|
entity.ProductTime = (DateTime?)reader[7];
|
|
list.Add(entity);
|
|
}
|
|
}
|
|
}
|
|
return new BindingList<SegmentInStore>(list);
|
|
}
|
|
|
|
public static BindingList<SegmentCodeError> GetExceptionList()
|
|
{
|
|
var query = new DmoQuery(typeof(SegmentCodeError));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID"));
|
|
using (var session = DmoSession.New())
|
|
{
|
|
return new BindingList<SegmentCodeError>(session.ExecuteList(query).Cast<SegmentCodeError>().ToList());
|
|
}
|
|
}
|
|
|
|
public static BindingList<SegmentInStore> GetUnInStoreList()
|
|
{
|
|
try
|
|
{
|
|
var json = RpcFacade.Call<string>(RpcPath + "GetUnInStoreList");
|
|
var list = JsonConvert.DeserializeObject<List<SegmentInStore>>(json);
|
|
var inStored = GetInstoredList(list.Select(x => x.BarCode));
|
|
var result = list.Where(x => !inStored.Any(y => x.BarCode == y)).ToList();
|
|
var idx = 1;
|
|
foreach (var item in result)
|
|
{
|
|
item.RowIndex = idx;
|
|
idx++;
|
|
}
|
|
return new BindingList<SegmentInStore>(result);
|
|
}
|
|
catch
|
|
{
|
|
#if DEBUG
|
|
throw;
|
|
#endif
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static List<string> GetInstoredList(IEnumerable<string> barCodes)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(SegmentInStore)));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.InList(DQExpression.Field("BarCode"), barCodes.Select(x => DQExpression.Value(x)).ToArray()), DQCondition.EQ("Delete", false)));
|
|
using (var session = DmoSession.New())
|
|
{
|
|
return query.EExecuteList<string>(session);
|
|
}
|
|
}
|
|
|
|
public static SegmentInStore InsertInStore(string barCode, long? storeID, int rowIndex)
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
var entity = new SegmentInStore();
|
|
entity.BarCode = barCode;
|
|
entity.Store_ID = storeID;
|
|
entity.InStoreTime = DateTime.Now;
|
|
entity.RowIndex = rowIndex;
|
|
|
|
var localData = GetLocalData(session, barCode);
|
|
if (localData == null) //if not exist LoadFromServer
|
|
{
|
|
try
|
|
{
|
|
var json = RpcFacade.Call<string>(RpcPath + "GetSegmentProductInfo", barCode);
|
|
var obj = JsonConvert.DeserializeObject<SegmentProductObj>(json);
|
|
entity.Goods_Code = obj.Goods_Code;
|
|
entity.Goods_Name = obj.Goods_Name;
|
|
entity.Goods_Spec = obj.Goods_Spec;
|
|
entity.ProductTime = obj.ProductTime;
|
|
entity.Weight = obj.Weight;
|
|
}
|
|
catch
|
|
{
|
|
#if DEBUG
|
|
throw;
|
|
#endif
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (localData.Item3) //if delete LoadFromLocal
|
|
{
|
|
var obj = LoadLocalData(session, localData.Item1);
|
|
entity.Goods_Code = obj.Goods_Code;
|
|
entity.Goods_Name = obj.Goods_Name;
|
|
entity.Goods_Spec = obj.Goods_Spec;
|
|
entity.ProductTime = obj.ProductTime;
|
|
entity.Weight = obj.Weight;
|
|
}
|
|
else
|
|
{
|
|
//if inStored Error 已入库
|
|
//if isBacking Error 正在退库
|
|
//if back Error 已退库
|
|
var msg = "已入库";
|
|
switch (localData.Item2)
|
|
{
|
|
case 1:
|
|
msg = "正在退库";
|
|
break;
|
|
case 2:
|
|
msg = "已退库";
|
|
break;
|
|
}
|
|
throw new Exception(string.Format("入库失败!当前条码{0}", msg));
|
|
}
|
|
}
|
|
|
|
session.Insert(entity);
|
|
session.Commit();
|
|
return entity;
|
|
}
|
|
}
|
|
|
|
static Tuple<long, int, bool> GetLocalData(IDmoSession session, string barCode)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(SegmentInStore)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("State"));
|
|
query.Columns.Add(DQSelectColumn.Field("Delete"));
|
|
query.Where.Conditions.Add(DQCondition.EQ("BarCode", barCode));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true));
|
|
query.Range = SelectRange.Top(1);
|
|
return query.EExecuteScalar<long, int, bool>(session);
|
|
}
|
|
|
|
static SegmentProductObj LoadLocalData(IDmoSession session, long id)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(SegmentInStore)));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Code"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Name"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Spec"));
|
|
query.Columns.Add(DQSelectColumn.Field("ProductTime"));
|
|
query.Columns.Add(DQSelectColumn.Field("Weight"));
|
|
query.Where.Conditions.Add(DQCondition.EQ("ID", id));
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
var obj = new SegmentProductObj();
|
|
if (reader.Read())
|
|
{
|
|
obj.Goods_Code = (string)reader[0];
|
|
obj.Goods_Name = (string)reader[1];
|
|
obj.Goods_Spec = (string)reader[2];
|
|
obj.ProductTime = (DateTime?)reader[3];
|
|
obj.Weight = (decimal?)reader[4];
|
|
}
|
|
return obj;
|
|
}
|
|
}
|
|
|
|
public static void SetAsBacking(string barCode, int rowIndex)
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
var localData = GetLocalData(session, barCode);
|
|
if (localData == null)
|
|
throw new Exception("未入库,无法退库");
|
|
if (localData.Item3)
|
|
throw new Exception("已删除,无法退库");
|
|
switch (localData.Item2)
|
|
{
|
|
case 1:
|
|
throw new Exception("已在退库中,等待提交");
|
|
case 2:
|
|
throw new Exception("已退库,无法重复退库");
|
|
default:
|
|
UpdateAsBacking(session, localData.Item1, rowIndex);
|
|
session.Commit();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void UpdateAsBacking(IDmoSession session, long id, int rowIndex)
|
|
{
|
|
var update = new DQUpdateDom(typeof(SegmentInStore));
|
|
update.Columns.Add(new DQUpdateColumn("State", 1));
|
|
update.Columns.Add(new DQUpdateColumn("RowIndex", rowIndex));
|
|
update.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1))));
|
|
update.Where.Conditions.Add(DQCondition.EQ("ID", id));
|
|
session.ExecuteNonQuery(update);
|
|
}
|
|
|
|
public static void Delete(long id)
|
|
{
|
|
var update = new DQUpdateDom(typeof(SegmentInStore));
|
|
update.Columns.Add(new DQUpdateColumn("State", 0));
|
|
update.Columns.Add(new DQUpdateColumn("Delete", 1));
|
|
update.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1))));
|
|
update.Where.Conditions.Add(DQCondition.EQ("ID", id));
|
|
using (var session = DmoSession.New())
|
|
{
|
|
session.ExecuteNonQuery(update);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static void SubmitBackStore(IEnumerable<long> ids)
|
|
{
|
|
var update = new DQUpdateDom(typeof(SegmentInStore));
|
|
update.Columns.Add(new DQUpdateColumn("State", 2));
|
|
update.Columns.Add(new DQUpdateColumn("Sync", false));
|
|
update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1))));
|
|
update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray()));
|
|
using (var session = DmoSession.New())
|
|
{
|
|
session.ExecuteNonQuery(update);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static void InsertScanException(SegmentCodeError error)
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
session.Insert(error);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static BindingList<SegmentInStore> GetInStoreSummary()
|
|
{
|
|
var query = new DQueryDom(new JoinAlias("_main", typeof(SegmentInStore)));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Code"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Name"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Spec"));
|
|
query.Columns.Add(DQSelectColumn.Count());
|
|
query.Columns.Add(DQSelectColumn.Sum("Weight"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Goods_Code"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Goods_Name"));
|
|
query.GroupBy.Expressions.Add(DQExpression.Field("Goods_Spec"));
|
|
|
|
query.Where.Conditions.Add(DQCondition.EQ(DQExpression.Snippet("CAST([_main].[InStoreTime] AS DATE)"), DQExpression.Value(DateTime.Today)));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.InEQ("State", 2), DQCondition.EQ("Delete", false)));
|
|
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("Goods_Code"));
|
|
var list = new BindingList<SegmentInStore>();
|
|
using (var session = DmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
var idx = 1;
|
|
while (reader.Read())
|
|
{
|
|
var entity = new SegmentInStore();
|
|
entity.RowIndex = idx;
|
|
entity.Goods_Code = (string)reader[0];
|
|
entity.Goods_Name = (string)reader[1];
|
|
entity.Goods_Spec = (string)reader[2];
|
|
entity.Number = Convert.ToInt32(reader[3]);
|
|
entity.Weight = (decimal?)reader[4];
|
|
list.Add(entity);
|
|
idx++;
|
|
}
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static void UploadSegmentInstoreInfo()
|
|
{
|
|
try
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
var needUpload = GetUnSyncData(session);
|
|
if (needUpload.Count == 0)
|
|
return;
|
|
|
|
var json = JsonConvert.SerializeObject(needUpload);
|
|
RpcFacade.Call<int>(RpcPath + "UploadSegmentInStoreInfo", json);
|
|
foreach (var item in needUpload)
|
|
SetLocalAsSyncd(item, session);
|
|
session.Commit();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
#if DEBUG
|
|
throw;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
static List<SegmentInStoreObj> GetUnSyncData(IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(SegmentInStore)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("RowVersion"));
|
|
query.Columns.Add(DQSelectColumn.Field("BarCode"));
|
|
query.Columns.Add(DQSelectColumn.Field("InStoreTime"));
|
|
query.Columns.Add(DQSelectColumn.Field("Store_ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("State"));
|
|
query.Columns.Add(DQSelectColumn.Field("Delete"));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.InEQ("State", 1), DQCondition.EQ("Sync", false)));
|
|
query.Range = SelectRange.Top(10);
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID"));
|
|
|
|
var upload = new List<SegmentInStoreObj>();
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var obj = new SegmentInStoreObj();
|
|
obj.ID = (long)reader[0];
|
|
obj.RowVersion = (int)reader[1];
|
|
obj.BarCode = (string)reader[2];
|
|
obj.InStoreTime = (DateTime)reader[3];
|
|
obj.Store_ID = (long?)reader[4];
|
|
obj.State = (int)reader[5];
|
|
obj.Delete = (bool)reader[6];
|
|
upload.Add(obj);
|
|
}
|
|
}
|
|
return upload;
|
|
}
|
|
|
|
static void SetLocalAsSyncd(SegmentInStoreObj obj, IDmoSession session)
|
|
{
|
|
var update = new DQUpdateDom(typeof(SegmentInStore));
|
|
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 SegmentInStoreObj
|
|
{
|
|
[JsonIgnore]
|
|
public long ID { get; set; }
|
|
|
|
[JsonIgnore]
|
|
public long RowVersion { get; set; }
|
|
|
|
public string BarCode { get; set; }
|
|
|
|
public DateTime InStoreTime { get; set; }
|
|
|
|
public long? Store_ID { get; set; }
|
|
|
|
public int State { get; set; }
|
|
|
|
public bool Delete { get; set; }
|
|
}
|
|
|
|
class SegmentProductObj
|
|
{
|
|
public string BarCode { get; set; }
|
|
public string Goods_Name { get; set; }
|
|
public string Goods_Code { get; set; }
|
|
public string Goods_Spec { get; set; }
|
|
public decimal? Weight { get; set; }
|
|
public DateTime? ProductTime { get; set; }
|
|
}
|
|
}
|