using AutoMapper; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WMS.Web.Core; using WMS.Web.Core.Dto; using WMS.Web.Domain.Entitys; using WMS.Web.Domain.Infrastructure; using WMS.Web.Domain.IService.Public; using WMS.Web.Domain.Values; using WMS.Web.Domain.Values.Single; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { public class FileDownManagerRepositories : IFileDownManagerRepositories { private readonly IMapper _mapper; private readonly RepositoryDbContext _context; private readonly ISingleDataService _singleDataService; public FileDownManagerRepositories(RepositoryDbContext context, IMapper mapper, ISingleDataService singleDataService) { _mapper = mapper; _context = context; _singleDataService = singleDataService; } public async Task Add(FileDownManager entity) { await _context.FileDownManager.AddAsync(entity); await _context.SaveChangesAsync(); return entity; } public async Task Edit(FileDownManager entity) { var res = await _context.FileDownManager .FirstOrDefaultAsync(f => f.Id == entity.Id); if (res == null) return null; _mapper.Map(entity, res); await _context.SaveChangesAsync(); return res; } public async Task GetList(FileDownManagerRequest dto, int companyId) { List userIds = new List(); if (!string.IsNullOrEmpty(dto.User)) userIds = _singleDataService.GetIdsBySingleName(SingleAction.Staffs, companyId, dto.User); var res = _context.FileDownManager //.GroupJoin(_context.UcStaff, manager => manager.UserId, user => user.Id, (manager, user) => new { manager, user }) // .SelectMany(x => x.user.DefaultIfEmpty(), (d, user) => new { d.manager, user }) .OrderByDescending(o => o.Date) .Where(w => w.CompanyId == companyId); #region 条件 if (!string.IsNullOrEmpty(dto.User)) res = res.Where(w => userIds.Contains(w.UserId)); if (dto.BeginDate != null) res = res.Where(w => w.Date >= dto.BeginDate); if (dto.EndDate != null) res = res.Where(w => w.Date <= (dto.EndDate ?? DateTime.Now).AddHours(23).AddMinutes(59).AddSeconds(59)); if (dto.Type != null) res = res.Where(w => w.Type == (FileDownLoadOrderType)dto.Type); if (dto.SupplierId != null) res = res.Where(w => w.SupplierId == dto.SupplierId); if (dto.Status != null) { if ((ExportStatus)dto.Status == ExportStatus.Ing) res = res.Where(w => w.Status == ExportStatus.Ing && w.Date >= DateTime.Now.AddHours(-1)); else if ((ExportStatus)dto.Status == ExportStatus.Fail) res = res.Where(w => (w.Status == ExportStatus.Fail) || (w.Status == ExportStatus.Ing && w.Date < DateTime.Now.AddHours(-1))); else res = res.Where(w => w.Status == (ExportStatus)dto.Status); } #endregion int total = await res.CountAsync(); var list = await res.Select(s => new FileDownInfoManagerResponse(_mapper.Map(s)) { StatusKey = (s.Status == ExportStatus.Ing && s.Date < DateTime.Now.AddHours(-1)) ? (int)ExportStatus.Fail : (int)s.Status, Status = (s.Status == ExportStatus.Ing && s.Date < DateTime.Now.AddHours(-1)) ? ExportStatus.Fail.GetRemark() : s.Status.GetRemark(), UserName = dto.SupplierId != null ? _singleDataService.GetSingleData(SingleAction.Users, companyId, s.UserId) : _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.UserId),//s.StaffName }) .Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize) .ToListAsync(); return new FileDownManagerResponse(list, total); } } }