Beispiel #1
0
 public void Transcation(Action action)
 {
     using (IRedisTransaction irt = this.iClient.CreateTransaction())
     {
         try
         {
             action.Invoke();
             irt.Commit(); // 提交事务
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             irt.Rollback();
             throw ex;
         }
         finally {
             irt.Dispose();
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// Redis事务
 /// </summary>
 /// <param name="action">执行的方法</param>
 /// <param name="redisClient">导入redis客户端</param>
 public static void RedisContextInvoke(Action action, IRedisClient redisClient)
 {
     using (IRedisTransaction irt = redisClient.CreateTransaction())
     {
         try
         {
             action.Invoke();
             irt.Commit();
         }
         catch (Exception ex)
         {
             if (irt != null)
             {
                 irt.Rollback();
             }
             throw ex;
         }
         finally
         {
             irt.Dispose();
         }
     }
 }
 public void Dispose()
 {
     _transaction.Dispose();
 }
 public void Dispose()
 {
     _client?.Dispose();
     _transaction?.Dispose();
 }
 public void RollbackTransaction()
 {
     trans.Dispose();
 }
Beispiel #6
0
        /// <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);
        }
Beispiel #7
0
 public void Dispose()
 {
     _tran.Dispose();
     _redisContext.Dispose();
 }
Beispiel #8
0
        /// <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);
        }