using JiepeiWMS.IServices; using JiepeiWMS.Model.Models; using JiepeiWMS.Services.BASE; using JiepeiWMS.IRepository.Base; using System.Threading.Tasks; using JiepeiWMS.Model; using JiepeiWMS.Common.Enums; using JiepeiWMS.Common.Helper; using SqlSugar; using System.Linq.Expressions; using System; using System.Linq; using JiepeiWMS.Extends; using JiepeiWMS.IRepository; using JiepeiWMS.Common.HttpContextUser; namespace JiepeiWMS.Services { public class WMStockLogServices : BaseServices, IWMStockLogServices { private readonly IBaseRepository _dal; private readonly IWMProductListRepository _wMProductListRepository; private readonly IWMProductSpecRepository _wMProductSpecRepository; private readonly IWMPurchaseInfoRepository _wMPurchaseInfoRepository; private readonly IWMQuoteInfoRepository _wMQuoteInfoRepository; private readonly IWMSaleInfoRepository _wMSaleInfoRepository; private readonly IWMWareHouseRepository _wMWareHouseRepository; private readonly IUser _user; public WMStockLogServices(IBaseRepository dal, IWMProductListRepository WMProductListRepository, IWMProductSpecRepository WMProductSpecRepository, IWMPurchaseInfoRepository WMPurchaseInfoRepository, IWMQuoteInfoRepository WMQuoteInfoRepository, IWMSaleInfoRepository WMSaleInfoRepository, IWMWareHouseRepository WMWareHouseRepository, IUser user) { this._dal = dal; base.BaseDal = dal; _wMProductListRepository = WMProductListRepository; _wMProductSpecRepository = WMProductSpecRepository; _wMPurchaseInfoRepository = WMPurchaseInfoRepository; _wMQuoteInfoRepository = WMQuoteInfoRepository; _wMSaleInfoRepository = WMSaleInfoRepository; _wMWareHouseRepository = WMWareHouseRepository; _user = user; } /// /// 获取库存流水日志分页 /// /// public async Task> GetWMStockLogPage(int page, int intPageSize, string key = "", string typeoptions = "", string warehouseoptions = "", DateTime? startdate = null, DateTime? enddate = null) { Expression> whereExpression = StockLog => StockLog.Id > 0; if (!string.IsNullOrWhiteSpace(key)) { //筛选采购订单编号 var PurchaseCode = (await _wMPurchaseInfoRepository.Query(s => s.PurchaseCode.Contains(key))).Select(c => c.Id); //筛选报价订单编号 var QuoteCode = (await _wMQuoteInfoRepository.Query(s => s.QuoteCode.Contains(key))).Select(c => c.Id); //筛选领料订单编号 var SaleCode = (await _wMSaleInfoRepository.Query(s => s.SaleCode.Contains(key))).Select(c => c.Id); //筛选商品编号 var ProductCode = (await _wMProductListRepository.Query(s => s.Code.Contains(key))).Select(c => c.Id); //筛选商品名称 var ProductName = (await _wMProductListRepository.Query(s => s.Name.Contains(key))).Select(c => c.Id); //筛选商品规格 var ProductSpec = (await _wMProductSpecRepository.Query(s => s.Name.Contains(key))).Select(c => c.Id); whereExpression = whereExpression.And(StockLog => PurchaseCode.Contains(StockLog.OrderId.Value) || QuoteCode.Contains(StockLog.OrderId.Value) || SaleCode.Contains(StockLog.OrderId.Value) || ProductCode.Contains(StockLog.ProductListId) || ProductName.Contains(StockLog.ProductListId) || ProductSpec.Contains(StockLog.ProductSpecId)); } if (!string.IsNullOrWhiteSpace(typeoptions)) { whereExpression = whereExpression.And(StockLog => StockLog.Type == typeoptions.ObjToInt()); } if (!string.IsNullOrWhiteSpace(warehouseoptions)) { whereExpression = whereExpression.And(StockLog => StockLog.WareHouseId == warehouseoptions.ObjToInt()); } else { var WareHouseIdList = (await _wMWareHouseRepository.Query(x => x.SysOrgId == _user.SysOrgId)).Select(x => x.Id).ToList(); whereExpression = whereExpression.And(StockLog => WareHouseIdList.Contains(StockLog.WareHouseId)); } if (startdate != null) { whereExpression = whereExpression.And(StockLog => StockLog.CreateTime >= startdate); } if (enddate != null) { whereExpression = whereExpression.And(StockLog => StockLog.CreateTime <= enddate); } var PageList = await base.QueryTabsPage( (StockLog, ProductList, ProductSpec, WareHouse, UserInfo) => new object[] { JoinType.Left,StockLog.ProductListId==ProductList.Id, JoinType.Left,StockLog.ProductSpecId==ProductSpec.Id, JoinType.Left,StockLog.WareHouseId==WareHouse.Id, JoinType.Left,StockLog.AdminId==UserInfo.uID, }, (StockLog, ProductList, ProductSpec, WareHouse, UserInfo) => new WMStockLog() { Id = StockLog.Id, Code = ProductList.Code, ProductName = ProductList.Name, ProductSpec = ProductSpec.Name, WareHouseName = WareHouse.Name, Price = ProductList.Price, Type = StockLog.Type, TotalPrice = StockLog.TotalPrice, RemainQuantity = StockLog.RemainQuantity, RemainPrice = StockLog.RemainPrice, TotalRemainPrice = StockLog.TotalRemainPrice, QuantitySign = StockLog.Quantity > 0 ? ((StockLog.Operation == 1 ? "+" : "-") + StockLog.Quantity.ToString()) : "", OrderId = StockLog.OrderId, CreateTime = StockLog.CreateTime, AdminName = UserInfo.uRealName, }, whereExpression, page, intPageSize, " StockLog.Id desc" ); foreach (var item in PageList.data) { var dicType = typeof(EnumWMStockType)._GetValueDescriptionDicFromEnumType(); string name; item.TypeName = dicType.TryGetValue((EnumWMStockType)item.Type, out name) ? name : string.Empty; if (item.Type == 1) { var PurchaseInfoModel = await _wMPurchaseInfoRepository.QueryById(item.OrderId); if (PurchaseInfoModel != null) item.CodeNo = PurchaseInfoModel.PurchaseCode; } else if (item.Type == 2) { var SaleInfoModel = await _wMSaleInfoRepository.QueryById(item.OrderId); if (SaleInfoModel != null) item.CodeNo = SaleInfoModel.SaleCode; } else if (item.Type == 4) { var QuoteInfoModel = await _wMQuoteInfoRepository.QueryById(item.OrderId); if (QuoteInfoModel != null) item.CodeNo = QuoteInfoModel.QuoteCode; } } return PageList; } /// /// 获取最近几天得库存 /// /// 商品列表id /// 商品id /// 天数 /// 库存流水类型 /// public async Task GetStockNumByDateDay(int ProductListId, int Id, int Day, int Type) { var sql = string.Format(@" select isnull((SELECT SUM(Quantity) FROM dbo.WMStockLog WHERE ProductListId = {0} AND ProductId = {1} AND Type = {2} AND CreateTime >= CONVERT(VARCHAR(10),GETDATE() - {3},120)),0) ", ProductListId, Id, Type, Day); var dt_count = await BaseDal.QueryTable(sql.ToString()); return (decimal)dt_count.Rows[0][0]; } } }