huanghongfeng
2025-03-13 06cac68706ac1a4ae2ac05842f947a10507ae215
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
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_ISystemRepository;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models.System;
 
namespace WIDESEA_SystemService
{
    public class Dt_palletService : ServiceBase<Dt_pallet, IDt_palletRepository>, IDt_palletService
    {
        public Dt_palletService(IDt_palletRepository BaseDal) : base(BaseDal)
        {
        }
 
        public IDt_palletRepository Repository => BaseDal;
 
        public WebResponseContent SetCacheList()
        {
            var data = BaseDal.QueryData().Select(x => new { x.palletname, x.palletnumber }).Distinct().ToList();
            return WebResponseContent.Instance.OK(data: data);
        }
 
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public virtual WebResponseContent AddData(SaveModel saveModel)
        {
            try
            {
                if (saveModel == null || saveModel.MainData == null || saveModel.MainData.Count == 0)//判断参数是否传入
                {
                    return WebResponseContent.Instance.Error("传参错误,参数不能为空");
                }
                string validResult = typeof(Dt_pallet).ValidateDicInEntity(saveModel.MainData, true, TProperties);
 
                if (!string.IsNullOrEmpty(validResult))
                {
                    return WebResponseContent.Instance.Error(validResult);
                }
 
                PropertyInfo keyPro = typeof(Dt_pallet).GetKeyProperty();
                if (keyPro == null)
                {
                    return WebResponseContent.Instance.Error("请先设置主键");
                }
                if (keyPro.PropertyType == typeof(Guid))
                {
                    saveModel.MainData.Add(keyPro.Name, Guid.NewGuid());
                }
                else if (keyPro.PropertyType == typeof(int) || keyPro.PropertyType == typeof(long))
                {
                    SugarColumn? sugarColumn = keyPro.GetCustomAttribute<SugarColumn>();
                    if (sugarColumn?.IsIdentity ?? true)
                    {
                        saveModel.MainData.Remove(keyPro.Name.FirstLetterToUpper());
                        saveModel.MainData.Remove(keyPro.Name.FirstLetterToLower());
                    }
                }
                Dt_pallet entity = saveModel.MainData.DicToModel<Dt_pallet>();
 
                List<Dt_pallet> dt_Pallet = BaseDal.QueryData(x => x.palletnumber == entity.palletnumber).ToList();
                if(dt_Pallet.Count > 0)
                {
                    return WebResponseContent.Instance.Error("已添加该类型编号!!!");
                }
                List<Dt_pallet> dt_Pallet2 = BaseDal.QueryData(x => x.palletname == entity.palletname).ToList();
                if (dt_Pallet2.Count > 0)
                {
                    return WebResponseContent.Instance.Error("已添加该类型名称!!!");
                }
 
                if (saveModel.DetailData == null || saveModel.DetailData.Count == 0)
                {
                    BaseDal.AddData(entity);
                    return WebResponseContent.Instance.OK();
                }
 
                if (typeof(Dt_pallet).GetNavigatePro() == null)
                {
                    return WebResponseContent.Instance.Error("未配置导航属性");
                }
 
                Type detailType = typeof(Dt_pallet).GetDetailType();
                MethodInfo? methodInfo = GetType().GetMethod("AddDataIncludesDetail");
                methodInfo = methodInfo?.MakeGenericMethod(new Type[] { detailType });
                object? obj = methodInfo?.Invoke(this, new object[] { entity, detailType, saveModel.DetailData });
                return obj as WebResponseContent;
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
    }
}