using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using TouchSocket.Core; using WCS.BLL.Config; using WCS.BLL.DbModels; using WCS.BLL.Services.IService; using WCS.DAL.Db; using WCS.DAL.DbModels; using WCS.Model; using WCS.Model.ApiModel; using WCS.Model.ApiModel.MatInventoryDetail; using WCS.Model.ApiModel.User; namespace WCS.BLL.Services.Service { public class MatInventoryDetailService : IMatInventoryDetailService { public async Task> getMatInventoryDetail(GetMatInventoryDetailRequest request) { try { var recordsQueryable = DbHelp.db.Queryable() .LeftJoin((id, si) => id.StoreId == si.Id) .WhereIF(!string.IsNullOrEmpty(request.MatSN), (id, si) => id.MatSN.Contains(request.MatSN)) .WhereIF(!string.IsNullOrEmpty(request.MatCode), (id, si) => id.MatCode.Contains(request.MatCode)) .WhereIF(!string.IsNullOrEmpty(request.MatName), (id, si) => id.MatName.Contains(request.MatName)) .WhereIF(!string.IsNullOrEmpty(request.MatBatch), (id, si) => id.MatBatch.Contains(request.MatBatch)) .WhereIF(!string.IsNullOrEmpty(request.MatSpec), (id, si) => id.MatSpec.Contains(request.MatSpec)) .WhereIF(!string.IsNullOrEmpty(request.MatSupplier), (id, si) => id.MatSpec.Contains(request.MatSupplier)) .WhereIF(!string.IsNullOrEmpty(request.MatCustomer), (id, si) => id.MatSpec.Contains(request.MatCustomer)) .WhereIF(request.StoreId != 0, (id, si) => id.StoreId == request.StoreId) .WhereIF(!string.IsNullOrEmpty(request.StoreCode), (id, si) => id.StoreCode.Contains(request.StoreCode)) .WhereIF(!string.IsNullOrEmpty(request.ShelfCode), (id, si) => si.ShelfCode.Contains(request.ShelfCode)) ; var totalCount = await recordsQueryable.CountAsync(); var records = await recordsQueryable .Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize) .Select() .ToListAsync(); //生成序号 for (int i = 0; i < records.Count; i++) { records[i].RowNumber = (request.PageNumber - 1) * request.PageSize + i + 1; } return new PageQueryResponse() { Code = 200, Message = $"success", Data = new PageQueryResponseData() { TotalCount = totalCount, MaxPage = request.PageSize == 0 ? 0 : (int)Math.Ceiling((decimal)totalCount / request.PageSize), Count = records.Count, Lists = records.ToList() } }; } catch (Exception ex) { return new PageQueryResponse() { Code = 300, Message = $"操作失败:{ex.Message}", }; } } public async Task> exportMatInventoryDetail(GetMatInventoryDetailRequest request) { try { var recordsQueryable = DbHelp.db.Queryable() .LeftJoin((id, si) => id.StoreId == si.Id) .WhereIF(!string.IsNullOrEmpty(request.MatSN), (id, si) => id.MatSN.Contains(request.MatSN)) .WhereIF(!string.IsNullOrEmpty(request.MatCode), (id, si) => id.MatCode.Contains(request.MatCode)) .WhereIF(!string.IsNullOrEmpty(request.MatName), (id, si) => id.MatName.Contains(request.MatName)) .WhereIF(!string.IsNullOrEmpty(request.MatBatch), (id, si) => id.MatBatch.Contains(request.MatBatch)) .WhereIF(!string.IsNullOrEmpty(request.MatSpec), (id, si) => id.MatSpec.Contains(request.MatSpec)) .WhereIF(!string.IsNullOrEmpty(request.MatSupplier), (id, si) => id.MatSpec.Contains(request.MatSupplier)) .WhereIF(!string.IsNullOrEmpty(request.MatCustomer), (id, si) => id.MatSpec.Contains(request.MatCustomer)) .WhereIF(request.StoreId != 0, (id, si) => id.StoreId == request.StoreId) .WhereIF(!string.IsNullOrEmpty(request.StoreCode), (id, si) => id.StoreCode.Contains(request.StoreCode)) .WhereIF(!string.IsNullOrEmpty(request.ShelfCode), (id, si) => si.ShelfCode.Contains(request.ShelfCode)) ; var records = await recordsQueryable .Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize) .Select() .ToListAsync(); //生成序号 var index = 1; records.ForEach(r => { r.RowNumber = index++; }); return new PageQueryResponse() { Code = 200, Message = $"success", Data = new PageQueryResponseData() { Lists = records } }; } catch (Exception ex) { return new PageQueryResponse() { Code = 300, Message = $"操作失败:{ex.Message}", }; } } public async Task>> getMatInventorySummary(GetMatInventorySummaryRequest request) { var inventortyDetails = await DbHelp.db.Queryable() .WhereIF(!string.IsNullOrEmpty(request.MatName), t => t.MatName.Contains(request.MatName)) .WhereIF(!string.IsNullOrEmpty(request.MatCode), t => t.MatCode.Contains(request.MatCode)) .WhereIF(!string.IsNullOrEmpty(request.MatBatch), t => t.MatBatch.Contains(request.MatBatch)) .Where(t => t.StoreInfo.ShelfTypeId == request.ShelfTypeId) .GroupBy(t => t.MatCode) .GroupBy(t => t.MatBatch) .Select(t => new MatInventorySummaryModel { MatCode = t.MatCode, MatName = t.MatName, MatSpec = t.MatSpec, MatBatch = t.MatBatch, TotalQty = SqlFunc.AggregateSum(t.MatQty) }) .ToListAsync(); return new ResponseCommon>() { Code = 200, Message = inventortyDetails.Count().ToString(), Data = inventortyDetails }; } public async Task> compareMatInventoryDetail(CompareMatInventoryDetailRequest request) { try { //查询 本分组中 是否含有所传SN的物料 var recordsQueryable = DbHelp.db.Queryable() .Where(t => request.MatSns.Contains(t.MatSN)) .OrderBy(t => t.MatCode) .WhereIF(!string.IsNullOrEmpty(LocalFile.Config.GroupName), t => t.StoreInfo.GroupName == LocalFile.Config.GroupName); var totalCount = await recordsQueryable.CountAsync(); var records = await recordsQueryable.ToListAsync(); //生成序号 选中 for (int i = 0; i < records.Count; i++) { records[i].RowNumber = i + 1; records[i].IsSelected = true; } return new PageQueryResponse() { Code = 200, Message = $"success", Data = new PageQueryResponseData() { TotalCount = totalCount, Count = records.Count, Lists = records.ToList() } }; } catch { return new PageQueryResponse() { Code = 200, Message = $"success", Data = new PageQueryResponseData() { Lists = new List() } }; } } } }