mongo数据库的基本操作

using EnYuan.DEV.Mongo;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using pq.yuan.cn.Models.CRM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yuan.Data;

namespace pq.yuan.cn.Dao
{
    public class CrmLinkmanDao : MongoDao<linkmanModel>
    {

        //数据库链接配置
        protected override string ConfigName
        {
            get { return "****"; }
        }
        //数据库名称
        protected override string DatabaseName
        {
            get { return "****"; }
        }
        //表名称
        protected override string CollectionName
        {
            get { return "linkman"; }
        }

        protected override void Initialize()
        {
        }

        private static CrmLinkmanDao instance = new CrmLinkmanDao();

        public static CrmLinkmanDao Instance
        {
            get { return instance; }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Create(linkmanModel model)
        {
            return this.GetCollection().Save(model).Ok;
        }
        /// <summary>
        /// 获取联系人对象
        /// </summary>
        /// <param name="lid"></param>
        /// <returns></returns>
        public linkmanModel GetModel(string lid)
        {
            var query = Query<linkmanModel>.EQ(q => q.id, lid);
            return this.GetCollection().FindOne(query);

        }

        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateModel(linkmanModel model)
        {
            var one = this.GetCollection().FindOne(Query<linkmanModel>.EQ(q => q.id, model.id));
            if (one == null)
                return false;
            if (!string.IsNullOrEmpty(model.Name))
                one.Name = model.Name;
            if (!string.IsNullOrEmpty(model.isFrist))
                one.isFrist = model.isFrist;
            if (!string.IsNullOrEmpty(model.Sex))
                one.Sex = model.Sex;  
            if (!string.IsNullOrEmpty(model.Remark))
                one.Remark = model.Remark;
            return this.GetCollection().Save(one).Ok;
        }

        /// 重复
        /// </summary> 
        /// <param name="ClientName">客户名称</param>
        /// <returns>true重复</returns>
        public bool RepeatName(string Name, string userId)
        {
            var list_query = new List<IMongoQuery>();
            list_query.Add(Query<linkmanModel>.EQ(q => q.Name, Name));
            list_query.Add(Query<linkmanModel>.EQ(q => q.UserId, userId));
            var mangoQuery = Query.And(list_query);
            if (this.GetCollection().Find(mangoQuery).ToList().Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 联系人列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<linkmanModel> List(string userID)
        {
            var query2 = Query<linkmanModel>.EQ(q => q.UserId, userID);
            var result = this.GetCollection().Find(query2).SetSortOrder(SortBy<linkmanModel>.Descending(m => m.CreatTime));
            return result.ToList();
        }
        /// <summary>
        /// 联系人列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<linkmanModel> List(string userID, string cid)
        {
            var queries = new List<IMongoQuery>();
            queries.Add(Query<linkmanModel>.EQ(q => q.UserId, userID));
            queries.Add(Query<linkmanModel>.EQ(q => q.CId, cid));
            var result = this.GetCollection().Find(Query.And(queries)).SetSortOrder(SortBy<linkmanModel>.Descending(m => m.CreatTime));
            return result.ToList();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="count"></param>
        /// <param name="query"></param>
        /// <param name="skip"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IList<linkmanModel> Search(out int count, CrmLinkmanQuery query, int skip = 0, int limit = 50)
        {
            var query2 = toQuery(query);
            var result = this.GetCollection().Find(query2).SetSortOrder(SortBy<linkmanModel>.Descending(m => m.CreatTime)).SetSkip(skip).SetLimit(limit);
            count = (int)result.Count();
            return result.ToList();
        }
        /// <summary>
        /// 条件转换
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public IMongoQuery toQuery(CrmLinkmanQuery query)
        {
            var queries = new List<IMongoQuery>();
            if (!query.owner.Equals("54d3310287508d2064ade5ae"))
            {
                queries.Add(Query<linkmanModel>.EQ(q => q.UserId, query.owner));
            }
            // 按用户姓名
            if (!string.IsNullOrWhiteSpace(query.keyUserName))
            {
                queries.Add(Query<linkmanModel>.EQ(q => q.UserName, query.keyUserName));
            }
            // 按公司
            if (!string.IsNullOrWhiteSpace(query.keyCoName))
            {
                queries.Add(Query.Or(Query<clientModel>.Matches(m => m.CompanyName, query.keyCoName)));
                //queries.Add(Query<clientModel>.EQ(q => q.CompanyName, query.keyUserName));
            }
            // 添加关键字检索
            if (!string.IsNullOrWhiteSpace(query.searchkeyword))
            {
                queries.Add(Query.Or(Query<linkmanModel>.Matches(m => m.Name, query.searchkeyword)));
            }
            if (!string.IsNullOrWhiteSpace(query.cid))
            {
                queries.Add(Query<linkmanModel>.EQ(q => q.CId, query.cid));
            }
            if (queries.Count == 0)
                return null;

            return Query.And(queries);
        }
        
    }

}

你可能感兴趣的:(数据库,mongo)