heshaofeng
15 小时以前 673b5a596f611099eaacc310f6e7def0e022daca
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
113
114
115
116
117
118
119
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Stock;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StockService
{
    public class PlasticContainerLedgerService : ServiceBase<Dt_PlasticContainerLedger, IRepository<Dt_PlasticContainerLedger>>, IPlasticContainerLedgerService
    {
        private readonly IRepository<Dt_StockInfo> _stockRepository;
        public PlasticContainerLedgerService(IRepository<Dt_PlasticContainerLedger> BaseDal, IRepository<Dt_StockInfo> stockRepository) : base(BaseDal)
        {
            _stockRepository = stockRepository;
        }
 
        public IRepository<Dt_PlasticContainerLedger> Repository => BaseDal;
 
        public WebResponseContent GlueLineLedgerSummary(string supplyCode)
        {
           
            supplyCode = supplyCode?.Trim() ?? "";
            
            var now = DateTime.Now;
 
            
            var borrowGroup = BaseDal.Db.Queryable<Dt_PlasticContainerLedger>()
                .Where(x => x.SupplyCode == supplyCode)
                .GroupBy(x => new { x.SupplyCode, x.CreateDate.Date })
                .Select(x => new
                {
                    x.SupplyCode,
                    CreateDate = x.CreateDate.Date,
                    BorrowQty = SqlFunc.AggregateCount(x.Id)
                })
                .ToList();
 
            
            var returnedGroup = _stockRepository.Db.Queryable<Dt_StockInfo>()
                .Where(x => x.SupplyCode == supplyCode && !string.IsNullOrEmpty(x.Remark))
                .GroupBy(x => new { x.SupplyCode, x.BorrowTime.Date })
                .Select(x => new
                {
                    x.SupplyCode,
                    CreateDate = x.BorrowTime.Date,
                    ReturnedQty = SqlFunc.AggregateCount(x.Id)
                })
                .ToList();
 
            var result = new List<GlueLineLedgerSummaryDTO>();
 
            
            foreach (var item in borrowGroup)
            {
                result.Add(new GlueLineLedgerSummaryDTO
                {
                    SupplyCode = item.SupplyCode,
                    BorrowTime = item.CreateDate.ToString("yyyy-MM-dd"),
                    UnreturnedQuantity = item.BorrowQty,
                    ReturnedQty = 0,
                    PendingReturnQty = item.BorrowQty
                });
            }
 
            
            foreach (var item in returnedGroup)
            {
                result.Add(new GlueLineLedgerSummaryDTO
                {
                    SupplyCode = item.SupplyCode,
                    BorrowTime = item.CreateDate.ToString("yyyy-MM-dd"),
                    UnreturnedQuantity = item.ReturnedQty,
                    ReturnedQty = item.ReturnedQty,
                    PendingReturnQty = 0
                });
            }
 
            var filterResult = result
                .Where(x =>
                {
                    DateTime borrowTime = DateTime.Parse(x.BorrowTime);
 
                    if (x.PendingReturnQty > 0)
                        return true;
 
                    if (borrowTime >= now.AddMonths(-1))
                        return true;
 
                    return false;
                })
                .ToList();
 
            return WebResponseContent.Instance.OK(data: filterResult);
        }
 
 
        public class GlueLineLedgerSummaryDTO{
           public string SupplyCode { get; set; }
           
           public int UnreturnedQuantity { get; set; }
 
           public int ReturnedQty { get; set; }
           public int PendingReturnQty { get; set; }
           public string BorrowTime { get; set; }
           
         
        }
    }
}