wanshenmean
2024-09-13 319e8729b47c96e3a3717c5a40cd5df867d65ce5
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
using Masuit.Tools;
using System.ComponentModel.DataAnnotations;
 
namespace WIDESEA_StorageBasicService;
 
public class BoxingInfoService : ServiceBase<DtBoxingInfo, IBoxingInfoRepository>, IBoxingInfoService
{
    public BoxingInfoService(IBoxingInfoRepository BaseDal) : base(BaseDal)
    {
    }
 
    public async Task<WebResponseContent> AddBoxingInfoAsync(DtBoxingInfo boxingInfo)
    {
        WebResponseContent content = new WebResponseContent();
        // 验证模型里面数据是否合法,如果不合法抛出异常
        var errors = ValidateModel(boxingInfo);
        if (errors.Count > 0)
        {
            foreach (var error in errors)
            {
                Console.WriteLine(error.ErrorMessage);
 
            }
            string errorMessage = string.Join(",", errors.Select(e => e.ErrorMessage));
            content.Error(errorMessage);
        }
        else
        {
            var  info = await BaseDal.QueryFirstAsync(x=>x.PalletCode ==  boxingInfo.PalletCode);
            if (!info.IsNullOrEmpty())
            {
                content.Error("该托盘已存在组盘");
            }
            else
            {
                // 添加组盘信息
                var result = await BaseDal.AddDataNavAsync(boxingInfo);
                if (result)
                {
                    content.OK("添加成功", boxingInfo);
                }
            }
        }
        return content;
    }
 
    // 验证模型
    public static List<ValidationResult> ValidateModel(object model)
    {
        // 创建一个验证结果列表
        var validationResults = new List<ValidationResult>();
        // 创建一个验证上下文
        var validationContext = new ValidationContext(model, serviceProvider: null, items: null);
 
        // 使用验证器尝试验证模型,并将验证结果添加到验证结果列表中
        Validator.TryValidateObject(model, validationContext, validationResults, validateAllProperties: true);
 
        // 返回验证结果列表
        return validationResults;
    }
}