Files
WMS-Api/src/WMS.Web.Repositories/SubscribeNotificationRepositories.cs
18942506660 f34bb21828 修复bug
2024-04-13 10:31:43 +08:00

225 lines
8.5 KiB
C#

using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Web.Core.Dto.SubscribeNotification;
using WMS.Web.Core.Help;
using WMS.Web.Domain.Entitys;
using WMS.Web.Domain.Infrastructure;
using WMS.Web.Domain.IService.Public;
using WMS.Web.Domain.Mappers;
using WMS.Web.Domain.Values.Single;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
/// <summary>
/// 订阅通知
/// </summary>
public class SubscribeNotificationRepositories : ISubscribeNotificationRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly RepositoryDbContext _context;
private readonly ISingleDataService _singleDataService;
private readonly ILoginRepositories _loginRepositories;
private readonly IBasicsRepositories _basicsRepositories;
public SubscribeNotificationRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider,
ISingleDataService singleDataService, ILoginRepositories loginRepositories, IBasicsRepositories basicsRepositories)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_singleDataService = singleDataService;
_loginRepositories = loginRepositories;
_basicsRepositories = basicsRepositories;
}
/// <summary>
/// 增加
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<SubscribeNotification> Add(SubscribeNotification entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
await _context.SubscribeNotification.AddAsync(entity);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
return entity;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<SubscribeNotification> Edit(SubscribeNotification entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var res = await _context.SubscribeNotification
.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;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
/// <summary>
/// 批量编辑
/// </summary>
/// <param name="entitys"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> EditList(List<SubscribeNotification> entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
List<int> list = entitys.Select(s => s.Id).ToList();
var res = await _context.SubscribeNotification
.Where(f => list.Contains(f.Id)).ToListAsync();
_mapper.ToMapList(entitys, res);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
return true;
}
public async Task<SubscribeNotification> Get(int id)
{
return await _context.SubscribeNotification
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == id);
}
public async Task<List<SubscribeNotification>> GetList(List<int> ids = null)
{
if (ids == null)
{
return await _context.SubscribeNotification.Where(w => w.IsDelete != true)
.AsNoTracking().ToListAsync();
}
else
{
return await _context.SubscribeNotification
.AsNoTracking()
.Where(w => ids.Contains(w.Id) && w.IsDelete != true)
.ToListAsync();
}
}
/// <summary>
/// 列表
/// </summary>
/// <param name="dto"></param>
/// <param name="companyId"></param>
/// <returns></returns>
public async Task<(List<SubscribeNotificationQueryInfoResponse> list, int total)> GetListAsync(SubscribeNotificationQueryRequest dto, int companyId = 0)
{
if (companyId == 0)
companyId = _loginRepositories.CompanyId;
List<int> ids = new List<int>();
if (!string.IsNullOrEmpty(dto.Creator))
{
var staffList = await _basicsRepositories.GetStaffListAsync(companyId);
if (staffList != null)
ids = staffList.Where(w => w.Name.Contains(dto.Creator)).Select(s => s.Id).ToList();
}
var query = _context.SubscribeNotification
.OrderByDescending(o => o.OperateTime)
.Where(adv => 1 == 1 && adv.IsDelete != true);
if (!string.IsNullOrEmpty(dto.Creator))
query = query.Where(w => ids.Contains(w.CreatorId));
if (!string.IsNullOrEmpty(dto.CustomerName))
query = query.Where(w => EF.Functions.Like(w.CustomerName, "%" + dto.CustomerName + "%"));
if (!string.IsNullOrEmpty(dto.CustomerNumber))
query = query.Where(w => EF.Functions.Like(w.CustomerNumber, "%" + dto.CustomerNumber + "%"));
if (dto.CreateBeginDate != null)
query = query.Where(w => w.OperateTime >= dto.CreateBeginDate);
if (dto.CreateEndDate != null)
query = query.Where(w => w.OperateTime <= dto.CreateEndDate);
//组装
int total = await query.CountAsync();
var list = await query.Select(s => new SubscribeNotificationQueryInfoResponse()
{
#region dto组装
Id = s.Id,
CustomerName = s.CustomerName,
CustomerNumber = s.CustomerNumber,
Telephones = string.Join(",", s.Telephones),
Emails = string.Join(",", s.Emails),
Operate = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.OperateId),
OperateTime = s.OperateTime.DateToStringSeconds(),
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.CreatorId),
CreateTime = s.CreateTime.DateToStringSeconds()
#endregion
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, total);
}
/// <summary>
/// 验重
/// </summary>
/// <param name="name"></param>
/// <param name="number"></param>
/// <returns></returns>
public async Task<bool> IsExist(string name, string number)
{
var res = await _context.SubscribeNotification
.FirstOrDefaultAsync(f => f.CustomerName.Equals(name) && f.CustomerNumber.Equals(number) && f.IsDelete != true);
if (res == null) return false;
return true;
}
}
}