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 { /// /// 订阅通知 /// 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; } /// /// 增加 /// /// /// /// public async Task 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; } } /// /// 编辑 /// /// /// /// public async Task 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; } } /// /// 批量编辑 /// /// /// /// public async Task EditList(List entitys, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { List 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 Get(int id) { return await _context.SubscribeNotification .AsNoTracking() .FirstOrDefaultAsync(f => f.Id == id); } public async Task> GetList(List ids = null) { if (ids == null) { return await _context.SubscribeNotification .AsNoTracking().ToListAsync(); } else { return await _context.SubscribeNotification .AsNoTracking() .Where(w => ids.Contains(w.Id)) .ToListAsync(); } } /// /// 列表 /// /// /// /// public async Task<(List list, int total)> GetListAsync(SubscribeNotificationQueryRequest dto, int companyId = 0) { if (companyId == 0) companyId = _loginRepositories.CompanyId; List ids = new List(); 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.Id) .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.OperateBeginDate != null) query = query.Where(w => w.OperateTime >= dto.OperateBeginDate); if (dto.OperateEndDate != null) query = query.Where(w => w.OperateTime <= dto.OperateEndDate); //组装 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 = s.CustomerName, Emails = s.CustomerName, 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); } /// /// 验重 /// /// /// /// public async Task IsExist(string name, string number) { var res = await _context.SubscribeNotification .FirstOrDefaultAsync(f => f.CustomerName.Equals(name) && f.CustomerNumber.Equals(number)); if (res == null) return false; return true; } } }