using DClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace CouchBaseDAL
{
    public class CsCouchBaseHelper
    {


        static Repository myRepository = DClientHelper.GetRepository(ConfigurationManager.AppSettings["CouchBaseBucketName"], ConfigurationManager.AppSettings["db_adminName"], ConfigurationManager.AppSettings["db_adminPassword"]);
        static string id;
        static string msg;
        /// <summary>
        /// 增加
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="doc">实体</param>
        /// <returns></returns>
        public static bool Add<T>(T doc) where T : class
        {
            return myRepository.Add(doc, out id, out msg);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键ID</param>
        /// <param name="doc">实体</param>
        /// <returns></returns>
        public static bool Update<T>(string id, T doc) where T : class
        {
            return myRepository.Update(id, doc, out msg);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public static bool Delete(string id)
        {
            return myRepository.Delete(id, out msg);
        }
        /// <summary>
        /// 取到一个唯一的实体
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public static T Get<T>(string id) where T : class
        {
            return myRepository.Get<T>(id, out msg);
        }
        /// <summary>
        /// 查询1参数
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="fieldName">参数名</param>
        /// <param name="fieldValueFrom">参数数值范围</param>
        /// <param name="fieldValueTo">参数数值范围</param>
        /// <param name="limitNum">取出数据的条数</param>
        /// <param name="skipNum">跳过多少条</param>
        /// <returns></returns>
        public static List<T> Query<T>(string fieldName, object fieldValueFrom, object fieldValueTo, int limitNum = 0, int skipNum = 0) where T : class
        {
            return myRepository.Query<T>(fieldName, fieldValueFrom, fieldValueTo, out msg, StaleSetting.State.False, limitNum, skipNum).Select<QueryResult<T>, T>(d => d.value).ToList();
        }
        /// <summary>
        /// 查询记录数1参数
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="fieldName">参数</param>
        /// <param name="fieldValueFrom">数值范围</param>
        /// <param name="fieldValueTo">数值范围</param>
        /// <param name="limitNum">取出数据的条数</param>
        /// <param name="skipNum">跳过多少条</param>
        /// <returns></returns>
        public static int QueryCount1<T>(string fieldName, object fieldValueFrom, object fieldValueTo, int limitNum = 0, int skipNum = 0) where T : class
        {
            int iCount = 0;
            IList<QueryResult<object>> myCount = myRepository.Query<T, object>(fieldName, fieldValueFrom, fieldValueTo, out msg, StaleSetting.State.False, limitNum, skipNum, false, true);
            if (myCount.Count > 0)
            {
                iCount = Convert.ToInt32(myCount[0].value);
            }
            return iCount;
        }
        /// <summary>
        /// 查询记录数2参数
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="field1Name">参数1</param>
        /// <param name="field1Value">参数1数据</param>
        /// <param name="field2Name">参数2</param>
        /// <param name="field2ValueFrom">参数2数值范围</param>
        /// <param name="field2ValueTo">参数2数值范围</param>
        /// <param name="limitNum">取出数据的条数</param>
        /// <param name="skipNum">跳过多少条</param>
        /// <returns></returns>
        public static int QueryCount2<T>(string field1Name, object field1Value, string field2Name, object field2ValueFrom, object field2ValueTo, int limitNum = 0, int skipNum = 0) where T : class
        {
            int iCount = 0;
            IList<QueryResult<object>> myCount = myRepository.Query<T, object>(field1Name, field1Value, field2Name, field2ValueFrom, field2ValueTo, out msg, StaleSetting.State.False, limitNum, skipNum, false, true);
            if (myCount.Count>0)
            {
                iCount = Convert.ToInt32(myCount[0].value);
            }
            return iCount;
        }


        /// <summary>
        /// 查询2参数
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="field1Name">参数1</param>
        /// <param name="field1Value">参数1数据</param>
        /// <param name="field2Name">参数2</param>
        /// <param name="field2ValueFrom">参数2数值范围</param>
        /// <param name="field2ValueTo">参数2数值范围</param>
        /// <param name="limitNum">取出数据的条数</param>
        /// <param name="skipNum">跳过多少条</param>
        /// <returns></returns>
        public static List<T> Query<T>(string field1Name, object field1Value, string field2Name, object field2ValueFrom, object field2ValueTo, int limitNum = 0, int skipNum = 0) where T : class
        {
            return myRepository.Query<T>(field1Name, field1Value, field2Name, field2ValueFrom, field2ValueTo, out msg, StaleSetting.State.False, limitNum, skipNum).Select<QueryResult<T>, T>(d => d.value).ToList();
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="limitNum">取出数据的条数</param>
        /// <param name="skipNum">跳过多少条</param>
        /// <returns></returns>
        public static List<T> GetAll<T>(int limitNum = 0, int skipNum = 0) where T : class
        {
            return myRepository.GetAll<T>(out msg, StaleSetting.State.False, limitNum, skipNum).Select<QueryResult<T>, T>(d => d.value).ToList();
        }


        /// <summary>
        /// 查询所有数据条数
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="limitNum">取出数据的条数</param>
        /// <param name="skipNum">跳过多少条</param>
        /// <returns></returns>
        public static int GetAllCount<T>(int limitNum = 0, int skipNum = 0) where T : class
        {
            int iCount = 0;
            IList<QueryResult<object>> myCount = myRepository.GetAll<T, object>(out msg, StaleSetting.State.False, limitNum, skipNum, false, true);
            if (myCount.Count > 0)
            {
                iCount = Convert.ToInt32(myCount[0].value);
            }
            return iCount;
        }


        /// <summary>
        /// N1ql查询
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="sN1ql">N1ql语句</param>
        /// <returns></returns>
        public static List<T> RunQuery<T>(string sN1ql) where T : class
        {
            return myRepository.RunQuery<T>(out msg, sN1ql);
        }


    }
}


posted on 2017-02-04 15:51  cxd1008  阅读(122)  评论(0编辑  收藏  举报