1
huangxiaoqiang
2025-12-18 9753fb2756f6b4e30ff79d901a7bb86145517c8b
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using SqlSugar;
using WIDESEA_Core.Attributes;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.CodeConfigEnum;
using WIDESEA_Core.DB;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Seed;
using WIDESEA_DTO.Base;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
 
namespace WIDESEA_BasicService
{
    public class BasicService : IBasicService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
        public IPalletCodeInfoService PalletCodeInfoService { get; }
 
        public ILocationInfoService LocationInfoService { get; }
 
        public IMaterielInfoService MaterielInfoService { get; }
 
        public IWarehouseService WarehouseService { get; }
 
        public IMaterielCodeInfoService MaterielCodeInfoService { get; }
 
        public IMaterialUnitService MaterialUnitService { get; }
 
        public BasicService(IUnitOfWorkManage unitOfWorkManage, ILocationInfoService locationInfoService, IMaterielInfoService materielInfoService, IWarehouseService warehouseService, IPalletCodeInfoService palletCodeInfoService, IMaterielCodeInfoService materielCodeInfoService, IMaterialUnitService materialUnitService)
        {
            _unitOfWorkManage = unitOfWorkManage;
            LocationInfoService = locationInfoService;
            MaterielInfoService = materielInfoService;
            WarehouseService = warehouseService;
            PalletCodeInfoService = palletCodeInfoService;
            MaterielCodeInfoService = materielCodeInfoService;
            MaterialUnitService = materialUnitService;
        }
 
        #region
        public string CreateCodeByRule(string ruleCode)
        {
            string code = string.Empty;
            DateTime dataTime = DateTime.Now;
            DateTime now = DateTime.Now;
 
            try
            {
                Dt_CodeRuleConfig ruleConfig = _unitOfWorkManage.Db.Queryable<Dt_CodeRuleConfig>().Where(x => x.RuleCode == ruleCode).First();
                if (ruleConfig.ModifyDate != null)
                {
                    dataTime = ruleConfig.ModifyDate.Value;
                }
                else
                {
                    dataTime = ruleConfig.CreateDate;
                }
 
                if (now.Year == dataTime.Year && now.Month == dataTime.Month && now.Day == dataTime.Day)
                {
                    now = dataTime;
                    ruleConfig.CurrentVal = Convert.ToInt32(ruleConfig.CurrentVal) + 1;
                }
                else
                {
                    ruleConfig.CurrentVal = 1;
                }
 
                ruleConfig.ModifyDate = DateTime.Now;
 
                code = ruleConfig.Format;
                code = code.Replace($"[{CodeFormatTypeEnum.YYYY}]", now.Year.ToString().PadLeft(4, '0'));
                code = code.Replace($"[{CodeFormatTypeEnum.MM}]", now.Month.ToString().PadLeft(2, '0'));
                code = code.Replace($"[{CodeFormatTypeEnum.DD}]", now.Day.ToString().PadLeft(2, '0'));
                code = code.Replace($"[{CodeFormatTypeEnum.ST}]", ruleConfig.StartStr.ToString());
                code = code.Replace($"[{CodeFormatTypeEnum.NUM}]", ruleConfig.CurrentVal.ToString().PadLeft(ruleConfig.Length, '0'));
 
                _unitOfWorkManage.Db.Updateable(ruleConfig).ExecuteCommand();
            }
            catch (Exception ex)
            {
 
            }
            return code;
        }
        #endregion
 
        #region 单位转换
        /// <summary>
        /// 单位转换
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="fromUnit"></param>
        /// <param name="toUnit"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitQuantityConvert(string materialCode, string fromUnit, string toUnit, decimal quantity)
        {
            if (string.IsNullOrEmpty(fromUnit))
            {
                throw new Exception($"转换前单位不能为空");
            }
 
            if (string.IsNullOrEmpty(toUnit))
            {
                throw new Exception($"转换后单位不能为空");
            }
 
            decimal ratio = 1;
 
            if (fromUnit.Trim().ToLower() == toUnit.Trim().ToLower())
            {
                return new UnitConvertResultDTO(materialCode, fromUnit, toUnit, quantity, ratio);
            }
 
            Dt_MaterialUnit materialUnit = MaterialUnitService.Repository.QueryFirst(x => x.ItemNo == materialCode && x.FromUom == fromUnit && x.ToUom == toUnit);
            if (materialUnit != null)
            {
                ratio = materialUnit.Ratio;
                return new UnitConvertResultDTO(materialCode, fromUnit, toUnit, quantity, ratio);
            }
            else
            {
                materialUnit = MaterialUnitService.Repository.QueryFirst(x => x.ItemNo == materialCode && x.FromUom == toUnit && x.ToUom == fromUnit);
                if (materialUnit == null)
                {
                    throw new Exception($"未找到单位转换关系,物料编号:{materialCode},转换前单位:{fromUnit},转换后单位:{toUnit}");
                }
                ratio = materialUnit.Ratio;
                return new UnitConvertResultDTO(materialCode, fromUnit, toUnit, quantity, ratio);
            }
        }
 
        /// <summary>
        /// 单位转换,领料转采购
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitUsageToPurchase(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
 
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
 
            return UnitQuantityConvert(materialCode, materielInfo.usageUOM, materielInfo.purchaseUOM, quantity);
        }
 
        /// <summary>
        /// 单位转换,领料转库存
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitUsageToInventory(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
 
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
 
            return UnitQuantityConvert(materialCode, materielInfo.usageUOM, materielInfo.inventoryUOM, quantity);
        }
 
        /// <summary>
        /// 单位转换,采购转库存
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitPurchaseToInventory(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
 
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
 
            return UnitQuantityConvert(materialCode, materielInfo.purchaseUOM, materielInfo.inventoryUOM, quantity);
        }
 
        /// <summary>
        /// 单位转换,采购转领料
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitPurchaseToUsage(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
 
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
 
            return UnitQuantityConvert(materialCode, materielInfo.purchaseUOM, materielInfo.usageUOM, quantity);
        }
 
        /// <summary>
        /// 单位转换,库存转领料
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitInventoryToUsage(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
 
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
 
            return UnitQuantityConvert(materialCode, materielInfo.inventoryUOM, materielInfo.usageUOM, quantity);
        }
 
        /// <summary>
        /// 单位转换,库存转采购
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public UnitConvertResultDTO UnitInventoryToPurchase(string materialCode, decimal quantity)
        {
            if (string.IsNullOrEmpty(materialCode))
            {
                throw new Exception($"物料编号不能为空");
            }
 
            Dt_MaterielInfo materielInfo = MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == materialCode);
            if (materielInfo == null)
            {
                throw new Exception($"未找到物料信息{materialCode}");
            }
 
            return UnitQuantityConvert(materialCode, materielInfo.inventoryUOM, materielInfo.purchaseUOM, quantity);
        }
        #endregion
    }
}