mongo repository
using MongoDB.Driver; namespace Dben.Invoice.Repository
{
/// <summary>
/// 仓储基类
/// </summary>
public abstract class BaseRepository
{
protected static readonly IMongoClient Client; protected static readonly IMongoDatabase Database; static BaseRepository()
{
//MongoClientSettings setting = new MongoClientSettings
//{
// MaxConnectionPoolSize = 500,
// WaitQueueSize = 2000,
// Server = new MongoServerAddress(DbSetting.ConnectionString)
//};
Client = new MongoClient(DbSetting.ConnectionString);
Database = Client.GetDatabase(DbSetting.InvoiceDataBase);
}
}
}
/*******************************************************
*
* 作者:朱皖苏
* 创建日期:20180528
* 说明:此文件只包含一个类,具体内容见类型注释。
* 运行环境:.NET 4.0
* 版本号:1.0.0
*
* 历史记录:
* 创建文件 朱皖苏 20180528 16:19
*
*******************************************************/ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dben.Invoice.Models;
using NLog;
using MongoDB.Driver;
using System.Linq;
using MongoDB.Bson; namespace Dben.Invoice.Repository
{
/// <summary>
/// 发票仓储
/// </summary>
public class InvoiceRepository : BaseRepository
{
/// <summary>
/// 过滤器
/// </summary>
public FilterDefinitionBuilder<Models.Invoice> filterBuilder = Builders<Models.Invoice>.Filter; /// <summary>
/// 更新器
/// </summary>
public UpdateDefinitionBuilder<Models.Invoice> updateBuilder = Builders<Models.Invoice>.Update; private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); /// <summary>
/// 大于开始 小于等于结束
/// </summary>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="source"></param>
/// <returns></returns>
internal long CountByDate(DateTime begin, DateTime end, string source)
{
var filter = filterBuilder.Where(m => m.Source == source && m.CreateTime > begin && m.CreateTime <= end);
return Database.GetCollection<Models.Invoice>("Invoice").Count(filter);
} internal long CountBySync(bool syncStatus)
{
var filter = filterBuilder.Where(m => m.SynStatus == syncStatus);
return Database.GetCollection<Models.Invoice>("Invoice").Count(filter);
} /// <summary>
/// 新增发票
/// </summary>
/// <param name="invoices"></param>
public async Task AddAsync(IEnumerable<Models.Invoice> invoices)
{
try
{
var collection = Database.GetCollection<Models.Invoice>("Invoice");
await collection.InsertManyAsync(invoices);
}
catch (Exception e)
{
logger.Error(e);
} } /// <summary>
/// 新增发票
/// </summary>
/// <param name="invoice"></param>
public async Task AddAsync(Models.Invoice invoice)
{
try
{
var collection = Database.GetCollection<Models.Invoice>("Invoice");
await collection.InsertOneAsync(invoice);
}
catch (Exception e)
{
logger.Error(e);
} }
/// <summary>
/// 保存发票文件
/// </summary>
/// <param name="invoiceFile"></param>
public async Task SaveInvoiceFileAsync(InvoiceFile invoiceFile)
{
try
{
var collection = Database.GetCollection<Models.InvoiceFile>("InvoiceFile");
await collection.InsertOneAsync(invoiceFile);
}
catch (Exception e)
{
logger.Error(e);
} } /// <summary>
/// 查询前top条未推送的数据。
/// </summary>
/// <param name="top"></param>
/// <returns></returns>
public async Task<List<Models.InvoiceTitle>> GetInvoices(int top)
{
try
{
var collection = Database.GetCollection<Models.InvoiceTitle>("Invoice");
var list = await collection.Find(x => x.SynStatus == false).Limit(top).ToListAsync(); if (list != null && list.Count > )
{
return list;
}
}
catch (Exception e)
{
logger.Error(e);
}
return null;
} /// <summary>
/// 批量更新推送状态
/// </summary>
/// <param name="invoices"></param>
/// <returns></returns>
public async Task UpdateInvoices(List<Models.InvoiceTitle> invoices)
{
try
{
List<ObjectId> ids = invoices.Select(a => a._id).ToList();
var collection = Database.GetCollection<Models.InvoiceTitle>("Invoice");
var filter = Builders<Models.InvoiceTitle>.Filter.In("_id", ids);
var update = Builders<Models.InvoiceTitle>.Update.Set("SynStatus", true);
await collection.UpdateManyAsync(filter, update);
}
catch (Exception e)
{
logger.Error(e);
}
} }
}
/// <summary>
/// 发票信息
/// </summary>
public class Invoice
{
/// <summary>
/// 购方名称
/// </summary>
public string PurchaserName { get; set; } /// <summary>
/// 购方税号
/// </summary>
public string PurchaserTaxNo { get; set; } /// <summary>
/// 购方开户行账户
/// </summary>
public string PurchaserBank { get; set; } /// <summary>
/// 购方地址电话
/// </summary>
public string PurchaserAddressPhone { get; set; } /// <summary>
/// 销方名称
/// </summary>
public string SalesName { get; set; } /// <summary>
/// 销方税号
/// </summary>
public string SalesTaxNo { get; set; } /// <summary>
/// 销方地址电话
/// </summary>
public string SalesAddressPhone { get; set; } /// <summary>
/// 销方开户行账户
/// </summary>
public string SalesBank { get; set; } ///// <summary>
///// 主键ID
///// </summary>
//public ObjectId Id { get; set; } /// <summary>
/// 地区编码
/// </summary>
public string AreaCode { get; private set; } /// <summary>
/// 地区名称
/// </summary>
public string AreaName { get; private set; } private string _invoiceCode; /// <summary>
/// 发票代码
/// </summary>
public string InvoiceCode
{
get { return _invoiceCode; }
set
{
_invoiceCode = value;
var area = InvoiceParser.ParseArea(value);
AreaCode = area.Item1;
AreaName = area.Item2;
}
} /// <summary>
/// 发票号码
/// </summary>
public string InvoiceNumber { get; set; } /// <summary>
/// 发票类型
/// 1=增值税专用发票 2=机动车销售统一发票 3=货物运输业增值税专用发票
/// 4=增值税普通发票 5=增值税电子发票 6=增值税普通发票(卷票)
/// </summary>
public InvoiceType InvoiceType { get; set; } /// <summary>
/// 发票状态
/// </summary>
public InvoiceStatus InvoiceStatus { get; set; } /// <summary>
/// 开票日期
/// </summary>
public DateTime BillingDate { get; set; } /// <summary>
/// 金额(价税合计)
/// </summary>
public double SumAmount { get; set; } /// <summary>
/// 未税金额
/// </summary>
public double Amount { get; set; } /// <summary>
/// 税额
/// </summary>
public double TaxAmount { get; set; } /// <summary>
/// 备注
/// </summary>
public string Remark { get; set; } /// <summary>
/// 发票来源 如 发票管家、发票查验平台
/// </summary>
public string Source { get; set; } /// <summary>
/// 发票来源的唯一标识
/// </summary>
public string SourceId { get; set; } /// <summary>
/// 采集时间
/// </summary>
public DateTime CreateTime { get; set; } /// <summary>
/// 收款人
/// </summary>
public string Payee { get; set; } /// <summary>
/// 复核人
/// </summary>
public string Checker { get; set; } /// <summary>
/// 开票人
/// </summary>
public string Drawer { get; set; } /// <summary>
/// 校验码
/// </summary>
public string CheckCode { get; set; } /// <summary>
/// 机器码
/// </summary>
public string MachineCode { get; set; } /// <summary>
/// 发票是否为真
/// </summary>
public bool IsTrue { get; set; } /// <summary>
/// 发票明细
/// </summary>
public IList<InvoiceItem> Items { get; set; } /// <summary>
/// 机动车发票信息
/// </summary>
public VehicleInfo VehicleInfo { get; set; } /// <summary>
/// 货物运输业发票信息
/// </summary>
public FreightTransport FreightTransport { get; set; } /// <summary>
/// 同步状态
/// </summary>
public bool SynStatus { get; set; }
}
mongo repository的更多相关文章
- Spring data mongodb 替换 Repository 实现类,findAll 排除 字段
因文档比较大,有时候findAll 不想返回所有数据.没有找到默认的findAll 能够include 或者 exclude 的方法,所以想办法扩展一下实现类 query.fields().inclu ...
- mongodb 速成笔记
以下环境为mac osx + jdk 1.8 + mongodb v3.2.3 一.安装 brew安装方式是mac下最简单的方式 brew update brew install mongodb 其它 ...
- Spring data mongodb 聚合,投射,内嵌数组文档分页.
尽量别直接用 DBObject ,Spring data mongodb 的api 本来就没什么多大用处,如果还直接用 DBObject 那么还需要自己去解析结果,说动做个对象映射,累不累 Spri ...
- .net core 2.2 & Mongodb
.net core 2.2 API项目中使用Mongodb 简单的CRUD封装 创建FoodPlan.Core 项目 创建IEntityBase.cs 接口约束 创建Single.cs 实体 IEnt ...
- linux记录-docker配置mysql
docker部署mysql 1.拉取镜像 docker pull mysql 2.docker rm containerID 删除镜像iD 3.创建镜像 docker run --name=m ...
- Repository 仓储,你的归宿究竟在哪?(一)-仓储的概念
写在前面 写这篇博文的灵感来自<如何开始DDD(完)>,很感谢young.han兄这几天的坚持,陆陆续续写了几篇有关于领域驱动设计的博文,让园中再次刮了一阵"DDD探讨风&quo ...
- [Mongo] How to Install Mongo on Debian(不要安装)
Install MongoDB on Debian¶ This tutorial outlines the steps to install MongoDB on Debian systems. Th ...
- spring-boot支持双数据源mysql+mongo
这里,首先想说的是,现在的web应用,处理的数据对象,有结构化的,也有非结构化的.同时存在.但是在spring-boot操作数据库的时候,若是在properties文件中配置数据源的信息,通过默认配置 ...
- 将 node.js 的数据保存到 mongo 数据库中
Mongo 数据库 安装 首先到 Mongo 的官方网站下载安装程序:http://www.mongodb.org/,我下载的文件名为:mongodb-win32-x86_64-2008plus-2. ...
随机推荐
- H3C 帧中继协议栈
- python基础七之集合
集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. 增加 set1 = {'zxc', 'zxf'} set1.add('zxv') # 无序添加 set1.update('zx ...
- Webstorm 配置 Less编译
配置less编译
- ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(11)之支付管理及广告管理
源码下载地址:http://www.yealuo.com/Sccnn/Detail?KeyValue=c891ffae-7441-4afb-9a75-c5fe000e3d1c 本项目属于个人项目,不支 ...
- vue-learning:38 - router - 前端路由的发展
前端路由的发展 参考博客 前端路由是什么东西? 什么是路由 在jQuery时代,我们使用<a href="https://www.example.com/example/home.ht ...
- url查找参数
function GetUrlParam(paraName) { var url = document.location.toString(); var arrObj = url.split(&quo ...
- Makefile记录
需要把sum.c编译汇编成可执行程序zzj zzj:sum.o gcc -o zzj sum.osum.o:sum.c gcc -c -o sum.o sum.cclean: rm -rf *.o z ...
- MFC下获取系统内存和当前进程的内存使用情况
GlobalMemoryStatusEx来获取系统的内存使用情况 GetProcessMemoryInfo获取某个进程的内存使用情况.和任务管理器看到的是一样的. 具体代码如下: void GetSy ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Windows Server Core Remote Manage Hyper-V
原帖:https://serverfault.com/questions/852144/how-do-i-remotely-manage-hyper-v-2016-standalone-via-win ...