﻿/*===================================================================== 
* Copyright © 2014 www.icaiban.com All Rights Reserved.
* CLR版本： 4.0.30319.18444 
* 类名称：  Tab_Wechat_ServiceDAL 
* 创建人：  jimmy
* 创建时间：2014/11/19 10:42:19 
* 修改人：
* 修改时间：
* 修改备注：
* 版本：@version 1.0
===================================================================== */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICaiBan.ModelDB;
using ICaiBan.Framework.ORM;
using System.Data;
using ICaiBan.ModelBL.Merchant;
using ICaiBan.DAL.Common;
using ICaiBan.ModelBL.Enums;
using System.Data.SqlClient;
using ICaiBan.Framework.DB;

namespace ICaiBan.DAL.Wechat
{
    /// <summary>
    /// 微信服务号
    /// </summary>
    public class Tab_Wechat_ServiceDAL
    {
        internal static EntityDbContext<Tab_Wechat_Service> caiBanDbContext = EntityDbContext<Tab_Wechat_Service>.GetInstance(Config.ConnString);

        /// <summary>
        /// 新增微信服务号
        /// </summary>
        /// <param name="bl"></param>
        /// <param name="newid"></param>
        /// <returns></returns>
        public static bool Add(WechatServiceBL bl, out string newid)
        {
            Tab_Wechat_Service model = new Tab_Wechat_Service();
            model.AppId = bl.AppId;
            model.Original_Id = bl.OriginalId;
            model.AppSecret = bl.AppSecret;
            model.Serv_Name = bl.ServName;
            model.Wechat_Id = bl.WechatId;
            model.Wechat_Id_Type = (decimal)bl.WechatIdType;
            model.Scene_Id = bl.SceneId;
            model.Scene_Temp_Id = bl.SceneTempId;
            model.Access_Token = bl.AccessToken;
            model.Access_Token_Lastly_Time = bl.AccessTokenLastlyTime;
            model.Is_Delete = bl.IsDelete;
            model.Create_Time = bl.CreateTime;
            model.Create_User_Id = bl.CreateUserId;
            model.Modify_Time = bl.ModifyTime;
            model.Modify_User_Id = bl.ModifyUserId;
            return caiBanDbContext.Insert(model, out newid);
        }

        #region 查询

