88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
using AutoMapper;
|
|
using BarCode.Web.Domain.Entitys;
|
|
using BarCode.Web.Domain.Infrastructure;
|
|
using BarCode.Web.Domain.Values;
|
|
using BarCode.Web.Repositories.Configuration;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BarCode.Web.Repositories
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class CenerateDataRepositories : ICenerateDataRepositories
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly RepositoryDbContext _context;
|
|
|
|
public CenerateDataRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider)
|
|
{
|
|
_context = context;
|
|
_mapper = mapper;
|
|
_serviceProvider = serviceProvider;
|
|
}
|
|
/// <summary>
|
|
/// 添加
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<CenerateData> Add(CenerateData entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
await _context.CenerateData.AddAsync(entity);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return entity;
|
|
}
|
|
/// <summary>
|
|
/// 修改
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<CenerateData> Edit(CenerateData entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
var res = await _context.CenerateData
|
|
.FirstOrDefaultAsync(f => f.Id == entity.Id);
|
|
if (res == null) return null;
|
|
|
|
_mapper.Map(entity, res);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
|
|
return res;
|
|
}
|
|
|
|
public async Task<CenerateData?> Get(CenerateDataType type)
|
|
{
|
|
var entity = await _context.CenerateData
|
|
.FirstOrDefaultAsync(f => f.Type == type);
|
|
if (entity == null)
|
|
{
|
|
entity = await this.Add(new CenerateData(type));
|
|
return entity;
|
|
}
|
|
return entity;
|
|
}
|
|
}
|
|
}
|