heshaofeng
9 天以前 b6a40a2d8fdcffb3accfc7e424c0726a87a59ddf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using SqlSugar;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.HttpContextUser;
using WIDESEA_DTO.Stock;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StockService
{
    public partial class StockInfoDetailService : ServiceBase<Dt_StockInfoDetail, IRepository<Dt_StockInfoDetail>>, IStockInfoDetailService
    {
        public StockInfoDetailService(IRepository<Dt_StockInfoDetail> BaseDal, IRepository<Dt_StockInfo> stockinfoRepository) : base(BaseDal)
        {
            _stockinfoRepository = stockinfoRepository;
        }
 
        public IRepository<Dt_StockInfo> _stockinfoRepository;
        public IRepository<Dt_StockInfoDetail> Repository => BaseDal;
 
        public bool ExistBarcodes(string barcode)
        {
            return BaseDal.QueryFirst(x => x.Barcode == barcode) != null;
        }
 
        public bool ExistBarcodes(List<string> barcodes)
        {
            return BaseDal.QueryFirst(x => !string.IsNullOrEmpty(x.Barcode) && barcodes.Contains(x.Barcode)) != null;
        }
 
        public PageGridData<StockInfoDetailWithPalletDto> GetPageData2(PageDataOptions options)
        {
            PageGridData<Dt_StockInfoDetail> lists =  base.GetPageData (options);
 
            List<int> stockIds = lists.Rows.Select(detail => detail.StockId).Distinct().ToList();
            var stocks= _stockinfoRepository.QueryData(x => stockIds.Contains(x.Id)).ToList();
 
 
            List<StockInfoDetailWithPalletDto> dtoList = lists.Rows
                .Select(detail => new StockInfoDetailWithPalletDto
                {
                    
                    Id = detail.Id,
                    StockId = detail.StockId,
                    MaterielCode = detail.MaterielCode,
                    MaterielName = detail.MaterielName,
                    OrderNo = detail.OrderNo,
                    BatchNo = detail.BatchNo,
                    ProductionDate = detail.ProductionDate,
                    EffectiveDate = detail.EffectiveDate,
                    SerialNumber = detail.SerialNumber,
                    StockQuantity = detail.StockQuantity,
                    OutboundQuantity = detail.OutboundQuantity,
                    Status = detail.Status,
                    Unit = detail.Unit,
                    InboundOrderRowNo = detail.InboundOrderRowNo,
                    SupplyCode = detail.SupplyCode,
                    WarehouseCode = detail.WarehouseCode,
                    Barcode = detail.Barcode,
                    BusinessType = detail.BusinessType,
                    Remark = detail.Remark,
                    Creater = detail.Creater,
                    CreateDate = detail.CreateDate,
                    Modifier = detail.Modifier,
                    ModifyDate = detail.ModifyDate,
                    PalletCode= stocks
                        .FirstOrDefault(stock => stock.Id == detail.StockId)?
                        .PalletCode ?? "无托盘编号"  
                })
                .ToList();
 
            return new PageGridData<StockInfoDetailWithPalletDto> { Rows = dtoList, Total = lists.Total, Summary = lists.Summary  };
        }
 
        public PageGridData<StockDetailDtO> GetPageDataByMateriel(PageDataOptions options)
        {
            PageGridData<Dt_StockInfoDetail> lists = base.GetPageData(options);
 
            List<StockDetailDtO> dtoList = lists.Rows.GroupBy(detail => new { detail.MaterielCode, detail.BatchNo, detail.SupplyCode }).Select(group =>
            {
                var firstItem = group.First();
                return new StockDetailDtO
                {
                    Id = firstItem.Id,
                    StockId = firstItem.StockId,
                    MaterielCode = group.Key.MaterielCode, 
                    MaterielName = firstItem.MaterielName,
                    OrderNo = firstItem.OrderNo,
                    BatchNo = group.Key.BatchNo, 
                    ProductionDate = firstItem.ProductionDate,
                    EffectiveDate = firstItem.EffectiveDate,
                    SerialNumber = firstItem.SerialNumber,
                    // 核心:对当前分组的StockQuantity求和
                    StockQuantity = group.Sum(item => item.StockQuantity),
                    OutboundQuantity = firstItem.OutboundQuantity,
                    Status = firstItem.Status,
                    Unit = firstItem.Unit,
                    InboundOrderRowNo = firstItem.InboundOrderRowNo,
                    SupplyCode = group.Key.SupplyCode,
                    WarehouseCode = firstItem.WarehouseCode,
                    Barcode = firstItem.Barcode,
                    BusinessType = firstItem.BusinessType,
                    Remark = firstItem.Remark
                };
            }).ToList();
            
 
            return new PageGridData<StockDetailDtO> { Rows = dtoList, Total = lists.Total, Summary = lists.Summary };
        }
    }
 }