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 > 0)
{
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; }
}