using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WCS_Client
{
    public class TryCachHelper
    {
        public static void TryExecute(Action action)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
            }
        }
        public static string TryExecute2(Action action)
        {
            string result = string.Empty;
            try
            {
                action();
            }
            catch (Exception ex)
            {
                result = ex.Message;
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
            }
            return result;
        }
        /// 
        /// 无事务无返回值的通用代码
        /// 
        /// 
        /// 
        public static string TryExecute(Action action)
        {
            string msg = string.Empty;
            SqlSugarClient db = null;
            try
            {
                db = SugarClient.GetInstance();
                action(db);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
                if (db != null) ((IDisposable)db).Dispose();
            }
            return msg;
        }
        /// 
        /// 无事务的有返回值通用代码
        /// 
        /// 
        /// 
        public static string TryFuncExecute(Func func)
        {
            string msg = string.Empty;
            SqlSugarClient db = null;
            try
            {
                db = SugarClient.GetInstance();
                msg = func(db);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
                if (db != null) ((IDisposable)db).Dispose();
            }
            return msg;
        }
        public static void TryExecute(Action action, string methodName)
        {
            SqlSugarClient db = null;
            try
            {
                db = SugarClient.GetInstance();
                action(db);
            }
            catch (Exception ex)
            {
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
                if (db != null) ((IDisposable)db).Dispose();
            }
        }
        /// 
        /// 有事务的无返回值通用代码
        /// 
        /// 
        /// 
        public static string TryTranExecute(Action action)
        {
            string msg = string.Empty;
            SqlSugarClient db = null;
            try
            {
                db = SugarClient.GetInstance();
                db.Ado.BeginTran();
                action(db);
                db.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                if (db != null) db.Ado.RollbackTran();
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
                if (db != null) ((IDisposable)db).Dispose();
            }
            return msg;
        }
        /// 
        /// 有事务的无返回值通用代码
        /// 
        /// 
        /// 
        public static string TryTranExecute(Action action, int loc)
        {
            string msg = string.Empty;
            if (Interlocked.Exchange(ref loc, 1) == 0)
            {
                SqlSugarClient db = null;
                try
                {
                    db = SugarClient.GetInstance();
                    db.Ado.BeginTran();
                    action(db);
                    db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    if (db != null) db.Ado.RollbackTran();
                    LogHelper.Sys_Log.WriteLog(ex.ToString());
                }
                finally
                {
                    if (db != null) ((IDisposable)db).Dispose();
                    Interlocked.Exchange(ref loc, 0);
                }
            }
            return msg;
        }
        /// 
        /// 有事务的有返回值通用代码
        /// 
        /// 
        /// 
        public static object TryTranFuncExecute(Func func)
        {
            object obj = null;
            SqlSugarClient db = null;
            try
            {
                db = SugarClient.GetInstance();
                db.Ado.BeginTran();
                obj = func(db);
                db.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                obj = null;
                if (db != null) db.Ado.RollbackTran();
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
                if (db != null) ((IDisposable)db).Dispose();
            }
            return obj;
        }
        public static void TryTranExecute(Action action, string methodName)
        {
            SqlSugarClient db = null;
            try
            {
                db = SugarClient.GetInstance();
                db.Ado.BeginTran();
                action(db);
                db.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                if (db != null) db.Ado.RollbackTran();
                LogHelper.Sys_Log.WriteLog(ex.ToString());
            }
            finally
            {
                if (db != null) ((IDisposable)db).Dispose();
            }
        }
    }
}