移动系统liao
2025-02-17 557c2711a3e103ebc3d0492344eca9730d5e92b2
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 2024/5/24 11:11:45
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Caching.Manual;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.Model.ViewModels.UI;
using SqlSugar;
using CoreCms.Net.Model.Entities.baifenbingfa.puhuo;
 
namespace CoreCms.Net.Repository
{
    /// <summary>
    /// 铺货 接口实现
    /// </summary>
    public class PuhuoRepository : BaseRepository<Puhuo>, IPuhuoRepository
    {
        private readonly IUnitOfWork _unitOfWork;
        public PuhuoRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
 
       #region 实现重写增删改查操作==========================================================
 
        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> InsertAsync(Puhuo entity)
        {
            var jm = new AdminUiCallBack();
 
            var bl = await DbClient.Insertable(entity).ExecuteReturnIdentityAsync() > 0;
            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(Puhuo entity)
        {
            var jm = new AdminUiCallBack();
 
            var oldModel = await DbClient.Queryable<Puhuo>().In(entity.id).SingleAsync();
            if (oldModel == null)
            {
            jm.msg = "不存在此信息";
            return jm;
            }
            //事物处理过程开始
            oldModel.id = entity.id;
            oldModel.distributionId = entity.distributionId;
            oldModel.distributionName = entity.distributionName;
            oldModel.areaCode = entity.areaCode;
            oldModel.areaCodeName = entity.areaCodeName;
            oldModel.CategoryId = entity.CategoryId;
            oldModel.CategoryName = entity.CategoryName;
            oldModel.goodsId = entity.goodsId;
            oldModel.goodsName = entity.goodsName;
            oldModel.productsId = entity.productsId;
            oldModel.productsName = entity.productsName;
            oldModel.spesDesc = entity.spesDesc;
            oldModel.price = entity.price;
            oldModel.nums = entity.nums;
            oldModel.amount = entity.amount;
            oldModel.createTime = entity.createTime;
            oldModel.upDataTime = entity.upDataTime;
            oldModel.createBy = entity.createBy;
            oldModel.upDataBy = entity.upDataBy;
            oldModel.isdelete = entity.isdelete;
            oldModel.shoukuannums = entity.shoukuannums;
            oldModel.shoukuanamount = entity.shoukuanamount;
 
            //事物处理过程结束
            var bl = await DbClient.Updateable(oldModel).ExecuteCommandHasChangeAsync();
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(List<Puhuo> entity)
        {
            var jm = new AdminUiCallBack();
 
            var bl = await DbClient.Updateable(entity).ExecuteCommandHasChangeAsync();
            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(object id)
        {
            var jm = new AdminUiCallBack();
 
            var bl = await DbClient.Deleteable<Puhuo>(id).ExecuteCommandHasChangeAsync();
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
 
            return jm;
        }
 
        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdsAsync(int[] ids)
        {
            var jm = new AdminUiCallBack();
 
            var bl = await DbClient.Deleteable<Puhuo>().In(ids).ExecuteCommandHasChangeAsync();
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
 
            return jm;
        }
 
        #endregion
 
       #region 获取缓存的所有数据==========================================================
 
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<Puhuo>> GetCaChe()
        {
           var list = await DbClient.Queryable<Puhuo>().With(SqlWith.NoLock).WithCache().ToListAsync();
            return list;
        }
 
        #endregion
 
 
        #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<IPageList<Puhuo>> QueryPageAsync(Expression<Func<Puhuo, bool>> predicate,
            Expression<Func<Puhuo, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
            int pageSize = 20, bool blUseNoLock = false)
        {
            RefAsync<int> totalCount = 0;
            List<Puhuo> page;
            if (blUseNoLock)
            {
                page = await DbClient.Queryable<Puhuo>()
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .WhereIF(predicate != null, predicate).Select(p => new Puhuo
                {
                      id = p.id,
                distributionId = p.distributionId,
                distributionName = p.distributionName,
                areaCode = p.areaCode,
                areaCodeName = p.areaCodeName,
                CategoryId = p.CategoryId,
                CategoryName = p.CategoryName,
                goodsId = p.goodsId,
                goodsName = p.goodsName,
                productsId = p.productsId,
                productsName = p.productsName +" " + p.spesDesc,
                spesDesc = p.spesDesc,
                price = p.price,
                nums = p.nums,
                amount = p.amount,
                createTime = p.createTime,
                upDataTime = p.upDataTime,
                createBy = p.createBy,
                upDataBy = p.upDataBy,
                isdelete = p.isdelete,
                
                }).With(SqlWith.NoLock).ToPageListAsync(pageIndex, pageSize, totalCount);
            }
            else
            {
                page = await DbClient.Queryable<Puhuo>()
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .WhereIF(predicate != null, predicate).Select(p => new Puhuo
                {
                      id = p.id,
                distributionId = p.distributionId,
                distributionName = p.distributionName,
                areaCode = p.areaCode,
                areaCodeName = p.areaCodeName,
                CategoryId = p.CategoryId,
                CategoryName = p.CategoryName,
                goodsId = p.goodsId,
                goodsName = p.goodsName,
                productsId = p.productsId,
                productsName = p.productsName + " " + p.spesDesc,
                spesDesc = p.spesDesc,
                price = p.price,
                nums = p.nums,
                amount = p.amount,
                createTime = p.createTime,
                upDataTime = p.upDataTime,
                createBy = p.createBy,
                upDataBy = p.upDataBy,
                isdelete = p.isdelete,
                
                }).ToPageListAsync(pageIndex, pageSize, totalCount);
            }
 
            foreach (var puhuo  in page)
            {
              var distribution = await  DbClient.Queryable<CoreCmsDistribution>().In(puhuo.distributionId).WithNoLockOrNot(blUseNoLock).WithCacheIF(false).SingleAsync();
                if (distribution !=null)
                {
                    puhuo.schoolName = distribution.schoolName;
                }
            }
 
            var list = new PageList<Puhuo>(page, pageIndex, pageSize, totalCount);
            return list;
        }
 
        #endregion
 
 
 
 
        #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<IPageList<Puhuo>> QueryPageGroupAsync(Expression<Func<Puhuo, bool>> predicate,
            Expression<Func<Puhuo, object>> orderByExpression, OrderByType orderByType, OtherData otherData, int pageIndex = 1,
            int pageSize = 20, bool blUseNoLock = false)
        {
            RefAsync<int> totalCount = 0;
            List<Puhuo> page;
            if (blUseNoLock)
            {
                page = await DbClient.Queryable<Puhuo>()
                .GroupBy(it => new {  it.CategoryId, it.CategoryName, it.goodsId, it.goodsName, it.productsId, it.productsName, it.price ,it.spesDesc })
                .WhereIF(predicate != null, predicate)
                .Select(p => new Puhuo
                {
                 
                  
                    CategoryId = p.CategoryId,
                    CategoryName = p.CategoryName,
                    goodsId = p.goodsId,
                    goodsName = p.goodsName,
                    productsId = p.productsId,
                    productsName =  p.productsName + " " + p.spesDesc,
                    price = p.price,
                    tiaozhengnums =SqlFunc.AggregateCount(p.id),
                    nums =  SqlFunc.AggregateSum(p.nums),
                    amount = SqlFunc.AggregateSum(p.amount),
 
 
                })
               .MergeTable()//需要加MergeTable才能排序统计过的列
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .With(SqlWith.NoLock).ToPageListAsync(pageIndex, pageSize, totalCount);
 
 
               
            }
            else
            {
                page = await DbClient.Queryable<Puhuo>()
                 .GroupBy(it => new { it.CategoryId, it.CategoryName, it.goodsId, it.goodsName, it.productsId, it.productsName, it.price })
                 .WhereIF(predicate != null, predicate)
                 .Select(p => new Puhuo
                 {
                    
                     CategoryId = p.CategoryId,
                     CategoryName = p.CategoryName,
                     goodsId = p.goodsId,
                     goodsName = p.goodsName,
                     productsId = p.productsId,
                     productsName = p.productsName + " " + p.spesDesc,
                     price = p.price,
                     tiaozhengnums = SqlFunc.AggregateCount(p.id),
                     nums = SqlFunc.AggregateSum(p.nums),
                     amount = SqlFunc.AggregateSum(p.amount),
 
 
                 })
                .MergeTable()//需要加MergeTable才能排序统计过的列
                 .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                 .ToPageListAsync(pageIndex, pageSize, totalCount);
            }
 
 
            var pagetotal = DbClient.Queryable<Puhuo>()
               // .GroupBy(it => new { it.CategoryId, it.CategoryName, it.goodsId, it.goodsName, it.productsId, it.productsName, it.price })
               .WhereIF(predicate != null, predicate)
               .Select(p => new Puhuo
               {
                   tiaozhengnums = SqlFunc.AggregateCount(p.id),
                   nums = SqlFunc.AggregateSum(p.nums),
                   amount = SqlFunc.AggregateSum(p.amount),
               })
                 .First(); // 或者使用 Single/FirstOrDefault 等方法获取结果
              //.Select(p => new Puhuo
              //{
              //    tiaozhengnums = SqlFunc.AggregateCount(p.id),
              //    nums = SqlFunc.AggregateSum(p.nums),
              //    amount = SqlFunc.AggregateSum(p.amount),
 
 
            //});
 
            otherData.heji4 = pagetotal.nums;
            otherData.heji5 = pagetotal.amount;
            otherData.heji6 = pagetotal.tiaozhengnums;
 
            var list = new PageList<Puhuo>(page, pageIndex, pageSize, totalCount);
            return list;
        }
 
        #endregion
    }
}