using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Newtonsoft.Json;
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.Core.Dto.Erp;
using WMS.Web.Core.Dto.Erp.Org;
using WMS.Web.Core.Internal.Results;
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 BackRecordRepositories : IAllFielRepositories, IBackRecordRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly ILoginRepositories _loginRepositories;
private readonly IBasicsRepositories _basicsRepositories;
private readonly RepositoryDbContext _context;
private readonly IErpService _erpService;
private readonly ISingleDataService _singleDataService;
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
public BackRecordRepositories(RepositoryDbContext context,
IMapper mapper,
IErpService erpService,
IBasicsRepositories basicsRepositories,
ILoginRepositories loginRepositories,
IServiceProvider serviceProvider,
ISingleDataService singleDataService,
IErpBasicDataExtendService erpBasicDataExtendService)
{
_erpService = erpService;
_context = context;
_mapper = mapper;
_basicsRepositories = basicsRepositories;
_serviceProvider = serviceProvider;
_loginRepositories = loginRepositories;
_singleDataService = singleDataService;
_erpBasicDataExtendService = erpBasicDataExtendService;
}
///
/// 列表-分页
///
///
///
public async Task<(List list,int total)> GetPagedList(BackRecordQueryRequest dto, int companyId)
{
//1.获取物料集合和组织集合和供应商的集合
var materials = new List();
var materials_result = await _erpService.BillQueryForMaterial();
if (materials_result.IsSuccess)
materials = materials_result.Data.ToList();
//组织集合
var orgs = new List();
var orgs_result = await _erpService.BillQueryForOrg();
if (orgs_result.IsSuccess)
orgs = orgs_result.Data.ToList();
List ids = new List();
if (!string.IsNullOrEmpty(dto.Creator))
{
var staffList = await _basicsRepositories.GetStaffListAsync(companyId);
ids = staffList.Where(w =>w.Name.Contains(dto.Creator)).Select(s => s.Id).ToList();
}
var query = _context.BackRecordDetails
.GroupJoin(_context.BackRecord, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
.GroupJoin(_context.Box, p => p.detail.BoxId, ts => ts.Id, (p, ts) => new { p.detail, p.order, ts })
.SelectMany(x => x.ts.DefaultIfEmpty(), (p, box) => new { p.detail, p.order,box })
.Where(adv => 1 == 1);
if(!string.IsNullOrEmpty(dto.BoxBillNo))
query = query.Where(w => EF.Functions.Like(w.box.BoxBillNo, "%" + dto.BoxBillNo + "%"));
if (!string.IsNullOrEmpty(dto.SubStockCode))
query = query.Where(w => w.order.SubStockCode == dto.SubStockCode);
if (dto.Type.HasValue)
query = query.Where(w => (int)w.order.Type == dto.Type.Value);
if (ids.Count() > 0)
query = query.Where(w => ids.Contains(w.order.CreatorId));
if (dto.CreateBeginDate != null)
query = query.Where(w => w.order.CreateTime.Date >= dto.CreateBeginDate.Value);
if (dto.CreateEndDate != null)
query = query.Where(w => w.order.CreateTime.Date <= dto.CreateEndDate.Value);
int total = await query.CountAsync();
var list = await query.Select(s => new BackRecordQueryResponse()
{
Id = s.order.Id,
DetailsId = s.detail.Id,
BillNo=s.order.BillNo,
BoxBillNo = s.box.BoxBillNo,
Type = s.order.Type.GetRemark(),
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.order.CreatorId),
CreateTime =s.order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.detail.MaterialNumber),
MaterialNumber = s.detail.MaterialNumber,
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialNumber),
Qty = s.detail.Qty,
Org= _erpBasicDataExtendService.GetOrgName(orgs, s.order.OrgCode),
Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, companyId, s.order.StockCode + s.order.OrgCode),
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.order.SubStockCode + s.order.StockCode + s.order.OrgCode),
SerialNumbers = (string.Join(",", s.detail.SerialNumbers)).TrimEnd(','),
Remark=s.detail.Remark
}).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list,total);
}
///
/// 新增
///
///
///
///
public async Task Add(BackRecord entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
await _context.BackRecord.AddAsync(entity);
await _context.SaveChangesAsync();
if (string.IsNullOrEmpty(entity.BillNo))
{
entity.GenerateNo();
await _context.SaveChangesAsync();
}
if (_transaction != null)
_transaction.Commit();
return entity;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
///
/// 导出
///
///
///
///
public async Task<(object obj, int total)> GetListField(BackRecordQueryRequest dto, int companyId)
{
return await GetPagedList(dto,companyId);
}
public async Task Edit(BackRecord entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var res = await _context.BackRecord
.Include(s => s.Details)
.FirstOrDefaultAsync(f => f.Id == entity.Id);
if (res == null) return null;
_mapper.Map(entity, res);
_mapper.ToMapList(entity.Details, res.Details);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
return res;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
public async Task GetEntity(int id)
{
return await _context.BackRecord.AsNoTracking()
.Include(s => s.Details)
.Where(f => id == f.Id).FirstOrDefaultAsync();
}
}
}