刘磊
2025-06-09 dabbcafc629ef87d11ba55ef8cc1cdc776c047d8
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
using Masuit.Tools;
using Newtonsoft.Json.Serialization;
using System.ComponentModel.DataAnnotations;
using WIDESEA_Common.Boxing;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StorageBasicService;
 
public class BoxingInfoService : ServiceBase<DtBoxingInfo, IBoxingInfoRepository>, IBoxingInfoService
{
    private readonly IStockInfoRepository stockInfoRepository;
    private readonly IBoxingInfoDetailRepository boxingInfoDetailRepository;
    public BoxingInfoService(IBoxingInfoRepository BaseDal, IStockInfoRepository stockInfoRepository, IBoxingInfoDetailRepository boxingInfoDetailRepository) : base(BaseDal)
    {
        this.stockInfoRepository = stockInfoRepository;
        this.boxingInfoDetailRepository = boxingInfoDetailRepository;
    }
 
    // 验证模型
    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;
    }
 
    public WebResponseContent AddBoxingInfo(SaveModel saveModel)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            string palletcode = saveModel.MainData["palletCode"].ToString();
            List<BoxingInfos> obj = JsonConvert.DeserializeObject<List<BoxingInfos>>(saveModel.MainData["jsonData"].ToString());
            if (obj == null || obj.Count == 0) throw new Exception("物料信息为空,请输入");
 
            var info = BaseDal.QueryFirst(x => x.PalletCode == palletcode);
            if (info != null) throw new Exception("该托盘已存在组盘");
 
            var stockinfo = stockInfoRepository.QueryFirst(x => x.PalletCode == palletcode);
            if (stockinfo != null) throw new Exception("该托盘已存在库存数据");
 
            foreach (var item in obj)
            {
                if (boxingInfoDetailRepository.QueryFirst(x => x.MaterielName == item.MaterilName) != null) throw new Exception($"{item.MaterilName}已存在组盘数据");
            }
 
            DtBoxingInfo dtBoxingInfo = new DtBoxingInfo()
            {
                Creater = App.User.UserName,
                CreateDate = DateTime.Now,
                PalletCode = palletcode,
                CurrentStatue = 1,
                BoxingInfoDetails = obj.Select(detail => new DtBoxingInfoDetail()
                {
                    MaterielName = detail.MaterilName,
                    Quantity = detail.Num
                }).ToList(),
            };
 
            var isBox = BaseDal.AddDataNavAsync(dtBoxingInfo).Result;
            if (isBox)
            {
                return content.OK();
            }
            else
            {
                return content.Error();
            }
 
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
 
    }
}