tangxu
2024-01-09 ddc2780231ea76be74fadb7486401a3d0d17b101
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
namespace Yw.Application
{
    /// <summary>
    /// 
    /// </summary>
    public class UpdateHealthQuotaEvaluationModelInput : IValidatableObject
    {
        /// <summary>
        /// id
        /// </summary>    
        [Required, Range(1, long.MaxValue, ErrorMessage = "ID 必须大于0")]
        public long ID { get; set; }
 
        /// <summary>
        /// 名称
        /// </summary>
        [Required]
        public string Name { get; set; }
 
        /// <summary>
        /// 评价方式
        /// </summary>
        [Required, Range(0, 1, ErrorMessage = "Way 值范围 [0,1] ")]
        public eEvaluateWay Way { get; set; }
 
        /// <summary>
        /// 评价标准
        /// </summary>
        [Required]
        public string Evaluation { get; set; }
 
        /// <summary>
        /// 缺省值
        /// </summary>
        [Required]
        public double DefaultValue { get; set; }
 
        /// <summary>
        /// 说明
        /// </summary>    
        public string Description { get; set; }
 
        /// <summary>
        /// 验证
        /// </summary>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (this.Way == eEvaluateWay.Auto)
            {
                var json = AutoEvaluation.ToModel(this.Evaluation);
                if (json == null)
                {
                    yield return new ValidationResult(
                       "Evaluation 参数错误"
                       , new[] { nameof(Evaluation) }
                   );
                }
 
                else if (json.Items == null || json.Items.Count < 1)
                {
                    yield return new ValidationResult(
                       "Evaluation 参数错误"
                       , new[] { nameof(Evaluation) }
                   );
                }
 
                else
                {
                    foreach (var item in json.Items)
                    {
                        if (string.IsNullOrEmpty(item.Expression))
                        {
                            yield return new ValidationResult(
                              "Expression 不能为空"
                              , new[] { nameof(item.Expression) }
                            );
                        }
 
                        else if (!DynamicExpresso.Validator.VerifyMax(item.Expression, 1))
                        {
                            yield return new ValidationResult(
                              $"Expression [{item.Expression}] 配置错误"
                              , new[] { nameof(item.Expression) }
                            );
                        }
                    }
                }
 
            }
            else
            {
                var json = HandEvaluation.ToModel(this.Evaluation);
                if (json == null)
                {
                    yield return new ValidationResult(
                       "Evaluation 参数错误"
                       , new[] { nameof(Evaluation) }
                   );
                }
 
                else if (json.Items == null || json.Items.Count < 1)
                {
                    yield return new ValidationResult(
                       "Evaluation 参数错误"
                       , new[] { nameof(Evaluation) }
                   );
                }
 
                else if (json.Items.Exists(x => string.IsNullOrEmpty(x)))
                {
                    var index = json.Items.IndexOf(null);
                    if (index < 0)
                        index = json.Items.IndexOf(string.Empty);
                    yield return new ValidationResult(
                       $"Evaluation [{index}] 参数错误"
                       , new[] { nameof(Evaluation) }
                   );
                }
            }
        }
 
 
    }
}