Files
WMS-Api/src/WMS.Web.Repositories/ErpOpsSyncDateRepositories.cs
2023-11-10 15:15:28 +08:00

76 lines
2.4 KiB
C#

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
{
/// <summary>
/// 定时任务最新一次时间管理
/// </summary>
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;
}
/// <summary>
/// 定时任务执行后更新时间
/// </summary>
/// <param name="type"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> 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;
}
}
/// <summary>
/// 获取最新一次更新时间
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public async Task<DateTime> Get(ErpOpsSyncType type)
{
var res = await _context.ErpOpsSyncDate
.FirstOrDefaultAsync(f => f.Type == type);
return res == null ? DateTime.Now : res.SyncTime;
}
}
}