/*********************************************************************** * Project: baifenBinfa * ProjectName: 百分兵法管理系统 * Web: http://chuanyin.com * Author: * Email: * CreateTime: 202403/02 * 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.IRepository; using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.Model.ViewModels.Basics; using CoreCms.Net.Model.ViewModels.UI; using SqlSugar; namespace CoreCms.Net.Repository { /// /// 代理商等级设置表 接口实现 /// public class CoreCmsAgentGradeRepository : BaseRepository, ICoreCmsAgentGradeRepository { public CoreCmsAgentGradeRepository(IUnitOfWork unitOfWork) : base(unitOfWork) { } #region 实现重写增删改查操作========================================================== /// /// 重写异步插入方法 /// /// 实体数据 /// public async Task InsertAsync(CoreCmsAgentGrade entity) { var jm = new AdminUiCallBack(); if (await DbClient.Queryable().AnyAsync(p => p.sortId == entity.sortId)) { jm.msg = "存在相同等级排序,请更换!"; return jm; } var id = await DbClient.Insertable(entity).ExecuteReturnIdentityAsync(); var bl = id > 0; jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.CreateSuccess : GlobalConstVars.CreateFailure; if (bl) { if (entity.isDefault) { await DbClient.Updateable().SetColumns(p => p.isDefault == false).Where(p => p.isDefault == true && p.id != id).ExecuteCommandAsync(); } } return jm; } /// /// 重写异步更新方法 /// /// /// public async Task UpdateAsync(CoreCmsAgentGrade entity) { var jm = new AdminUiCallBack(); if (await DbClient.Queryable().AnyAsync(p => p.sortId == entity.sortId && entity.id != p.id)) { jm.msg = "存在相同等级排序,请更换!"; return jm; } if (entity.isDefault == false) { var otherHave = await DbClient.Queryable().AnyAsync(p => p.isDefault == true && p.id != entity.id); if (otherHave == false) { jm.msg = "请保持一个默认分销等级"; return jm; } } var oldModel = await DbClient.Queryable().In(entity.id).SingleAsync(); if (oldModel == null) { jm.msg = "不存在此信息"; return jm; } //事物处理过程开始 //oldModel.id = entity.id; oldModel.name = entity.name; oldModel.isDefault = entity.isDefault; oldModel.isAutoUpGrade = entity.isAutoUpGrade; oldModel.defaultSalesPriceType = entity.defaultSalesPriceType; oldModel.defaultSalesPriceNumber = entity.defaultSalesPriceNumber; oldModel.sortId = entity.sortId; oldModel.description = entity.description; //事物处理过程结束 var bl = await DbClient.Updateable(oldModel).ExecuteCommandHasChangeAsync(); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure; if (bl) { //其他处理 if (entity.isDefault) { await DbClient.Updateable().SetColumns(it => it.isDefault == false).Where(p => p.isDefault == true && p.id != entity.id).ExecuteCommandAsync(); } } return jm; } /// /// 重写删除指定ID的数据 /// /// /// public async Task DeleteByIdAsync(int id) { var jm = new AdminUiCallBack(); if (await DbClient.Queryable().AnyAsync(p => p.gradeId == id)) { jm.msg = "存在关联的分销用户数据,禁止删除"; return jm; } var bl = await DbClient.Deleteable(id).ExecuteCommandHasChangeAsync(); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure; if (bl) { await DbClient.Deleteable().Where(p => p.agentGradeId == id).ExecuteCommandHasChangeAsync(); } 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 CoreCmsAgentGrade { id = p.id, name = p.name, isDefault = p.isDefault, isAutoUpGrade = p.isAutoUpGrade, defaultSalesPriceType = p.defaultSalesPriceType, defaultSalesPriceNumber = p.defaultSalesPriceNumber, sortId = p.sortId, description = p.description, }).With(SqlWith.NoLock).ToPageListAsync(pageIndex, pageSize, totalCount); } else { page = await DbClient.Queryable() .OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WhereIF(predicate != null, predicate).Select(p => new CoreCmsAgentGrade { id = p.id, name = p.name, isDefault = p.isDefault, isAutoUpGrade = p.isAutoUpGrade, defaultSalesPriceType = p.defaultSalesPriceType, defaultSalesPriceNumber = p.defaultSalesPriceNumber, sortId = p.sortId, description = p.description, }).ToPageListAsync(pageIndex, pageSize, totalCount); } var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } #endregion } }