/*********************************************************************** * 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 { /// /// 铺货 接口实现 /// public class PuhuoRepository : BaseRepository, IPuhuoRepository { private readonly IUnitOfWork _unitOfWork; public PuhuoRepository(IUnitOfWork unitOfWork) : base(unitOfWork) { _unitOfWork = unitOfWork; } #region 实现重写增删改查操作========================================================== /// /// 重写异步插入方法 /// /// 实体数据 /// public async Task 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; } /// /// 重写异步更新方法 /// /// /// public async Task UpdateAsync(Puhuo entity) { var jm = new AdminUiCallBack(); var oldModel = await DbClient.Queryable().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; } /// /// 重写异步更新方法 /// /// /// public async Task UpdateAsync(List 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; } /// /// 重写删除指定ID的数据 /// /// /// public async Task DeleteByIdAsync(object id) { var jm = new AdminUiCallBack(); var bl = await DbClient.Deleteable(id).ExecuteCommandHasChangeAsync(); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure; return jm; } /// /// 重写删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(int[] ids) { var jm = new AdminUiCallBack(); var bl = await DbClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure; return jm; } #endregion #region 获取缓存的所有数据========================================================== /// /// 获取缓存的所有数据 /// /// public async Task> GetCaChe() { var list = await DbClient.Queryable().With(SqlWith.NoLock).WithCache().ToListAsync(); return list; } #endregion #region 重写根据条件查询分页数据 /// /// 重写根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NOLOCK) /// public async Task> QueryPageAsync(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { RefAsync totalCount = 0; List page; if (blUseNoLock) { page = await DbClient.Queryable() .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() .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().In(puhuo.distributionId).WithNoLockOrNot(blUseNoLock).WithCacheIF(false).SingleAsync(); if (distribution !=null) { puhuo.schoolName = distribution.schoolName; } } var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } #endregion #region 重写根据条件查询分页数据 /// /// 重写根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NOLOCK) /// public async Task> QueryPageGroupAsync(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, OtherData otherData, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { RefAsync totalCount = 0; List page; if (blUseNoLock) { page = await DbClient.Queryable() .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() .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() // .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(page, pageIndex, pageSize, totalCount); return list; } #endregion } }