/***********************************************************************
* 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.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.UI;
using Microsoft.AspNetCore.Mvc;
namespace CoreCms.Net.Repository
{
///
/// 菜单表 接口实现
///
public class SysMenuRepository : BaseRepository, ISysMenuRepository
{
public SysMenuRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
#region 实现重写增删改查操作==========================================================
///
/// 重写异步插入方法
///
/// 实体数据
///
public async Task InsertAsync(SysMenu 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(SysMenu 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.parentId = entity.parentId;
oldModel.menuName = entity.menuName;
oldModel.menuIcon = entity.menuIcon;
oldModel.path = entity.path;
oldModel.component = entity.component;
oldModel.menuType = entity.menuType;
oldModel.sortNumber = entity.sortNumber;
oldModel.authority = entity.authority;
oldModel.target = entity.target;
oldModel.iconColor = entity.iconColor;
oldModel.hide = entity.hide;
//oldModel.deleted = entity.deleted;
oldModel.createTime = entity.createTime;
oldModel.updateTime = entity.updateTime;
oldModel.identificationCode = entity.identificationCode;
//事物处理过程结束
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(int id)
{
var jm = new AdminUiCallBack();
var all = await DbClient.Queryable().ToListAsync();
var model = all.Find(p => p.id == id);
if (model == null)
{
jm.msg = GlobalConstVars.DataisNo;
return jm;
}
var ids = new List() { id };
GetIds(all, id, ids);
var bl = await DbClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync();
jm.code = bl ? 0 : 1;
jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
return jm;
}
#endregion
///
///获取下级所有数据序列
///
///
///
///
///
private List GetIds(List data, int parentId, List ids)
{
var childs = data.Where(p => p.parentId == parentId).ToList();
foreach (var item in childs)
{
ids.Add(item.id);
if (data.Exists(p => p.parentId == item.id))
{
ids = GetIds(data, item.id, ids);
}
}
return ids;
}
}
}