11
pulg
2025-06-05 28bea7128b8c24a6a664e880f1425e1fa24b9b5f
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
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
 
namespace HH.WCS.SJML.Dto
{
    public enum ResultStatus
    {
        Error = 0,
        Success = 1,
        Warning = 2
    }
    public class OperateResult
    {
        public OperateResult()
        {
            this.okList = new List<OkTask>();
            this.failList = new List<FailTask>();
        }
        public OperateResult(ResultStatus status, string msg, bool success, int affectedRows = 0)
        {
            Status = status;
            Msg = msg;
            Success = success;
            AffectedRows = affectedRows;
        }
        public OperateResult(ResultStatus status, string msg, object data, bool success)
        {
            Status = status;
            Msg = msg;
            Data = data;
            Success = success;
        }
        public List<OkTask> okList { get; set; }
        public List<FailTask> failList { get; set; }
        public ResultStatus Status { get; set; }
        public string Code { get; set; }
        public string Msg { get; set; }
        public object Data { get; set; }
        public int AffectedRows { get; set; }
        public bool Success { get; set; }
        public static OperateResult Succeed(string msg = "succeed")
        {
            return new OperateResult(ResultStatus.Success, msg, true);
        }
        public static OperateResult Succeed(int affectedRows)
        {
            return new OperateResult(ResultStatus.Success, "succeed", true, affectedRows);
        }
        public static OperateResult Succeed(string msg, object data)
        {
            return new OperateResult(ResultStatus.Success, msg, data, true);
        }
 
        public static OperateResult Error(string msg = "error")
        {
            return new OperateResult(ResultStatus.Error, msg, false);
        }
        public OperateResult Error<T>(T req, string msg, List<T> reqList = null)
        {
            this.Success = false;
            if (string.IsNullOrEmpty(msg))
            {
                if (failList.Any())
                    msg = failList[0].errMsg;
            }
            this.Msg = msg;
            this.Code = this.failList.Any() ? failList[0].errCode : "";
            return this;
        }
        public OperateResult Ok<T>(T req, List<T> reqList = null)
        {
            this.Success = true;
            return this;
        }
 
        public static OperateResult Error(string msg, object data)
        {
            return new OperateResult(ResultStatus.Error, msg, data, false);
        }
 
        public static OperateResult Warning(string msg = "warning")
        {
            return new OperateResult(ResultStatus.Warning, msg, false);
        }
        public static OperateResult Warning(string msg, object data)
        {
            return new OperateResult(ResultStatus.Warning, msg, data, false);
        }
 
        public string Describe()
        {
            return "结果:" + (Success ? "成功!" : "失败!" + Msg);
        }
    }
    public static class OperateResultExtensions
    {
        public static T GetData<T>(this OperateResult result) where T : class
        {
            if (result.Data == null)
                return default(T);
            if (result.Data.GetType() == typeof(JObject) ||
                result.Data.GetType() == typeof(JArray) ||
                result.Data.GetType() == typeof(string))
            {
                string data = result.Data.ToString();
                IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                return JsonConvert.DeserializeObject<T>(data, timeFormat);
            }
            return result.Data as T;
        }
 
    }
}