using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using WMS.Web.Domain.Infrastructure;
using WMS.Web.Domain.Values;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
///
/// 定时任务最新一次时间管理
///
public class ErpOpsSyncDateRepositories : IErpOpsSyncDateRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly RepositoryDbContext _context;
public ErpOpsSyncDateRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
}
///
/// 定时任务执行后更新时间
///
///
///
///
public async Task Edit(ErpOpsSyncType type, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var res = await _context.ErpOpsSyncDate
.FirstOrDefaultAsync(f => f.Type == type);
if (res == null) return false;
res.SyncTime = DateTime.Now;
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
return true;
}
catch (Exception)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
///
/// 获取最新一次更新时间
///
///
///
public async Task Get(ErpOpsSyncType type)
{
var res = await _context.ErpOpsSyncDate
.FirstOrDefaultAsync(f => f.Type == type);
return res == null ? DateTime.Now : res.SyncTime;
}
}
}