tx
2025-04-10 2538101febc78f525945da72c7cdcb2589f9e6ea
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace TProduct.DAL
{
    /// <summary>
    /// 测试项目
    /// </summary>
    public class AutoAdjustScheme : BaseLiteSqlDAL<Entity.AutoAdjustScheme>
    {
        public override ISqlSugarClient Connection
        {
            get { return TProduct.DAL.SQLite.ConfigHelper.MainConn; }
        }
 
        /// <summary>
        /// 插入包含扩展信息
        /// </summary>
        public long InsertEx(Entity.AutoAdjustScheme main, List<Entity.AutoAdjustSchemeDetail> details)
        {
            if (main == null)
                return default;
 
            using (ISqlSugarClient db = Connection)
            {
                try
                {
 
                    long scheme_id = 0;
                    if (details == null || details.Count() == 0)
                    {
                        db.Ado.BeginTran();
                        scheme_id = db.Insertable(main).ExecuteReturnSnowflakeId();
                        if (scheme_id < 1)
                        {
                            db.Ado.RollbackTran();
                            return default;
                        }
                        main.ID = scheme_id;
                    }
                    else
                    {
                        db.Ado.BeginTran();
                        scheme_id = db.Insertable(main).ExecuteReturnSnowflakeId();
                        if (scheme_id < 1)
                        {
                            db.Ado.RollbackTran();
                            return default;
                        }
                        main.ID = scheme_id;
 
                        // 
                        foreach (var detail in details)
                        {
                            detail.SchemeID = main.ID;
                            var result1 = db.Insertable(detail).ExecuteReturnSnowflakeId() > 0;
                            if (!result1)
                            {
                                db.Ado.RollbackTran();
                                return default;
                            }
                        }
                    }
 
                    db.Ado.CommitTran();
                    return scheme_id;
                }
                catch
                {
                    db.Ado.RollbackTran();
                    return default;
                }
            }
        }
 
 
        /// <summary>
        /// 更新包含扩展信息
        /// </summary>
        public bool UpdateEx(Entity.AutoAdjustScheme main, List<Entity.AutoAdjustSchemeDetail> details)
        {
            if (main == null)
                return default;
            var old_details = GetBySchemeID(main.ID);
 
            using (ISqlSugarClient db = Connection)
            {
                try
                {
                    db.Ado.BeginTran();
 
                    main.UpdateTime = DateTime.Now;
                    db.Updateable(main).IgnoreColumns(it => new { it.CreateTime, it.CreateUserID }).ExecuteCommand();
 
 
                    if (old_details == null || old_details.Count() == 0)
                    {
                        foreach (var detail in details)
                        {
                            detail.SchemeID = main.ID;
                            var result1 = db.Insertable(detail).ExecuteReturnSnowflakeId() > 0;
                            if (!result1)
                            {
                                db.Ado.RollbackTran();
                                return default;
                            }
                        }
                    }
                    else
                    {
                        foreach (var old_detail in old_details)
                        {
                            var f_n = details.Find(x => x.ID == old_detail.ID);
                            if (f_n == null)
                            {//没 表示已删除
                                db.Deleteable<Entity.AutoAdjustSchemeDetail>().Where(it => it.ID == old_detail.ID).ExecuteCommand();
                            }
                        }
                        foreach (var detail in details)
                        {
                            if (detail.ID <= 0)
                            {
                                detail.SchemeID = main.ID;
                                var result1 = db.Insertable(detail).ExecuteReturnSnowflakeId() > 0;
                                if (!result1)
                                {
                                    db.Ado.RollbackTran();
                                    return default;
                                }
                            }
                            else
                            {
                                db.Updateable(detail).ExecuteCommand();
                            }
                        }
                    }
 
 
                    db.Ado.CommitTran();
                    return true;
                }
                catch
                {
                    db.Ado.RollbackTran();
                    return default;
                }
            }
        }
 
        /// <summary>
        /// 通拓id列表获取
        /// </summary>
        /// <returns></returns>
        public List<Entity.AutoAdjustSchemeDetail> GetBySchemeID(long SchemeID)
        {
            using (ISqlSugarClient db = Connection)
            {
                return db.Queryable<Entity.AutoAdjustSchemeDetail>().Where(x => x.SchemeID == SchemeID)?.ToList();
            }
        }
        /// <summary>
        /// 通拓id列表获取
        /// </summary>
        /// <returns></returns>
        public List<Entity.AutoAdjustSchemeDetail> GetAllDetails()
        {
            using (ISqlSugarClient db = Connection)
            {
                return db.Queryable<Entity.AutoAdjustSchemeDetail>()?.ToList();
            }
        }
    }
}