刘磊
2025-11-17 efbeb93b3d454f06d65dbcc68ad828cc95bc9404
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
using WIDESEA_Common;
using WIDESEA_IServices;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_BasicInfoService
{
    public partial class Dt_BDCConfigurationService : ServiceBase<Dt_BDCConfiguration, IDt_BDCConfigurationRepository>, IDt_BDCConfigurationService
    {
        private readonly ISys_ConfigService _sys_ConfigService;
        private readonly IDt_PalletStockInfoRepository _palletStockInfoRepository;
 
        public Dt_BDCConfigurationService(IDt_BDCConfigurationRepository BaseDal, ISys_ConfigService sys_ConfigService, IDt_PalletStockInfoRepository palletStockInfoRepository) : base(BaseDal)
        {
            _sys_ConfigService = sys_ConfigService;
            _palletStockInfoRepository = palletStockInfoRepository;
        }
 
        public async Task<Dt_BDCConfiguration> GetCurrentConfiguration()
        {
            // 获取当前生效的配置
            var currentConfig = await BaseDal.QueryFirstAsync(c => c.IsActive == 1);
 
            // 如果没有配置,创建默认配置
            if (currentConfig == null)
            {
                currentConfig = new Dt_BDCConfiguration();
                BaseDal.AddData(currentConfig);
            }
            return currentConfig;
        }
 
        public async Task<Dt_BDCConfiguration> UpdateConfiguration(int maxWhiteBody, int maxPaintedBody,
           int maxBatteryCase, int maxEmptySled, string updatedBy)
        {
            // 禁用当前配置
            var currentConfigs = await BaseDal.QueryDataAsync(c => c.IsActive == 1);
 
            currentConfigs.ForEach(c => c.IsActive = 2);
 
            // 创建新配置
            var newConfig = new Dt_BDCConfiguration
            {
                MaxWhiteBodyCache = maxWhiteBody,
                MaxPaintedBodyCache = maxPaintedBody,
                MaxBatteryCaseCache = maxBatteryCase,
                MaxEmptySledCache = maxEmptySled,
                LastUpdatedTime = DateTime.Now,
                UpdatedBy = updatedBy,
                IsActive = 1
            };
 
            BaseDal.UpdateData(currentConfigs);
            BaseDal.AddData(newConfig);
            return newConfig;
        }
 
        public async Task<bool> CanAddWhiteBody()
        {
            var config = await GetCurrentConfiguration();
            var currentCount =  GetWhiteBodyCount();
 
            // 预留5%的缓冲空间
            var threshold = (int)(config.MaxWhiteBodyCache * 0.95);
            return currentCount < threshold;
        }
 
        public async Task<bool> CanAddPaintedBody()
        {
            var config = await GetCurrentConfiguration();
            var currentCount =  GetPaintedBodyCount();
 
            // 预留5%的缓冲空间
            var threshold = (int)(config.MaxPaintedBodyCache * 0.95);
            return currentCount < threshold;
        }
 
 
        public async Task<bool> CanAddEmptySled()
        {
            var config = await GetCurrentConfiguration();
            var currentCount =  GetEmptySledCount();
 
            // 预留5%的缓冲空间
            var threshold = (int)(config.MaxEmptySledCache * 0.95);
            return currentCount < threshold;
        }
 
        public int GetWhiteBodyCount()
        {
            return _palletStockInfoRepository.QueryData(b => b.CarType == (int)BodyType.WhiteBody && b.StockStatus == (int)BodyStatus.InBDC).Count;
        }
 
        public int GetPaintedBodyCount()
        {
            return  _palletStockInfoRepository.QueryData(b => b.CarType == (int)BodyType.PaintedBody && b.StockStatus == (int)BodyStatus.InBDC).Count;
        }
 
        public int GetBatteryCaseCount()
        {
            return _palletStockInfoRepository.QueryData(b => b.StockStatus == (int)BodyStatus.InBDC).Count;
        }
 
        public int GetEmptySledCount()
        {
            return _palletStockInfoRepository.QueryData(s => s.StockStatus == (int)BodyStatus.InBDC).Count;
        }
    }
}