//
namespace SafeCampus.System;
///
///
///
public class BatchEditService : DbRepository, IBatchEditService
{
private readonly ILogger _logger;
public BatchEditService(ILogger logger)
{
_logger = logger;
}
///
public async Task> Page(BatchEditPageInput input)
{
var query = Context.Queryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.ConfigId), it => it.ConfigId.Contains(input.ConfigId.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.EntityName), it => it.EntityName.Contains(input.EntityName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.TableName), it => it.TableName.Contains(input.TableName.Trim()))
//.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey))//根据关键字查询
.OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}");
var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
return pageInfo;
}
///
public async Task Add(BatchEditAddInput input)
{
var entity = input.Adapt();//实体转换
await CheckInput(entity);//检查参数
var tableColumns = SqlSugarUtils.GetTableColumns(input.ConfigId, input.TableName);//获取表的字段信息
var batchEdiConfig = new List();//代码生成配置字段集合
//遍历字段
tableColumns.ForEach(it =>
{
//判断是否是主键或者通用字段
var isPkOrCommon = it.IsPrimaryKey || SqlSugarUtils.IsCommonColumn(it.ColumnName);
if (!isPkOrCommon)
{
//添加到字段集合
batchEdiConfig.Add(GetUpdateBatchConfig(it));
}
});
//事务
var result = await Tenant.UseTranAsync(async () =>
{
entity = await InsertReturnEntityAsync(entity);//输入参数转实体并插入
batchEdiConfig.ForEach(it => { it.UId = entity.Id; });//遍历字段赋值基础Id
await Context.Insertable(batchEdiConfig).ExecuteCommandAsync();
});
if (!result.IsSuccess)//如果失败了
{
//写日志
_logger.LogError(result.ErrorMessage, result.ErrorException);
throw Oops.Oh(ErrorCodeEnum.A0003);
}
}
///
public async Task Config(List input)
{
var updateBatch = input.Adapt>();//实体转换
var configRep = ChangeRepository>();
var ids = input.Select(it => it.Id).ToList();//获取当前配置Id
if (ids.Any())
{
await configRep.DeleteAsync(it => !ids.Contains(it.Id) && it.UId == input.First().UId);//删除没有的
await Context.Updateable(updateBatch).ExecuteCommandAsync();//更新数据
}
}
///
public async Task Delete(BaseIdListInput input)
{
//获取所有ID
var ids = input.Ids;
if (ids.Count > 0)
{
//事务
var result = await Tenant.UseTranAsync(async () =>
{
await DeleteByIdsAsync(ids.Cast