using BWP.B3ClientService.BO;
|
|
using BWP.B3ClientService.RpcBO;
|
|
using BWP.B3Frameworks.Utils;
|
|
using Forks.EnterpriseServices.BusinessInterfaces;
|
|
using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Script.Serialization;
|
|
using TSingSoft.WebPluginFramework;
|
|
using TSingSoft.WebPluginFramework.BWPClients;
|
|
|
|
|
|
namespace BWP.B3ClientService.Tasks.UpdateLoad
|
|
{
|
|
public static class UploadOrderDetail
|
|
{
|
|
public static void Execute(string uri)
|
|
{
|
|
var serializer = new JavaScriptSerializer();
|
|
|
|
#region 删除
|
|
DeleteUnSyncDeleteData();
|
|
#endregion
|
|
//获取所有未上传的数据
|
|
var allBill = GetAllNeedSyncBill();
|
|
foreach (var group in allBill.GroupBy(x => new { x.Date, x.AccountingUnit_ID }).OrderBy(x => x.Key.Date))
|
|
{
|
|
var creator = group.First().Creator;
|
|
|
|
var bwpClient = new BWPClient(uri, creator);
|
|
using (var context = new TransactionContext())
|
|
{
|
|
var entity = new RpcOrderBill();
|
|
entity.AccountingUnit_ID = group.Key.AccountingUnit_ID;
|
|
entity.Date = group.Key.Date;
|
|
var details = new List<SOrderDetail>();
|
|
var delete = new List<long>();
|
|
foreach (var item in group)
|
|
{
|
|
if (item.DeleteState)
|
|
delete.Add(item.B3ID.Value);
|
|
else
|
|
{
|
|
var detail = new SOrderDetail();
|
|
DmoUtil.CopyDmoFields(item, detail);
|
|
details.Add(detail);
|
|
}
|
|
}
|
|
entity.Details = details.ToArray();
|
|
entity.DeleteIDs = delete.ToArray();
|
|
|
|
var sync = serializer.Serialize(entity);
|
|
var stringBack = bwpClient.Call<string>("/MainSystem/B3ButcherManage/Rpcs/ButcherOrderRpc/UpdateOrInsert", sync);
|
|
|
|
var back = serializer.Deserialize<List<CTuple<long, long, long>>>(stringBack);
|
|
#region 同步完了要清理掉删除的记录
|
|
if (delete.Any())
|
|
ClearDetails(delete, context.Session);
|
|
|
|
#endregion
|
|
|
|
#region 反填信息
|
|
foreach (var item in back)
|
|
{
|
|
Update(item, context.Session);
|
|
}
|
|
SetAllItemAsSync(context.Session, group.Select(x => DQExpression.Value(x.ID)));
|
|
#endregion
|
|
|
|
context.Commit();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void DeleteUnSyncDeleteData()
|
|
{
|
|
using (var session = Dmo.NewSession())
|
|
{
|
|
var delete = new DQDeleteDom(typeof(OrderDetail));
|
|
delete.Where.Conditions.Add(DQCondition.And(DQCondition.IsNull(DQExpression.Field("B3ID")), DQCondition.EQ("DeleteState", true)));
|
|
session.ExecuteNonQuery(delete);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<OrderDetail> GetAllNeedSyncBill()
|
|
{
|
|
var query = new DmoQuery(typeof(OrderDetail));
|
|
query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("B3WeighBill_ID")), DQCondition.EQ("Sync", false)));
|
|
return query.EExecuteList().Cast<OrderDetail>();
|
|
}
|
|
|
|
private static void ClearDetails(List<long> b3IDs, IDmoSession session)
|
|
{
|
|
var delete = new DQDeleteDom(typeof(OrderDetail));
|
|
delete.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("B3ID"), b3IDs.Select(x => DQExpression.Value(x)).ToArray()));
|
|
}
|
|
|
|
static void Update(CTuple<long, long, long> item, IDmoSession session)
|
|
{
|
|
var update = new DQUpdateDom(typeof(OrderDetail));
|
|
update.Columns.Add(new DQUpdateColumn("B3ID", item.Item2));
|
|
update.Columns.Add(new DQUpdateColumn("B3MainID", item.Item3));
|
|
update.Where.Conditions.Add(DQCondition.EQ("ID", item.Item1));
|
|
session.ExecuteNonQuery(update);
|
|
}
|
|
|
|
static void SetAllItemAsSync(IDmoSession session, IEnumerable<IDQExpression> ids)
|
|
{
|
|
var update = new DQUpdateDom(typeof(OrderDetail));
|
|
update.Columns.Add(new DQUpdateColumn("Sync", true));
|
|
update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.ToArray()));
|
|
session.ExecuteNonQuery(update);
|
|
}
|
|
}
|
|
}
|