        #region 通过商户Id查找微信服务号
        /// <summary>
        /// 通过商户Id查找微信服务号
        /// </summary>
        /// <param name="merchantId">商户Id</param>
        /// <returns>返回商户微信服务信息对象</returns>
        /// <remarks>added by jimmy,2014-11-19</remarks>
        public static WechatServiceBL SelectWechatService(decimal merchantId)
        {
            string sql = @"  select a.* from Tab_Wechat_Service a LEFT join (SELECT * from Tab_Merc_Wechat where Is_Delete = 0) b on a.Service_Id = b.Service_Id
WHERE b.Merchant_Id = @Merchant_Id";
            SqlParameter[] parames = { new SqlParameter("@Merchant_Id", SqlDbType.Decimal) };
            parames[0].Value = merchantId;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion

        #region 根据AppID查询是否存在该数据
        /// <summary>
        /// 根据AppID查询是否存在该数据
        /// </summary>
        /// <param name="appId">微信公众号ID</param>
        /// <returns>是否存在</returns>
        /// <remarks>李晓乐 2014-11-19</remarks>
        public static bool SelectWechatService(string appId)
        {
            string sql = @"  SELECT count(*) FROM  Tab_Wechat_Service 
                            where AppId = @appId and is_delete=0 ";

            SqlParameter[] parames =
            {
                new SqlParameter("@appId", SqlDbType.NVarChar)
            };
            parames[0].Value = appId;
            object obj = SqlDbHelper.ExecuteScalar(Config.ConnString, CommandType.Text, sql, parames);

            return ConvertObjectData.ConvertToInt(obj) > 0;
        }

        #endregion
        #region 根据AppID查询是否存在该数据
        /// <summary>
        /// 根据AppID查询是否存在该数据
        /// </summary>
        /// <param name="appId">微信公众号ID</param>
        /// <param name="serviceType">生码资源类型（0：自有服务号；1：第三方服务号；2：菜板服务号）</param>
        /// <returns>是否存在,存在>0，不存在 null</returns>
        /// <remarks>李晓乐 2014-11-19</remarks>
        public static decimal SelectWechatService(string appId, EServiceIdType serviceType)
        {
            string sql = @"  SELECT Service_Id FROM  Tab_Wechat_Service 
                            where AppId = @appId and is_delete=0 and Wechat_Id_Type=@serviceType";

            SqlParameter[] parames =
            {
                new SqlParameter("@appId", SqlDbType.NVarChar), new SqlParameter("@serviceType", SqlDbType.Decimal)
            };
            parames[0].Value = appId;
            parames[1].Value = (decimal)serviceType;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0].ServiceId;
                }
            }
            return 0;
        }

        #endregion

        #region 根据原始ID查询是否存在该数据
        /// <summary>
        /// 根据原始ID查询是否存在该数据
        /// </summary>
        /// <param name="origId">原始ID</param>
        /// <returns>是否存在</returns>
        /// <remarks>李晓乐 2014-11-19</remarks>
        public static bool SelectWechatByOrigId(string origId)
        {
            string sql = @"  SELECT count(*) FROM  Tab_Wechat_Service 
                            where Original_Id = @origId and is_delete=0 ";

            SqlParameter[] parames =
            {
                new SqlParameter("@origId", SqlDbType.NVarChar)
            };
            parames[0].Value = origId;
            object obj = SqlDbHelper.ExecuteScalar(Config.ConnString, CommandType.Text, sql, parames);

            return ConvertObjectData.ConvertToInt(obj) > 0;
        }

        #endregion

        #region 根据微信服务号ID查询微信公众号ID
        /// <summary>
        /// 根据微信服务号ID查询微信公众号ID
        /// </summary>
        /// <param name="serviceId">微信服务号ID</param>
        /// <returns>微信号信息</returns>
        /// <remarks>李晓乐 2014-11-19</remarks>
        public static WechatServiceBL SelectWechatByServiceId(decimal serviceId)
        {
            string sql = @"  SELECT * FROM  Tab_Wechat_Service 
                            where Service_Id = @serviceId and is_delete=0 ";
            SqlParameter[] parames =
            {
                new SqlParameter("@serviceId", SqlDbType.Decimal)
            };
            parames[0].Value = serviceId;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            List<WechatServiceBL> list = null;
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion

        #region 通过商户Id查询商户微信的
        /// <summary>
        /// 通过商户Id查询商户微信的
        /// </summary>
        /// <param name="merchantId">商户Id</param>
        /// <returns>返回商户微信信息</returns>
        /// <remarks>added by 李晓乐,2014-11-19</remarks>
        public static WechatServiceBL GetMercAppIdSecret(decimal merchantId)
        {
            string sql = string.Empty;
            sql = @"select w.AppId,w.AppSecret,w.Original_Id,w.Wechat_Id_Type,w.Service_Id,w.Access_Token,w.Access_Token_Lastly_Time from Tab_Merc_Wechat m 
                            inner join Tab_Wechat_Service w  on w.Service_Id=m.Service_Id AND w.Is_Delete=0
                           where m.Merchant_Id  = @Merchant_Id and m.Is_Delete = 0";

            SqlParameter[] parames = { new SqlParameter("@Merchant_Id", SqlDbType.Decimal) };
            parames[0].Value = merchantId;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion

        #region 通过商户Id查询商户微信的
        /// <summary>
        /// 通过商户Id查询商户微信的
        /// </summary>
        /// <param name="merchantId">商户Id</param>
        /// <returns>返回商户微信信息</returns>
        /// <remarks>added by 李晓乐,2014-11-19</remarks>
        public static WechatServiceBL GetQueueAppIdSecret(decimal merchantId)
        {
            string sql = string.Empty;

            sql = @"select w.AppId,w.AppSecret,w.Original_Id,w.Wechat_Id_Type,w.Service_Id,w.Access_Token,w.Access_Token_Lastly_Time from Tab_Queue_Parm q 
                            inner join Tab_Wechat_Service w  on w.Service_Id=q.Service_Id AND w.Is_Delete=0
                           where q.Merchant_Id  = @Merchant_Id  and q.Is_Delete=0";
            SqlParameter[] parames = { new SqlParameter("@Merchant_Id", SqlDbType.Decimal) };
            parames[0].Value = merchantId;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion

        #region 通过商户Id查询商户微信的[AppId][AppSecret][OriginalId][Wechat_Id_Type]
        /// <summary>
        /// 通过商户Id查询商户微信的[AppId][AppSecret][OriginalId][Wechat_Id_Type]
        /// </summary>
        /// <param name="merchantId">商户Id</param>
        /// <param name="serviceType">生码资源类型（0：自有服务号；1：第三方服务号；2：菜板服务号）</param>
        /// <returns>返回商户微信信息</returns>
        /// <remarks>added by 李晓乐,2014-11-19</remarks>
        public static WechatServiceBL GetAppIdSecret(decimal merchantId, EServiceIdType serviceType)
        {
            string sql = @"select w.AppId,w.AppSecret,w.Original_Id,w.Wechat_Id_Type from Tab_Merc_Wechat m 
                            inner join Tab_Wechat_Service w  on w.Service_Id=m.Service_Id AND w.Is_Delete=0
                           where m.Merchant_Id  = @Merchant_Id and w.Wechat_Id_Type=@serviceType and m.Is_Delete = 0";
            SqlParameter[] parames = { new SqlParameter("@Merchant_Id", SqlDbType.Decimal), new SqlParameter("@serviceType", SqlDbType.Decimal) };
            parames[0].Value = merchantId;
            parames[1].Value = (decimal)serviceType;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion

        #region 通过生码资源类型(2：菜板服务号)查询第一条商户微信的[AppId][AppSecret][OriginalId][Wechat_Id_Type]
        /// <summary>
        /// 通过生码资源类型(2：菜板服务号)查询第一条商户微信的[AppId][AppSecret][OriginalId][Wechat_Id_Type]
        /// </summary>
        /// <returns>返回商户微信信息</returns>
        /// <remarks>added by 李晓乐,2014-11-19</remarks>
        public static WechatServiceBL GetAppIdService()
        {
            string sql = @"select top 1 [Service_Id]
                          ,[AppId]
                          ,[Original_Id]
                          ,[AppSecret]
                          ,[Serv_Name]
                          ,[Wechat_Id]
                          ,[Wechat_Id_Type]
                          ,[Scene_Id]
                          ,[Scene_Temp_Id]
                          ,[Access_Token]
                          ,[Access_Token_Lastly_Time] from Tab_Wechat_Service where Wechat_Id_Type=2 and Is_Delete = 0";
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, null);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion

        #region 根据AppID查询商户微信
        /// <summary>
        /// 根据AppID查询商户微信
        /// </summary>
        /// <param name="appId">微信公众号ID</param>
        /// <returns>返回商户微信信息</returns>
        /// <remarks>李晓乐 2014-11-19</remarks>
        public static WechatServiceBL GetAppIdSecret(string appId)
        {
            string sql = @"  SELECT * FROM  Tab_Wechat_Service 
                            where AppId = @appId and is_delete=0 ";

            SqlParameter[] parames =
            {
                new SqlParameter("@appId", SqlDbType.NVarChar)
            };
            parames[0].Value = appId;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }

        #endregion

        #region 根据原始ID查询微信服务号信息
        /// <summary>
        /// 根据原始ID查询微信服务号信息
        /// </summary>
        /// <param name="origId">原始ID</param>
        /// <returns>返回微信服务号信息</returns>
        /// <remarks>added by jimmy, 2014-12-3</remarks>
        public static WechatServiceBL GetWechatByOrigId(string origId)
        {
            string sql = @" SELECT [Service_Id]
                          ,[AppId]
                          ,[Original_Id]
                          ,[AppSecret]
                          ,[Serv_Name]
                          ,[Wechat_Id]
                          ,[Wechat_Id_Type]
                          ,[Scene_Id]
                          ,[Scene_Temp_Id]
                          ,[Access_Token]
                          ,[Access_Token_Lastly_Time]
                      FROM  [Tab_Wechat_Service] where Original_Id = @origId AND Is_Delete = 0 ";

            SqlParameter[] parames =
            {
                new SqlParameter("@origId", SqlDbType.NVarChar)
            };
            parames[0].Value = origId;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }

        #endregion

        #region 获取菜版网服务号信息
        /// <summary>
        /// 获取菜版网服务号信息
        /// </summary>
        /// <returns>返回菜版网服务号信息</returns>
        /// <remarks>added by jimmy, 2014-12-9</remarks>
        public static WechatServiceBL GetWechatCaiban(EServiceIdType eServiceIdType)
        {
            string sql = @"SELECT [Service_Id]
                          ,[AppId]
                          ,[Original_Id]
                          ,[AppSecret]
                          ,[Serv_Name]
                          ,[Wechat_Id]
                          ,[Wechat_Id_Type]
                          ,[Scene_Id]
                          ,[Scene_Temp_Id]
                          ,[Access_Token]
                          ,[Access_Token_Lastly_Time]
                      FROM  [Tab_Wechat_Service] where Wechat_Id_Type = @Wechat_Id_Type and Is_Delete = 0 ";

            SqlParameter[] parames =
            {
                new SqlParameter("@Wechat_Id_Type", SqlDbType.Decimal)
            };
            parames[0].Value = (decimal)eServiceIdType;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }

        #endregion

        #region 通过微信OpenId链表查询商户微信的AppId
        /// <summary>
        /// 通过微信OpenId链表查询商户微信的AppId
        /// </summary>
        /// <param name="openId">微信OpenId</param>
        /// <returns>返回商户微信信息</returns>
        /// <remarks>added by 李晓乐,2014-12-31</remarks>
        public static WechatServiceBL GetQueueAppId(string openId)
        {
            string sql = string.Empty;

            sql = @"select w.AppId,w.Wechat_Id_Type,w.Service_Id,w.Access_Token,w.AppSecret from Tab_Wechat_Fo_Record r 
                            inner join Tab_Wechat_Service w  on w.Service_Id=r.Service_Id AND w.Is_Delete=0
                           where r.OpenId  = @OpenId and Fo_Status=0 and r.Is_Delete=0";
            SqlParameter[] parames = { new SqlParameter("@OpenId", SqlDbType.NVarChar) };
            parames[0].Value = openId;
            List<WechatServiceBL> list = null;
            DataSet ds = SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, parames);
            if (ds != null && ds.Tables.Count > 0)
            {
                list = ConvertDataTable(ds.Tables[0]);
                if (list != null && list.Count > 0)
                {
                    return list[0];
                }
            }
            return null;
        }
        #endregion
        #endregion

        #region 更新


        #region 更新商户微信

        /// <summary>
        /// 更新商户微信
        /// </summary>
        /// <param name="AccessTokenLastlyTime">Access_Token最后获取时间</param>
        /// <param name="AppId">第三方用户唯一凭证</param>
        /// <param name="AccessToken">Access_Token</param>
        /// <returns>返回true时，表示已存在；否则，表示不存在</returns>
        /// <remarks>added by jimmy,2014-7-10</remarks>
        public static bool UpdateMercWechat(DateTime AccessTokenLastlyTime, string AppId, string AccessToken)
        {
            string sql = @"UPDATE Tab_Wechat_Service
                           SET 
                              [Access_Token] = @Access_Token
                              ,[Access_Token_Lastly_Time] = @Access_Token_Lastly_Time
                              ,[Modify_Time] = GETDATE()

                         WHERE  AppId = @AppId  and Is_Delete = 0";

            int resultNum = SqlDbHelper.ExecuteNonQuery(Config.ConnString, CommandType.Text, sql, new SqlParameter[] { new SqlParameter("@Access_Token", AccessToken), new SqlParameter("@Access_Token_Lastly_Time", AccessTokenLastlyTime), new SqlParameter("@AppId", AppId) });
            return resultNum > 0;
        }

        /// <summary>
        /// 更新商户微信Token值
        /// </summary>
        /// <param name="AppId">第三方用户唯一凭证</param>
        /// <param name="AccessToken">Access_Token</param>
        /// <returns>返回true时，表示已存在；否则，表示不存在</returns>
        /// <remarks>added by jimmy,2014-8-8</remarks>
        public static bool UpdateAccessToken(string AppId, string AccessToken)
        {
            string sql = @"UPDATE Tab_Wechat_Service
                           SET 
                              [Access_Token] = @Access_Token
                              ,[Modify_Time] = GETDATE()

                         WHERE  AppId = @AppId  and Is_Delete = 0";

            int resultNum = SqlDbHelper.ExecuteNonQuery(Config.ConnString, CommandType.Text, sql, new SqlParameter[] { new SqlParameter("@Access_Token", AccessToken), new SqlParameter("@AppId", AppId) });
            return resultNum > 0;
        }
        #endregion

        #region 根据APPID更新微信服务号
        /// <summary>
        /// 根据APPID更新微信服务号
        /// </summary>
        /// <param name="appId">第三方用户唯一凭证</param>
        /// <param name="originalId">原始ID</param>
        /// <param name="secret">第三方用户唯一密钥</param>
        /// <param name="accessToken">accessToken</param>
        /// <param name="modifyUserId">修改人</param>
        /// <param name="access_TokenGetDate">accessToken最后获取时间</param>
        /// <returns>author：李晓乐 2014-11-19</returns>
        public static bool UpdateMercWechat(string appId, string originalId, string secret, string accessToken, decimal modifyUserId, DateTime access_TokenGetDate)
        {
            string sql = @"update Tab_Wechat_Service set Original_Id = @originalId,AppSecret=@secret,Access_Token=@accessToken,
                            Access_Token_Lastly_Time=@access_TokenGetDate,Modify_User_Id=@modifyUserId,Modify_Time=GETDATE() where AppId=@appId and Is_Delete = 0 ";
            int resultNum = SqlDbHelper.ExecuteNonQuery(Config.ConnString, CommandType.Text, sql, new SqlParameter[]
            {
                new SqlParameter("@appId", appId), new SqlParameter("@originalId", originalId),
                new SqlParameter("@secret", secret),  new SqlParameter("@accessToken", accessToken),
                new SqlParameter("@access_TokenGetDate", access_TokenGetDate),
                new SqlParameter("@modifyUserId", modifyUserId)
            });
            return resultNum > 0;
        }
        #endregion

        #region 根据微信服务号ID更新微信服务号
        /// <summary>
        /// 根据微信服务号ID更新微信服务号
        /// </summary>
        /// <param name="serviceId">微信服务号ID</param>
        /// <param name="appId">第三方用户唯一凭证</param>
        /// <param name="originalId">原始ID</param>
        /// <param name="secret">第三方用户唯一密钥</param>
        /// <param name="accessToken">accessToken</param>
        /// <param name="modifyUserId">修改人</param>
        /// <param name="access_TokenGetDate">accessToken最后获取时间</param>
        /// <returns>author：李晓乐 2014-11-20</returns>
        public static bool UpdateMercWechat(decimal serviceId, string appId, string originalId, string secret, string accessToken, decimal modifyUserId, DateTime access_TokenGetDate)
        {
            string sql = @"update Tab_Wechat_Service set  AppId=@appId,Original_Id = @originalId,AppSecret=@secret,Access_Token=@accessToken,
                            Access_Token_Lastly_Time=@access_TokenGetDate,Modify_User_Id=@modifyUserId,Modify_Time=GETDATE() 
                            where Service_Id=@serviceId and Is_Delete = 0 ";
            int resultNum = SqlDbHelper.ExecuteNonQuery(Config.ConnString, CommandType.Text, sql, new SqlParameter[]
            {
                new SqlParameter("@serviceId", serviceId), new SqlParameter("@appId", appId), new SqlParameter("@originalId", originalId),
                new SqlParameter("@secret", secret),  new SqlParameter("@accessToken", accessToken),
                new SqlParameter("@access_TokenGetDate", access_TokenGetDate),
                new SqlParameter("@modifyUserId", modifyUserId)
            });
            return resultNum > 0;
        }
        #endregion

        #region 初始化商户微信服务号场景ID

        /// <summary>
        /// 初始化商户微信服务号场景ID
        /// </summary>
        /// <param name="serviceId">微信服务号ID</param>
        /// <returns>返回true时，表示更新成功；反之，表示更新失败</returns>
        /// <returns>author：李晓乐 2014-11-20</returns>
        public static bool InitSceneId(decimal serviceId)
        {
            string sqlText = @"update Tab_Wechat_Service set Scene_Id=0,Scene_Temp_Id = 100000,Modify_Time=GETDATE() where Service_Id=@serviceId and Is_Delete=0";
            SqlParameter[] param =
            {
                new SqlParameter("@serviceId", serviceId)
            };
            return SqlDbHelper.ExecuteNonQuery(Config.ConnString, CommandType.Text, sqlText, param) > 0;
        }

        #endregion

        #region 删除微信服务号

        /// <summary>
        /// 删除微信服务号
        /// </summary>
        /// <param name="serviceId">微信服务号ID</param>
        /// <returns>返回true时，表示更新成功；反之，表示更新失败</returns>
        /// <returns>author：李晓乐 2014-11-20</returns>
        public static bool DeleteSercice(decimal serviceId)
        {
            string sqlText = @"update Tab_Wechat_Service set Is_Delete=1,Modify_Time=GETDATE() where Service_Id=@serviceId and Is_Delete=0";
            SqlParameter[] param =
            {
                new SqlParameter("@serviceId", serviceId)
            };
            return SqlDbHelper.ExecuteNonQuery(Config.ConnString, CommandType.Text, sqlText, param) > 0;
        }

        #endregion

        #endregion

        #region 集合转换

        static List<WechatServiceBL> ConvertDataTable(DataTable dt)
        {
            List<WechatServiceBL> list = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                list = new List<WechatServiceBL>();
                WechatServiceBL bl;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    bl = new WechatServiceBL();

                    #region 取值

                    if (dt.Columns.Contains("Service_Id"))
                    {
                        bl.ServiceId = ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Service_Id"]);
                    }
                    if (dt.Columns.Contains("AppId"))
                    {
                        bl.AppId = ConvertObjectData.ConvertToString(dt.Rows[i]["AppId"]);
                    }
                    if (dt.Columns.Contains("Original_Id"))
                    {
                        bl.OriginalId = ConvertObjectData.ConvertToString(dt.Rows[i]["Original_Id"]);
                    }
                    if (dt.Columns.Contains("AppSecret"))
                    {
                        bl.AppSecret = ConvertObjectData.ConvertToString(dt.Rows[i]["AppSecret"]);
                    }
                    if (dt.Columns.Contains("Serv_Name"))
                    {
                        bl.ServName = ConvertObjectData.ConvertToString(dt.Rows[i]["Serv_Name"]);
                    }
                    if (dt.Columns.Contains("Wechat_Id"))
                    {
                        bl.WechatId = ConvertObjectData.ConvertToString(dt.Rows[i]["Wechat_Id"]);
                    }
                    if (dt.Columns.Contains("Wechat_Id_Type"))
                    {
                        bl.WechatIdType = (EServiceIdType)ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Wechat_Id_Type"]);
                    }
                    if (dt.Columns.Contains("Scene_Id"))
                    {
                        bl.SceneId = ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Scene_Id"]);
                    }
                    if (dt.Columns.Contains("Scene_Temp_Id"))
                    {
                        bl.SceneTempId = ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Scene_Temp_Id"]);
                    }
                    if (dt.Columns.Contains("Access_Token"))
                    {
                        bl.AccessToken = ConvertObjectData.ConvertToString(dt.Rows[i]["Access_Token"]);
                    }
                    if (dt.Columns.Contains("Access_Token_Lastly_Time"))
                    {
                        bl.AccessTokenLastlyTime = ConvertObjectData.ConvertToDateTime(dt.Rows[i]["Access_Token_Lastly_Time"]);
                    }
                    if (dt.Columns.Contains("Is_Delete"))
                    {
                        bl.IsDelete = ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Is_Delete"]);
                    }


                    if (dt.Columns.Contains("Create_Time"))
                    {
                        bl.CreateTime = ConvertObjectData.ConvertToDateTime(dt.Rows[i]["Create_Time"]);
                    }
                    if (dt.Columns.Contains("Modify_Time"))
                    {
                        bl.ModifyTime = ConvertObjectData.ConvertToDateTimeNull(dt.Rows[i]["Modify_Time"]);
                    }
                    if (dt.Columns.Contains("Create_User_Id"))
                    {
                        bl.CreateUserId = ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Create_User_Id"]);
                    }
                    if (dt.Columns.Contains("Modify_User_Id"))
                    {
                        bl.ModifyUserId = ConvertObjectData.ConvertToNullOrDecimal(dt.Rows[i]["Modify_User_Id"]);
                    }
                    list.Add(bl);

                    #endregion
                }
            }
            return list;
        }

        #endregion
    }
}
