private const int ORGID = 100035;//使用组织 /// <summary> /// 从Redis获取同步数据 /// </summary> /// <param name="ctx"></param> /// <param name="dataType"></param> /// <param name="dbId"></param> /// <param name="numbers"></param> /// <returns></returns> public static List <AbsSynchroDataInfo> GetSynchroDatas(Context ctx, SynchroDataType dataType, long dbId, IEnumerable <string> numbers = null, SynchroDirection direction = SynchroDirection.ToK3) { List <AbsSynchroDataInfo> datas = null; RedisManager manager = new RedisManager(ctx); IRedisClient redis = manager.GetClientEx(ctx, dbId); IEnumerable <string> keys = null; HashSet <string> infos = null; Dictionary <string, string> dict = null; if (redis != null) { dict = RedisKeyUtils.GetRedisSetKey(dataType, SynchroDirection.ToK3); if (numbers != null && numbers.Count() > 0) { keys = numbers; } else { if (dict != null && dict.Count > 0) { if (!string.IsNullOrEmpty(dict["unreadKey"])) { keys = manager.GetAllItemsFromSet(dict["unreadKey"], ctx, dbId); } } } if (keys != default(HashSet <string>) && keys.Count() > 0) { infos = new HashSet <string>(); if (!string.IsNullOrEmpty(dict["infoKey"])) { foreach (var item in keys) { if (!string.IsNullOrWhiteSpace(item)) { string info = dict["infoKey"] + item; if (!string.IsNullOrEmpty(info)) { infos.Add(info.Trim()); } } } } } datas = GetSynchroObjects(ctx, dataType, redis, infos, direction); redis.Dispose(); redis = null; } return(datas); }
/// <summary> /// 删除Redis数据库中的数据 /// </summary> /// <param name="ctx"></param> /// <param name="numbers"></param> /// <param name="dataType"></param> public virtual void RemoveRedisData(Context ctx, IEnumerable <string> numbers, SynchroDataType dataType = SynchroDataType.None) { long dbId = 0; if (this.K3CloudContext == null) { dbId = GetRedisDbId(ctx); } else { dbId = this.RedisDbId; } if (true /*K3LoginInfo.GetRedisServerIp(ctx).CompareTo(DataBaseConst.RedisServerIP) == 0*/) { List <string> infoKeys = null; RedisManager manager = new RedisManager(ctx); if (numbers != null && numbers.Count() > 0) { infoKeys = new List <string>(); foreach (var num in numbers) { if (!string.IsNullOrWhiteSpace(num)) { if (dataType.CompareTo(SynchroDataType.None) == 0) { infoKeys.Add(this.RedisInfoKey + num); manager.RemoveItemFromSet(ctx, this.RedisUnreadkey, num, dbId); } else { infoKeys.Add(RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["infoKey"] + num); manager.RemoveItemFromSet(ctx, RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["unreadKey"], num, dbId); } } } if (DataBaseConst.CurrentRedisServerIp.CompareTo(DataBaseConst.HKRedisIP) == 0) { manager.RemoveAll(ctx, infoKeys, this.RedisDbId); } manager = null; } } }
/// <summary> /// 复制Redis正式库数据至测试库 /// </summary> /// <param name="ctx"></param> /// <param name="dict"></param> /// <param name="keys"></param> /// <param name="dataType"></param> public static void BackupDataToRedis(Context ctx, Dictionary <string, string> dict, IEnumerable <string> keys, SynchroDataType dataType, SynchroDirection direction) { long backupRedisId = 0; try { Dictionary <string, string> redisKey = RedisKeyUtils.GetRedisSetKey(dataType, SynchroDirection.ToK3); switch (direction) { case SynchroDirection.ToK3: backupRedisId = 1; break; case SynchroDirection.ToB2B: backupRedisId = 2; break; default: break; } if (DataBaseConst.CurrentRedisServerIp.CompareTo(DataBaseConst.HKRedisIP) == 0) { RedisManager manager = new RedisManager(ctx); Thread subTh = new Thread(new ThreadStart(() => { using (IRedisClient redis = manager.GetClientTest(ctx, backupRedisId)) { if (redis != null) { if (dict != null && dict.Count > 0) { redis.SetAll(dict); if (keys != null && keys.Count() > 0) { if (!string.IsNullOrWhiteSpace(redisKey["unreadKey"]) && !string.IsNullOrWhiteSpace(redis["allKey"])) { if (GetItems(keys) != null && GetItems(keys).Count > 0) { redis.AddRangeToSet(redisKey["unreadKey"], GetItems(keys)); redis.AddRangeToSet(redis["allKey"], GetItems(keys)); } } } } redis.Dispose(); } } } )); subTh.IsBackground = true; subTh.Start(); } } catch (Exception ex) { LogUtils.WriteSynchroLog(ctx, SynchroDataType.Redis, ex.Message + System.Environment.NewLine + ex.StackTrace); } }
/// <summary> /// 获取需要同步的数据(过滤掉100.03组织的单据数据) /// </summary> /// <param name="datas"></param> /// <returns></returns> public Dictionary <List <string>, Dictionary <string, string> > GetSynchroData(Context ctx, IEnumerable <AbsSynchroDataInfo> datas, SynchroDataType dataType = default(SynchroDataType)) { Dictionary <List <string>, Dictionary <string, string> > dicts = null; Dictionary <string, string> dict = null; List <string> keys = null; if (datas != null && datas.Count() > 0) { dicts = new Dictionary <List <string>, Dictionary <string, string> >(); dict = new Dictionary <string, string>(); keys = new List <string>(); var group = default(IEnumerable <IGrouping <string, AbsSynchroDataInfo> >); List <AbsDataInfo> infos = null; if (this.DataType == SynchroDataType.SaleOrder || this.DataType == SynchroDataType.SaleOrderOffline || this.DataType == SynchroDataType.ReceiveBill || this.DataType == SynchroDataType.ReFundBill || this.DataType == SynchroDataType.DropShippingSalOrder) { infos = datas.Select(d => (AbsDataInfo)d).ToList(); group = from g in infos where g != null && !string.IsNullOrWhiteSpace(g.SrcNo) && !string.IsNullOrWhiteSpace(g.FSaleOrgId) && g.FSaleOrgId.CompareTo("100.03") != 0 && ((g.DataType.CompareTo(SynchroDataType.SaleOrder) == 0 && (!string.IsNullOrWhiteSpace(g.F_HS_SaleOrderSource) && g.F_HS_SaleOrderSource.CompareTo("HCWebPendingOder") == 0 && !string.IsNullOrWhiteSpace(g.FCancelStatus) && g.FCancelStatus.CompareTo("B") == 0) ) || g.DataType.CompareTo(SynchroDataType.SaleOrderOffline) == 0 || g.DataType.CompareTo(SynchroDataType.ReceiveBill) == 0 || g.DataType.CompareTo(SynchroDataType.ReFundBill) == 0 || g.DataType.CompareTo(SynchroDataType.DropShippingSalOrder) == 0 ) group g by g.SrcNo into g select g; } else { group = from g in datas group g by g.SrcNo into g select g; } if (group != null && group.Count() > 0) { foreach (var g in group) { if (g != null && g.ToList().Count > 0) { keys.Add(g.Key); if (dataType == default(SynchroDataType)) { dict.Add(RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction)["infoKey"] + g.Key, JsonUtils.SerializeObject <IEnumerable <AbsSynchroDataInfo> >(ctx, g.ToList())); } else { dict.Add(RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["infoKey"] + g.Key, JsonUtils.SerializeObject <IEnumerable <AbsSynchroDataInfo> >(ctx, g.ToList())); } } } dicts.Add(keys, dict); } } return(dicts); }
/// <summary> /// 单据审核成功后同步数据到HC网站 /// </summary> /// <param name="ctx"></param> /// <param name="objects"></param> /// <returns></returns> public virtual HttpResponseResult OperateAfterAudit(Context ctx, List <DynamicObject> objects = null, IEnumerable <AbsSynchroDataInfo> datas = null, SynchroDataType dataType = default(SynchroDataType)) { IEnumerable <AbsSynchroDataInfo> oDatas = null; IRedisClient client = null; IRedisTransaction trans = null; HttpResponseResult result = null; Dictionary <List <string>, Dictionary <string, string> > dicts = null; if (objects != null && objects.Count > 0) { oDatas = GetK3Datas(ctx, objects, ref result); } if (datas != null && datas.Count() > 0) { oDatas = datas; } if (result == null) { result = new HttpResponseResult(); result.Success = true; } SqlCommand comm = null; bool flag = true; try { if (oDatas != null && oDatas.Count() > 0) { int count = 0; string sql = GetExecuteUpdateSql(ctx, oDatas.ToList()); comm = UpdateBillInfos(ctx, oDatas.ToList(), sql, ref result); if (comm != null) { count = comm.ExecuteNonQuery(); } dicts = GetSynchroData(ctx, oDatas, dataType); if (dicts != null && dicts.Count > 0) { RedisManager manager = new RedisManager(ctx); client = manager.GetClientEx(ctx, GetDBID(ctx)); trans = client.CreateTransaction(); foreach (var kv in dicts) { trans.QueueCommand(r => r.SetAll(kv.Value)); if (dataType == default(SynchroDataType)) { trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction)["allKey"], kv.Key)); trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction)["unreadKey"], kv.Key)); } else { trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["allKey"], kv.Key)); trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["unreadKey"], kv.Key)); } } } else { result.Success = result.Success && true; } if (count > 0) { if (comm != null) { try { comm.Transaction.Commit(); flag = true; } catch (Exception ex) { flag = false; result.Success = result.Success && false; result.Message += ex.Message + Environment.NewLine + ex.StackTrace; LogUtils.WriteSynchroLog(ctx, this.DataType, result.Message); } } } if (trans != null) { flag = trans.Commit(); } if (!flag) { if (trans != null) { trans.Rollback(); } if (comm != null) { if (comm.Transaction != null) { comm.Transaction.Rollback(); } } result.Success = result.Success && false; result.Message += string.Format("【{0}】单据编码【{1}】数据更新和同步到HC网站失败!", dataType == SynchroDataType.None ? DataType : dataType, string.Join(",", oDatas.Select(o => o.SrcNo).ToList())); LogUtils.WriteSynchroLog(ctx, dataType == SynchroDataType.None ? DataType : dataType, result.Message); } else { foreach (var kv in dicts) { if (kv.Value != null && kv.Value.Count > 0) { LogHelper.WriteSynchroDataLog(ctx, this.DataType, client, kv.Value, true); } } result.Success = result.Success && true; result.Message += string.Format("【{0}】单据编码【{1}】数据更新和同步到HC网站成功!", dataType == SynchroDataType.None ? DataType : dataType, string.Join(",", oDatas.Select(o => o.SrcNo).ToList())); LogUtils.WriteSynchroLog(ctx, dataType == SynchroDataType.None ? DataType : dataType, result.Message); } } else { result.Success = result.Success && true; } } catch (Exception ex) { if (result != null) { result.Success = result.Success && false; result.Message += ex.Message + Environment.NewLine + ex.StackTrace; LogUtils.WriteSynchroLog(ctx, this.DataType, result.Message); } if (trans != null) { trans.Rollback(); } if (client != null) { client.Dispose(); } if (comm != null) { if (comm.Transaction != null) { comm.Transaction.Rollback(); } } } finally { if (trans != null) { trans.Dispose(); } if (client != null) { client.Dispose(); } if (comm != null) { if (comm.Connection != null) { comm.Connection.Close(); } } } return(result); }
/// <summary> /// Redis string类型的具体数据 /// </summary> public static string RedisInfoKey(SynchroDataType DataType, SynchroDirection Direction) { return(RedisKeyUtils.GetRedisSetKey(DataType, Direction)["infoKey"]); }
/// <summary> /// Redis未读的Set类型的key /// </summary>taType, SynchroDirection public static string RedisUnreadkey(SynchroDataType DataType, SynchroDirection Direction) { return(RedisKeyUtils.GetRedisSetKey(DataType, Direction)["unreadKey"]); }
/// <summary> /// 同步K3数据至HC网站 /// </summary> /// <param name="k3Datas"></param> /// <param name="billNos"></param> /// <returns></returns> public virtual HttpResponseResult SynK3DataToWebSite(IEnumerable <AbsSynchroDataInfo> k3Datas = null, IEnumerable <string> billNos = null, bool SQLFilter = true, SynchroDirection direction = SynchroDirection.Default) { HttpResponseResult result = null; Dictionary <string, string> dict = null; IEnumerable <AbsSynchroDataInfo> datas = null; List <string> SynBillNos = null; bool IsSuccess = false; IRedisTransaction trans = null; lock (oLock) { if (k3Datas == null) { datas = GetK3Datas(billNos, SQLFilter); } else { datas = k3Datas; } try { Dictionary <string, string> redisKeys = RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction); string allKey = redisKeys["allKey"]; string unreadKey = redisKeys["unreadKey"]; Dictionary <string, string> synFailDatas = GetSynchroFailureDatas(); List <string> failBillNos = null; List <string> failInfoKeys = null; trans = RedisClient.CreateTransaction(); if (synFailDatas != null && synFailDatas.Count > 0) { failBillNos = GetSychroFailureBillNos(synFailDatas); failInfoKeys = GetSynchroFailureInfoKeys(synFailDatas); trans.QueueCommand(r => r.AddRangeToSet(allKey, failBillNos)); trans.QueueCommand(r => r.AddRangeToSet(unreadKey, failBillNos)); trans.QueueCommand(r => r.SetAll(synFailDatas)); IsSuccess = trans.Commit(); trans.Dispose(); if (IsSuccess) { UpdateSynchroDataLog(failInfoKeys); } } if (datas != null && datas.Count() > 0) { dict = new Dictionary <string, string>(); SynBillNos = new List <string>(); var group = from d in datas group d by d.SrcNo into g select g; if (group != null && group.Count() > 0) { foreach (var g in group) { if (g != null && g.ToList().Count > 0) { SynBillNos.Add(g.Key); string infoKey = RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction)["infoKey"] + g.Key; dict.Add(infoKey, JsonUtils.SerializeObject <IEnumerable <AbsSynchroDataInfo> >(this.K3CloudContext, g.ToList())); } } if (IsConnectSuccess()) { if (dict.Count > 0) { trans = RedisClient.CreateTransaction(); trans.QueueCommand(r => r.AddRangeToSet(allKey, SynBillNos)); trans.QueueCommand(r => r.AddRangeToSet(unreadKey, SynBillNos)); trans.QueueCommand(r => r.SetAll(dict)); IsSuccess = trans.Commit(); trans.Dispose(); if (IsSuccess) { UpdateAfterSynchro(datas, true); LogHelper.WriteSynchroDataLog(this.K3CloudContext, this.DataType, RedisClient, dict, true); string msg = "【" + this.DataType + "】同步,单据编码" + FormatNumber(datas) + "信息成功同步到Redis"; LogHelper.WriteSynchroLog_Succ(this.K3CloudContext, this.DataType, msg); result = new HttpResponseResult(); result.Success = true; result.Message = msg; } else { LogHelper.WriteSynchroDataLog(this.K3CloudContext, this.DataType, RedisClient, dict, false); string msg = "【" + this.DataType + "】同步,单据编码" + FormatNumber(datas) + "信息同步到Redis失败"; LogUtils.WriteSynchroLog(this.K3CloudContext, this.DataType, msg); result = new HttpResponseResult(); result.Success = false; result.Message = msg; } } } else { LogHelper.WriteSynchroDataLog(this.K3CloudContext, this.DataType, RedisClient, dict, false); } } } else { result = new HttpResponseResult(); result.Success = false; result.Message = "没有需要同步的数据"; LogUtils.WriteSynchroLog(this.K3CloudContext, this.DataType, result.Message); } } catch (Exception ex) { LogHelper.WriteSynchroDataLog(this.K3CloudContext, this.DataType, RedisClient, dict, false); string msg = "【" + this.DataType + "】同步过程中出现异常,异常信息:" + ex.Message + System.Environment.NewLine + ex.StackTrace; LogUtils.WriteSynchroLog(this.K3CloudContext, this.DataType, msg); result = new HttpResponseResult(); result.Success = false; result.Message = msg; UpdateAfterSynchro(datas, false); } finally { if (trans != null) { trans.Dispose(); } } } return(result); }