移动系统liao
2024-06-14 9e2f3d1c793207fbc599a82bab0464075d777f1f
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
 
namespace CoreCms.Net.Repository
{
    /// <summary>
    /// 单页 接口实现
    /// </summary>
    public class CoreCmsPagesRepository : BaseRepository<CoreCmsPages>, ICoreCmsPagesRepository
    {
        public CoreCmsPagesRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
 
 
        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> InsertAsync(CoreCmsPages entity)
        {
            var jm = new AdminUiCallBack();
 
            var have = await DbClient.Queryable<CoreCmsPages>().Where(p => p.code == entity.code).With(SqlWith.NoLock).AnyAsync();
            if (have)
            {
                jm.msg = "存在相同【区域编码】请更正";
                return jm;
            }
 
            entity.code = entity.code.Trim();
 
            var id = await DbClient.Insertable(entity).ExecuteReturnIdentityAsync();
            var bl = id > 0;
 
            if (bl && entity.type == 1)
            {
                //如果设为新默认,则修改其他为非默认。
                await DbClient.Updateable<CoreCmsPages>().Where(p => p.type == 1 && p.id != id).SetColumns(p => new CoreCmsPages() { type = 2 }).ExecuteCommandAsync();
            }
 
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.CreateSuccess : GlobalConstVars.CreateFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(CoreCmsPages entity)
        {
            var jm = new AdminUiCallBack();
 
            var have = await DbClient.Queryable<CoreCmsPages>().Where(p => p.code == entity.code && p.id != entity.id).With(SqlWith.NoLock).AnyAsync();
            if (have)
            {
                jm.msg = "存在相同【区域编码】请更正";
                return jm;
            }
            var oldModel = await DbClient.Queryable<CoreCmsPages>().In(entity.id).SingleAsync();
            if (oldModel == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            //事物处理过程开始
            var oldType = oldModel.type;
            var newType = entity.type;
 
            oldModel.code = entity.code;
            oldModel.name = entity.name;
            oldModel.description = entity.description;
            oldModel.layout = entity.layout;
            oldModel.type = entity.type;
 
            //事物处理过程结束
            var bl = await DbClient.Updateable(oldModel).ExecuteCommandHasChangeAsync();
 
            if (bl)
            {
                //如果不是默认的情况下
                if (oldType == 1 && newType != 1)
                {
                    //判断修改当前,而是否其他有默认
                    var haveDefault = await DbClient.Queryable<CoreCmsPages>().Where(p => p.type == 1 && p.id != oldModel.id).With(SqlWith.NoLock).AnyAsync();
                    //如果不存在,则当前不能调整为非默认。
                    if (!haveDefault)
                    {
                        await DbClient.Updateable<CoreCmsPages>().Where(p => p.id == oldModel.id).SetColumns(p => new CoreCmsPages() { type = 1 }).ExecuteCommandAsync();
                    }
                }
                //如果设为新默认,则修改其他为非默认。
                if (newType == 1)
                {
                    await DbClient.Updateable<CoreCmsPages>().Where(p => p.id != oldModel.id).SetColumns(p => new CoreCmsPages() { type = 2 }).ExecuteCommandAsync();
                }
            }
 
 
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdAsync(int id)
        {
            var jm = new AdminUiCallBack();
 
            var model = await DbClient.Queryable<CoreCmsPages>().Where(p => p.id == id).FirstAsync();
            if (model == null)
            {
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
            if (model.type == 1)
            {
                jm.msg = "默认页面禁止删除";
                return jm;
            }
 
            var count = await DbClient.Queryable<CoreCmsPages>().CountAsync();
            if (count == 1)
            {
                jm.msg = "只有一个页面了,别删了。";
                return jm;
            }
 
            var bl = await DbClient.Deleteable<CoreCmsPages>(id).ExecuteCommandHasChangeAsync();
            if (bl)
            {
                await DbClient.Deleteable<CoreCmsPagesItems>().Where(p => p.pageCode == model.code).ExecuteCommandAsync();
            }
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
 
            return jm;
        }
 
 
        /// <summary>
        /// 复制一个同样的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> CopyByIdAsync(int id)
        {
            var jm = new AdminUiCallBack();
 
            var model = await DbClient.Queryable<CoreCmsPages>().Where(p => p.id == id).FirstAsync();
            if (model == null)
            {
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
            var oldCode = model.code;
            model.type = 2;
            model.code = model.code + DateTime.Now.ToString("yyyyMMddHHmmssfffff");
            model.name = model.name + "(复制)";
 
            var items = await DbClient.Queryable<CoreCmsPagesItems>().Where(p => p.pageCode == oldCode).ToListAsync();
            foreach (var item in items)
            {
                item.pageCode = model.code;
            }
 
            var bl = await DbClient.Insertable<CoreCmsPages>(model).ExecuteReturnIdentityAsync() > 0;
            if (bl)
            {
                await DbClient.Insertable<CoreCmsPagesItems>(items).ExecuteCommandAsync();
            }
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
 
            return jm;
        }
 
 
 
        #region 更新设计==========================================================
 
        /// <summary>
        /// 更新设计
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateDesignAsync(FmPagesUpdate entity)
        {
            var jm = new AdminUiCallBack();
 
            var bl = false;
 
            var oldModel = await DbClient.Queryable<CoreCmsPages>().Where(p => p.code == entity.pageCode).SingleAsync();
            if (oldModel == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            //事物处理过程开始
 
            bl = await DbClient.Deleteable<CoreCmsPagesItems>().Where(p => p.pageCode == entity.pageCode).ExecuteCommandHasChangeAsync();
            var list = new List<CoreCmsPagesItems>();
            var count = 0;
            entity.datalist.ForEach(p =>
            {
                var model = new CoreCmsPagesItems
                {
                    widgetCode = p.sType,
                    pageCode = entity.pageCode,
                    positionId = count,
                    sort = count + 1,
                    parameters = p.sValue
                };
                list.Add(model);
                count++;
            });
 
            if (list.Any())
            {
                bl = await DbClient.Insertable(list).ExecuteCommandAsync() > 0;
            }
            //事物处理过程结束
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
 
 
            return jm;
        }
 
        #endregion
    }
}