五丰称屠宰重客户端
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.
 

153 lines
5.9 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using BO;
using System.Linq;
using Forks.EnterpriseServices.BusinessInterfaces;
using Forks.EnterpriseServices.DomainObjects2;
using Forks.EnterpriseServices.DomainObjects2.DQuery;
using Forks.EnterpriseServices.SqlDoms;
namespace B3ButcherWeightClient {
public class SyncUtil {
static readonly List<long> IdList = new List<long>();
static readonly Dictionary<long, Tuple<DateTime, long>> SequenceList = new Dictionary<long, Tuple<DateTime, long>>();
public static bool TryUpdateServerData(out string msg) {
var success = true;
msg = "同步数据......";
var step = "";
try {
IList dmos;
step = "查询";
using (var localContext = new TransactionContext(Dmo.NewSession(ConfigUtil.ConnectionStr))) {
var dom = new DmoQuery(typeof(WeightTable));
dom.Where.Conditions.Add(DQCondition.EQ("IsExport", false));
dom.Range = SelectRange.Top(50);
dmos = localContext.Session.ExecuteList(dom);
}
if (dmos == null || dmos.Count == 0)
return true;
var ids = dmos.Cast<WeightTable>().Where(weightBo => IdList.Contains(weightBo.ID)).ToList();
foreach (var weightBo in ids) {
dmos.Remove(weightBo);
}
step = "同步";
using (var context = new TransactionContext(Dmo.NewSession(ConfigUtil.RemoteConStr))) {
foreach (WeightTable weightBo in dmos) {
if (weightBo.LastExportFail) {
var dom = new DQueryDom(new JoinAlias(typeof(WeightTable)));
dom.Columns.Add(DQSelectColumn.Field("ID"));
dom.Where.Conditions.Add(DQCondition.EQ("SourceID", weightBo.ID));
var id = context.Session.ExecuteScalar(dom);
if (id != null) {
IdList.Add(weightBo.ID);
continue;
}
}
if (SequenceList.ContainsKey(weightBo.ID)) {
var dom = new DQueryDom(new JoinAlias(typeof(WeightTable)));
dom.Columns.Add(DQSelectColumn.Field("ID"));
dom.Where.Conditions.Add(DQCondition.EQ("SourceID", weightBo.ID));
var id = context.Session.ExecuteScalar(dom);
if (id != null) {
IdList.Add(weightBo.ID);
continue;
}
} else {
SequenceList.Add(weightBo.ID, new Tuple<DateTime, long>(weightBo.DateTime, weightBo.Sequence));
}
var newBo = new WeightTable();
newBo.DateTime = weightBo.DateTime;
newBo.Livestock_ID = weightBo.Livestock_ID;
newBo.Weight = weightBo.Weight;
newBo.GoodsBatchName = weightBo.GoodsBatchName;
newBo.Remark = weightBo.Remark;
newBo.Livestock_Name = weightBo.Livestock_Name;
newBo.SubWeight = weightBo.SubWeight;
newBo.PhaseCode = weightBo.PhaseCode;
newBo.Sequence = weightBo.Sequence;
newBo.IP = weightBo.IP;
newBo.Mac = weightBo.Mac;
newBo.SourceID = weightBo.ID;
context.Session.AddNew(newBo);
}
context.Commit();
}
step = "修改状态";
foreach (WeightTable weightBo in dmos) {
IdList.Add(weightBo.ID);
if (SequenceList.ContainsKey(weightBo.ID)) {
SequenceList.Remove(weightBo.ID);
}
}
using (var localContext = new TransactionContext(Dmo.NewSession(ConfigUtil.ConnectionStr))) {
var sql = string.Format("UPDATE dbo.[WeightTable] SET IsExport =1,LastExportFail = 0 WHERE ID in ({0})", string.Join(",", IdList));
localContext.Session.ExecuteSqlNonQuery(sql);
localContext.Commit();
}
IdList.Clear();
LogUtil.WriteLog(DateTime.Now + ":服务器数据同步成功!");
} catch (Exception ex) {
var er = SequenceList.Keys.ToList();
msg = "服务器数据同步失败!" + ex.Message + "|" + step + string.Join(",", IdList) + "|" + string.Join(",", er);
LogUtil.WriteLog(DateTime.Now + ":" + msg);
if (er.Count > 0)
using (var localContext = new TransactionContext(Dmo.NewSession(ConfigUtil.ConnectionStr))) {
var sql = string.Format("UPDATE dbo.WeightTable SET LastExportFail = 1 WHERE ID in ({0})", string.Join(",", er));
localContext.Session.ExecuteSqlNonQuery(sql);
localContext.Commit();
}
success = false;
}
return success;
}
public static bool TryDeleteServerData(string id, out string msg)
{
try
{
using (var localContext = new TransactionContext(Dmo.NewSession(ConfigUtil.RemoteConStr)))
{
var sql = "delete from dbo.[WeightTable] where SourceID ='" + id +"'";
localContext.Session.ExecuteSqlNonQuery(sql);
localContext.Commit();
}
msg = "";
return true;
}
catch (Exception e)
{
msg = e.ToString();
LogUtil.WriteLog(DateTime.Now + ":" + msg);
return false;
}
}
public static bool TryUpdateLevel(string id, long newLevelId, string newLevelName,out string msg)
{
try
{
using (var localContext = new TransactionContext(Dmo.NewSession(ConfigUtil.RemoteConStr)))
{
var updateSql = "update [WeightTable] set [Livestock_ID]=" + newLevelId + ",[Livestock_Name]='" + newLevelName + "' where SourceID="+id+" ";
localContext.Session.ExecuteSqlNonQuery(updateSql);
localContext.Commit();
}
msg = "";
return true;
}
catch (Exception e)
{
msg = e.ToString();
LogUtil.WriteLog(DateTime.Now + ":" + msg);
return false;
}
}
}
}