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;
|
|
|
|
namespace ButcherFactory.BO.LocalBL
|
|
{
|
|
public static class CarcassTakeOutBL
|
|
{
|
|
|
|
const string RpcPath = @"/MainSystem/B3ClientService/Rpcs/CarcassTakeOutRpc/";
|
|
|
|
public static CarcassTakeOut InsertOrUpdate(long? workUnitID, string barCode, out bool isNew)
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
var entity = GetEntityByBarCode(barCode, session);
|
|
if (entity == null)
|
|
{
|
|
entity = new CarcassTakeOut();
|
|
entity.WorkUnit_ID = workUnitID;
|
|
entity.BarCode = barCode;
|
|
entity.UserID = AppContext.Worker.ID;
|
|
entity.RowIndex = GetRowIndex(session);
|
|
session.Insert(entity);
|
|
isNew = true;
|
|
}
|
|
else
|
|
{
|
|
if (entity.WorkUnit_ID != workUnitID)
|
|
Update(entity.ID, session, new Tuple<string, object>("WorkUnit_ID", workUnitID));
|
|
isNew = false;
|
|
}
|
|
session.Commit();
|
|
return entity;
|
|
}
|
|
}
|
|
|
|
static int GetRowIndex(IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias("_main", typeof(CarcassTakeOut)));
|
|
query.Columns.Add(DQSelectColumn.Max("RowIndex"));
|
|
query.Where.Conditions.Add(DQCondition.EQ(DQExpression.Snippet("CAST([_main].[CreateTime] AS DATE)"), DQExpression.Value(DateTime.Today)));
|
|
return query.EExecuteScalar<int?>(session) ?? 0;
|
|
}
|
|
|
|
private static CarcassTakeOut GetEntityByBarCode(string barCode, IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(CarcassTakeOut)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("WorkUnit_ID"));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("BarCode", barCode), DQCondition.EQ("Delete", false)));
|
|
var item = query.EExecuteScalar<long, long?>();
|
|
if (item == null)
|
|
return null;
|
|
return new CarcassTakeOut() { ID = item.Item1, WorkUnit_ID = item.Item2 };
|
|
}
|
|
|
|
static void Update(long id, IDmoSession session, params Tuple<string, object>[] updates)
|
|
{
|
|
var update = new DQUpdateDom(typeof(CarcassTakeOut));
|
|
update.Where.Conditions.Add(DQCondition.EQ("ID", id));
|
|
foreach (var item in updates)
|
|
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 void Delete(long id)
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
Update(id, session, new Tuple<string, object>("Delete", true));
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static BindingList<CarcassTakeOut> GetLocalDataWithState(bool history)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(CarcassTakeOut)));
|
|
query.Columns.Add(DQSelectColumn.Field("ID"));
|
|
query.Columns.Add(DQSelectColumn.Field("BarCode"));
|
|
query.Columns.Add(DQSelectColumn.Field("Goods_Name"));
|
|
query.Columns.Add(DQSelectColumn.Field("BeforeWeight"));
|
|
query.Columns.Add(DQSelectColumn.Field("Weight"));
|
|
if (history)
|
|
query.Range = SelectRange.Top(30);
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Submited", history), DQCondition.EQ("Delete", false)));
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true));
|
|
var result = new BindingList<CarcassTakeOut>();
|
|
using (var session = DmoSession.New())
|
|
{
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var entity = new CarcassTakeOut();
|
|
result.Add(entity);
|
|
entity.ID = (long)reader[0];
|
|
entity.BarCode = (string)reader[1];
|
|
entity.Goods_Name = (string)reader[2];
|
|
entity.BeforeWeight = (decimal?)reader[3];
|
|
entity.Weight = (decimal?)reader[4];
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static void SetWeight(IEnumerable<long> ids, decimal weight)
|
|
{
|
|
var id = ids.Max();
|
|
using (var session = DmoSession.New())
|
|
{
|
|
Update(id, session, new Tuple<string, object>("GroupID", id), new Tuple<string, object>("Weight", weight));
|
|
GroupUpdate(ids.Where(x => x != id), session, new Tuple<string, object>("GroupID", id), new Tuple<string, object>("Weight", 0));
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static void GroupUpdate(IEnumerable<long> ids, IDmoSession session, params Tuple<string, object>[] updates)
|
|
{
|
|
if (ids.Count() == 0)
|
|
return;
|
|
var update = new DQUpdateDom(typeof(CarcassTakeOut));
|
|
update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray()));
|
|
foreach (var item in updates)
|
|
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 void Submit(IEnumerable<long> ids)
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
GroupUpdate(ids, session, new Tuple<string, object>("Submited", true));
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static List<ExtensionObj> GetBeforeInfo(IEnumerable<string> codeList)
|
|
{
|
|
try
|
|
{
|
|
var json = RpcFacade.Call<string>(RpcPath + "GetBeforeInfo", codeList);
|
|
var list = JsonConvert.DeserializeObject<List<ExtensionObj>>(json);
|
|
if (list.Any())
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
foreach (var item in list)
|
|
SaveBeforeInfoInDB(item, session);
|
|
session.Commit();
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
catch
|
|
{
|
|
#if DEBUG
|
|
throw;
|
|
#endif
|
|
return new List<ExtensionObj>(); }
|
|
}
|
|
|
|
private static void SaveBeforeInfoInDB(ExtensionObj obj, IDmoSession session)
|
|
{
|
|
var update = new DQUpdateDom(typeof(CarcassTakeOut));
|
|
update.Where.Conditions.Add(DQCondition.EQ("BarCode", obj.StringExt1));
|
|
update.Columns.Add(new DQUpdateColumn("Goods_ID", obj.LongExt1));
|
|
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<CarcassTakeOutObj> GetUnSyncData(IDmoSession session)
|
|
{
|
|
var query = new DQueryDom(new JoinAlias(typeof(CarcassTakeOut)));
|
|
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("Weight"));
|
|
query.Columns.Add(DQSelectColumn.Field("CreateTime"));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Submited", true), DQCondition.EQ("Sync", false)));
|
|
query.Range = SelectRange.Top(10);
|
|
query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID"));
|
|
|
|
var upload = new List<CarcassTakeOutObj>();
|
|
using (var reader = session.ExecuteReader(query))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
var obj = new CarcassTakeOutObj();
|
|
obj.ID = (long)reader[0];
|
|
obj.RowVersion = (int)reader[1];
|
|
obj.BarCode = (string)reader[2];
|
|
obj.TakeOutWorker_ID = (long)reader[3];
|
|
obj.WorkUnit_ID = (long?)reader[4];
|
|
obj.Weight = (decimal)reader[5];
|
|
obj.Time = (DateTime)reader[6];
|
|
upload.Add(obj);
|
|
}
|
|
}
|
|
return upload;
|
|
}
|
|
|
|
static void SetLocalAsSyncd(CarcassTakeOutObj obj, IDmoSession session)
|
|
{
|
|
var update = new DQUpdateDom(typeof(CarcassTakeOut));
|
|
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 CarcassTakeOutObj
|
|
{
|
|
public long ID { get; set; }
|
|
public int RowVersion { get; set; }
|
|
public string BarCode { get; set; }
|
|
public long? TakeOutWorker_ID { get; set; }
|
|
public long? WorkUnit_ID { get; set; }
|
|
public decimal? Weight { get; set; }
|
|
public DateTime? Time { get; set; }
|
|
}
|
|
}
|