yanjinhui
10 天以前 b330b8ff1b5315684b25afb534f74044dea1654b
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using HslCommunication;
using MailKit.Search;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.SquareCabin;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
 
namespace WIDESEA_WMsInfoServices
{
    public class InventoryInfoService : ServiceBase<Dt_InventoryInfo, IRepository<Dt_InventoryInfo>>, IInventoryInfoService
    {
        public InventoryInfoService(IRepository<Dt_InventoryInfo> BaseDal) : base(BaseDal)
        {
        }
        public IRepository<Dt_InventoryInfo> Repository => BaseDal;
 
        public WebResponseContent GetExpiredAndlow()
        {
            try
            {
                // 获取当前时间(只取日期部分,忽略时间)
                DateTime currentDate = DateTime.Today;
                // 计算30天后的日期
                DateTime thresholdDate = currentDate.AddDays(30);
 
                // 查找库存中所有商品
                var inventoryList = BaseDal.QueryData();
                var expiredSoonList = new List<object>();
 
                foreach (var item in inventoryList)
                {
                    // 检查ValidityPeriod是否为空
                    if (string.IsNullOrEmpty(item.ValidityPeriod))
                        continue;
 
                    // 使用精确格式解析 "2037-10-02"
                    if (DateTime.TryParseExact(item.ValidityPeriod.Trim(), "yyyy-MM-dd",
                        System.Globalization.CultureInfo.InvariantCulture,
                        System.Globalization.DateTimeStyles.None, out DateTime expiryDate))
                    {
                        // 计算剩余天数(只比较日期部分)
                        double daysRemaining = (expiryDate - currentDate).TotalDays;
 
                        // 检查是否在30天内(包括已过期的)
                        if (daysRemaining <= 30)
                        {
                            expiredSoonList.Add(new
                            {
                                MaterielCode = item.MaterielCode ?? "",
                                BatchNo = item.BatchNo ?? "",
                                ValidityPeriod = item.ValidityPeriod,
                                ExpiryDate = expiryDate.ToString("yyyy-MM-dd"),
                            });
                        }
                    }
                    else
                    {
                        // 记录解析失败的记录(用于调试)
                        Console.WriteLine($"无法解析效期: {item.ValidityPeriod}");
                    }
                }
 
                // 按剩余天数升序排列(即将过期的排前面)
                var sortedList = expiredSoonList.OrderBy(x =>
                {
                    var days = (int)((dynamic)x).DaysRemaining;
                    return days;
                }).ToList();
 
                return new WebResponseContent
                {
                    Status = true,
                    Message = $"发现 {sortedList.Count} 个商品将在30天内过期",
                    Data = new
                    {
                        TotalCount = sortedList.Count,
                        ExpiredCount = sortedList.Count(x => ((dynamic)x).DaysRemaining < 0),
                        WarningCount = sortedList.Count(x => ((dynamic)x).DaysRemaining >= 0),
                        CheckDate = currentDate.ToString("yyyy-MM-dd"),
                        Items = sortedList
                    }
                };
            }
            catch (Exception ex)
            {
                return new WebResponseContent
                {
                    Status = false,
                    Message = $"获取效期数据失败: {ex.Message}"
                };
            }
        }
 
        /// <summary>
        /// 获取库存
        /// </summary>
        /// <returns></returns>
        public WebResponseContent GetInventory()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<object> lists = new List<object>();
                #region 冷冻库
                string WareCodeLD = WarehouseEnum.冷冻库.ObjToInt().ToString("000");
                List<Dt_InventoryInfo> inventoryInfosLD = BaseDal.QueryData(x => x.WarehouseCode == WareCodeLD);
                object LDQty = new
                {
                    name = WarehouseEnum.冷冻库.ObjToString(),
                    count = inventoryInfosLD.Sum(x => x.StockQuantity).ObjToInt()
                };
                lists.Add(LDQty);
                #endregion
                #region 麻精库
                string WareCodeMJ = WarehouseEnum.麻精库.ObjToInt().ToString("000");
                List<Dt_InventoryInfo> inventoryInfosMJ = BaseDal.QueryData(x => x.WarehouseCode == WareCodeMJ);
                object MJQty = new
                {
                    name = WarehouseEnum.麻精库.ObjToString(),
                    count = inventoryInfosMJ.Sum(x => x.StockQuantity).ObjToInt()
                };
                lists.Add(MJQty);
                #endregion
                #region 大件库
                string WareCodeDJ = WarehouseEnum.大件库.ObjToInt().ToString("000");
                List<Dt_InventoryInfo> inventoryInfosDJ = BaseDal.QueryData(x => x.WarehouseCode == WareCodeDJ);
                object DJQty = new
                {
                    name = WarehouseEnum.大件库.ObjToString(),
                    count = inventoryInfosDJ.Sum(x => x.StockQuantity).ObjToInt()
                };
                lists.Add(DJQty);
                #endregion
                content.OK(data: lists);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
 
        
    }
}