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;
using WMS.Web.Core.Dto.Erp;
using WMS.Web.Domain.Entitys;
using WMS.Web.Domain.Infrastructure;
using WMS.Web.Domain.IService.Public;
using WMS.Web.Domain.Values.Single;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
///
/// 箱唛-仓储
///
public class BoxMarkRepositories : IBoxMarkRepositories
{
private readonly ILoginRepositories _loginRepositories;
private readonly RepositoryDbContext _context;
private readonly ISingleDataService _singleDataService;
private readonly IErpService _erpService;
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
private readonly IBasicsRepositories _basicsRepositories;
public BoxMarkRepositories(RepositoryDbContext context,
IErpService erpService,
ILoginRepositories loginRepositories,
ISingleDataService singleDataService,
IBasicsRepositories basicsRepositories,
IErpBasicDataExtendService erpBasicDataExtendService)
{
_context = context;
_erpService = erpService;
_basicsRepositories = basicsRepositories;
_loginRepositories = loginRepositories;
_singleDataService = singleDataService;
_erpBasicDataExtendService = erpBasicDataExtendService;
}
///
/// 列表-分页
///
///
///
public async Task<(List list, int total)> GetPagedList(BoxMarkQueryRequest dto, int companyId)
{
//1.获取物料集合和组织集合和供应商的集合
var materials = new List();
var materials_result = await _erpService.BillQueryForMaterial();
if (materials_result.IsSuccess)
materials = materials_result.Data.ToList();
//物料集合;模糊查询后的物料集合
if (!string.IsNullOrEmpty(dto.Material))
materials = materials.Where(w => w.MaterialNumber.Contains(dto.Material) || w.MaterialName.Contains(dto.Material) || w.Specifications.Contains(dto.Material)).ToList();
if (companyId == 0)
companyId = _loginRepositories.CompanyId;
List cr_ids = new List();
if (!string.IsNullOrEmpty(dto.Creator))
{
var staffList = await _basicsRepositories.GetStaffListAsync(companyId);
if (staffList != null)
cr_ids = staffList.Where(w => w.Name.Contains(dto.Creator)).Select(s => s.Id).ToList();
}
//var query = _context.BoxMark.Where(adv => 1 == 1);
var query = _context.BoxMarkBillNo
.GroupJoin(_context.BoxMark, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
.OrderByDescending(x => x.order.Id).ThenByDescending(x => x.detail.FirstBillNo).ThenByDescending(x => x.detail.LastBillNo)
.Where(adv => 1 == 1);
//编号查询
if (!string.IsNullOrEmpty(dto.BeginBillNo) &&
!string.IsNullOrEmpty(dto.EndBillNo))
{
if (dto.BeginBillNo.ToString().Length >= 13 && dto.EndBillNo.ToString().Length >= 13)
{
var begYMD = Convert.ToInt32(dto.BeginBillNo.Substring(2, 6));
var endYMD = Convert.ToInt32(dto.EndBillNo.Substring(2, 6));
//if (begYMD <= endYMD)
var begNo = Convert.ToInt32(dto.BeginBillNo.Substring(8));
var endNo = Convert.ToInt32(dto.EndBillNo.Substring(8));
query = query.Where(w => w.detail.FirstBillNo >= begYMD && w.detail.FirstBillNo <= endYMD && w.detail.LastBillNo >= begNo && w.detail.LastBillNo <= endNo);
}
else
query = query.Where(w => w.detail.BillNo == dto.BeginBillNo || w.detail.BillNo == dto.EndBillNo);
}
else if (!string.IsNullOrEmpty(dto.BeginBillNo))
query = query.Where(w => w.detail.BillNo == dto.BeginBillNo);
else if (!string.IsNullOrEmpty(dto.EndBillNo))
query = query.Where(w => w.detail.BillNo == dto.EndBillNo);
//订单号查询
if (!string.IsNullOrEmpty(dto.OrderBillNos))
{
var orderBNS = dto.OrderBillNos.Replace(",", ",");
var orderBillNoList = orderBNS.Split(",").Where(x => !string.IsNullOrEmpty(x)).ToList();
if (orderBillNoList != null && orderBillNoList.Count != 0)
{
query = query.Where(w => orderBillNoList.Contains(w.order.OrderBillNo));
}
}
//物料ID在模糊后的物料
if (!string.IsNullOrEmpty(dto.Material))
{
if (materials != null && materials.Count != 0)
{
var mids = materials.Select(x => x.MaterialId).ToList();
query = query.Where(w => mids.Contains(w.order.MaterialId));
}
else
query = query.Where(w => w.order.MaterialId == 0);
}
if (cr_ids.Count != 0)
query = query.Where(w => cr_ids.Contains(w.order.CreatorId));
else if (!string.IsNullOrEmpty(dto.Creator))
query = query.Where(w => w.order.CreatorId==0);
if (dto.BeginDate != null)
query = query.Where(w => w.order.CreateTime.Date >= dto.BeginDate.Value);
if (dto.EndDate != null)
query = query.Where(w => w.order.CreateTime.Date <= dto.EndDate.Value);
int total = await query.CountAsync();
var list = await query.Select(s => new BoxMarkQueryResponse()
{
Id = s.order.Id,
DetailId=s.detail.Id,
BillNo = s.detail.BillNo,
OrderBillNo = s.order.OrderBillNo,
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.order.MaterialId),
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.order.MaterialId),
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.order.MaterialId),
BarCode = _erpBasicDataExtendService.GetMaterialBarCode(materials, s.order.MaterialId),
CratingQty = s.order.CratingQty,
CratingNetWeightQty = s.order.CratingNetWeightQty,
CratingGrossWeightQty = s.order.CratingGrossWeightQty,
TailboxQty = s.order.TailboxQty,
TailboxNetWeightQty = s.order.TailboxNetWeightQty,
TailboxGrossWeightQty = s.order.TailboxGrossWeightQty,
IsTail=s.detail.IsTail,
Sort = s.detail.Sort,
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.order.CreatorId),
CreateTime = s.order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
Remark = s.order.Remark,
ProductQty=s.order.ProductQty,
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, total);
}
///
/// 新增
///
///
///
///
public async Task Add(BoxMark entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
await _context.BoxMark.AddAsync(entity);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
return entity;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
///
/// 详情-根据最新的ID
///
///
///
public async Task GetBy()
{
var entity = await _context.BoxMark.OrderByDescending(x => x.Id).FirstOrDefaultAsync();
return entity;
}
///
/// 列表-详情信息列表
///
///
///
///
public async Task> GetListInfoBy(int id, int companyId)
{
//1.获取物料集合和组织集合和供应商的集合
var materials = new List();
var materials_result = await _erpService.BillQueryForMaterial();
if (materials_result.IsSuccess)
materials = materials_result.Data.ToList();
var query = _context.BoxMarkBillNo
.GroupJoin(_context.BoxMark, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
.OrderByDescending(x => x.order.Id).ThenByDescending(x => x.detail.FirstBillNo).ThenByDescending(x => x.detail.LastBillNo)
.Where(adv => adv.order.Id == id);
var list = await query.Select(s => new BoxMarkQueryResponse()
{
Id = s.order.Id,
DetailId = s.detail.Id,
BillNo = s.detail.BillNo,
OrderBillNo = s.order.OrderBillNo,
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.order.MaterialId),
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.order.MaterialId),
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.order.MaterialId),
BarCode = _erpBasicDataExtendService.GetMaterialBarCode(materials, s.order.MaterialId),
CratingQty = s.order.CratingQty,
CratingNetWeightQty = s.order.CratingNetWeightQty,
CratingGrossWeightQty = s.order.CratingGrossWeightQty,
TailboxQty = s.order.TailboxQty,
TailboxNetWeightQty = s.order.TailboxNetWeightQty,
TailboxGrossWeightQty = s.order.TailboxGrossWeightQty,
IsTail = s.detail.IsTail,
Sort=s.detail.Sort,
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.order.CreatorId),
CreateTime = s.order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
Remark = s.order.Remark,
ProductQty = s.order.ProductQty,
}).ToListAsync();
return list;
}
///
/// 详情-根据最新的ID
///
///
///
public async Task GetLastBillNo()
{
var entity = await _context.BoxMarkBillNo.OrderByDescending(x => x.Id).FirstOrDefaultAsync();
return entity;
}
///
/// 批量删除
///
///
///
public async Task DeleteRange(List ids, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var list = await _context.BoxMark.Include(x=>x.BillNos).Where(f => ids.Contains(f.Id)).ToListAsync();
_context.BoxMark.RemoveRange(list);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
return true;
}
}
}