屠宰场客户端
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.

80 lines
2.6 KiB

using Forks.EnterpriseServices.DomainObjects2;
using Forks.EnterpriseServices.DomainObjects2.DQuery;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ButcherManage.BO.LocalBL
{
public static class BaseInfoSyncRpc
{
const string baseInfoRpcPath = @"/MainSystem/B3ClientService/Rpcs/SyncBaseInfoRpc/";
static void TruncateTable(Type type, IDmoSession session)
{
var table = DmoInfo.Get(type).MappedDBObject;
var sql = string.Format("truncate table [{0}]", table);
session.ExecuteSqlNonQuery(sql);
}
public static void SyncBaseInfo<T>(string methodName = null)
where T : BaseInfo
{
var type = typeof(T);
if (string.IsNullOrEmpty(methodName))
methodName = "Sync" + type.Name;
var json = SimpleRest.Call<string>(baseInfoRpcPath + methodName);
var list = JsonConvert.DeserializeObject<List<T>>(json);
using (var session = DmoSession.New())
{
TruncateTable(type, session);
foreach (var item in list)
session.Insert(item);
session.Commit();
}
}
public static void SyncBodyDiscont()
{
var json = SimpleRest.Call<string>(baseInfoRpcPath + "SyncBodyDiscont");
var list = JsonConvert.DeserializeObject<List<BodyDiscont>>(json);
using (var session = DmoSession.New())
{
DeleteLocal(session, list.Select(x => x.ID));
var exist = GetLocal(session);
foreach (var item in list)
{
if (exist.Any(x => x == item.ID))
UpdateName(session, item);
else
session.Insert(item);
}
session.Commit();
}
}
static void DeleteLocal(IDmoSession session, IEnumerable<long> ids)
{
var del = new DQDeleteDom(typeof(BodyDiscont));
del.Where.Conditions.Add(DQCondition.NotInList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray()));
session.ExecuteNonQuery(del);
}
static List<long> GetLocal(IDmoSession session)
{
var query = new DQueryDom(new JoinAlias(typeof(BodyDiscont)));
query.Columns.Add(DQSelectColumn.Field("ID"));
return query.EExecuteList<long>(session);
}
static void UpdateName(IDmoSession session, BodyDiscont entity)
{
var update = new DQUpdateDom(typeof(BodyDiscont));
update.Columns.Add(new DQUpdateColumn("Name", entity.Name));
update.Where.Conditions.Add(DQCondition.EQ("ID", entity.ID));
session.ExecuteNonQuery(update);
}
}
}