平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

215 lines
7.8 KiB

  1. //
  2. namespace SafeCampus.System;
  3. /// <summary>
  4. /// <inheritdoc cref="IBatchEditService"/>
  5. /// </summary>
  6. public class BatchEditService : DbRepository<BatchEdit>, IBatchEditService
  7. {
  8. private readonly ILogger<BatchEditService> _logger;
  9. public BatchEditService(ILogger<BatchEditService> logger)
  10. {
  11. _logger = logger;
  12. }
  13. /// <inheritdoc/>
  14. public async Task<SqlSugarPagedList<BatchEdit>> Page(BatchEditPageInput input)
  15. {
  16. var query = Context.Queryable<BatchEdit>()
  17. .WhereIF(!string.IsNullOrWhiteSpace(input.ConfigId), it => it.ConfigId.Contains(input.ConfigId.Trim()))
  18. .WhereIF(!string.IsNullOrWhiteSpace(input.EntityName), it => it.EntityName.Contains(input.EntityName.Trim()))
  19. .WhereIF(!string.IsNullOrWhiteSpace(input.TableName), it => it.TableName.Contains(input.TableName.Trim()))
  20. //.WhereIF(!string.IsNullOrEmpty(input.SearchKey), it => it.Name.Contains(input.SearchKey))//根据关键字查询
  21. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}");
  22. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  23. return pageInfo;
  24. }
  25. /// <inheritdoc />
  26. public async Task Add(BatchEditAddInput input)
  27. {
  28. var entity = input.Adapt<BatchEdit>();//实体转换
  29. await CheckInput(entity);//检查参数
  30. var tableColumns = SqlSugarUtils.GetTableColumns(input.ConfigId, input.TableName);//获取表的字段信息
  31. var batchEdiConfig = new List<BatchEditConfig>();//代码生成配置字段集合
  32. //遍历字段
  33. tableColumns.ForEach(it =>
  34. {
  35. //判断是否是主键或者通用字段
  36. var isPkOrCommon = it.IsPrimaryKey || SqlSugarUtils.IsCommonColumn(it.ColumnName);
  37. if (!isPkOrCommon)
  38. {
  39. //添加到字段集合
  40. batchEdiConfig.Add(GetUpdateBatchConfig(it));
  41. }
  42. });
  43. //事务
  44. var result = await Tenant.UseTranAsync(async () =>
  45. {
  46. entity = await InsertReturnEntityAsync(entity);//输入参数转实体并插入
  47. batchEdiConfig.ForEach(it => { it.UId = entity.Id; });//遍历字段赋值基础Id
  48. await Context.Insertable(batchEdiConfig).ExecuteCommandAsync();
  49. });
  50. if (!result.IsSuccess)//如果失败了
  51. {
  52. //写日志
  53. _logger.LogError(result.ErrorMessage, result.ErrorException);
  54. throw Oops.Oh(ErrorCodeEnum.A0003);
  55. }
  56. }
  57. /// <inheritdoc />
  58. public async Task Config(List<BatchEditConfigInput> input)
  59. {
  60. var updateBatch = input.Adapt<List<BatchEditConfig>>();//实体转换
  61. var configRep = ChangeRepository<DbRepository<BatchEditConfig>>();
  62. var ids = input.Select(it => it.Id).ToList();//获取当前配置Id
  63. if (ids.Any())
  64. {
  65. await configRep.DeleteAsync(it => !ids.Contains(it.Id) && it.UId == input.First().UId);//删除没有的
  66. await Context.Updateable(updateBatch).ExecuteCommandAsync();//更新数据
  67. }
  68. }
  69. /// <inheritdoc />
  70. public async Task Delete(BaseIdListInput input)
  71. {
  72. //获取所有ID
  73. var ids = input.Ids;
  74. if (ids.Count > 0)
  75. {
  76. //事务
  77. var result = await Tenant.UseTranAsync(async () =>
  78. {
  79. await DeleteByIdsAsync(ids.Cast<object>().ToArray());//删除数据
  80. await Context.Deleteable<BatchEditConfig>().Where(it => ids.Contains(it.UId)).ExecuteCommandAsync();
  81. });
  82. if (!result.IsSuccess)//如果失败了
  83. {
  84. //写日志
  85. _logger.LogError(result.ErrorMessage, result.ErrorException);
  86. throw Oops.Oh(ErrorCodeEnum.A0003);
  87. }
  88. }
  89. }
  90. /// <inheritdoc />
  91. public async Task SyncColumns(BaseIdInput input)
  92. {
  93. var config = await GetFirstAsync(it => it.Id == input.Id);
  94. if (config != null)
  95. {
  96. var newColumns = new List<BatchEditConfig>();
  97. //获取表的字段信息
  98. var tableColumns = SqlSugarUtils.GetTableColumns(config.ConfigId, config.TableName);
  99. //找到当前配置字段列表
  100. var batchEdiConfig = await Context.Queryable<BatchEditConfig>().Where(it => it.UId == config.Id).ToListAsync();
  101. foreach (var tableColumn in tableColumns)
  102. {
  103. //判断是否是主键或者通用字段
  104. var isPkOrCommon = tableColumn.IsPrimaryKey || SqlSugarUtils.IsCommonColumn(tableColumn.ColumnName);
  105. if (!isPkOrCommon)
  106. {
  107. //如果当前配置没有
  108. if (!batchEdiConfig.Any(it => it.ColumnName == tableColumn.ColumnName))
  109. {
  110. var netType = SqlSugarUtils.ConvertDataType(tableColumn.DataType);
  111. //添加到字段集合
  112. newColumns.Add(GetUpdateBatchConfig(tableColumn));
  113. }
  114. }
  115. }
  116. if (newColumns.Count > 0)
  117. {
  118. newColumns.ForEach(it => it.UId = config.Id);
  119. await Context.Insertable(newColumns).ExecuteCommandAsync();//插入新的字段数据
  120. }
  121. }
  122. }
  123. /// <inheritdoc />
  124. public async Task<List<BatchEditConfig>> Columns(string code)
  125. {
  126. var batchEdiConfig = new List<BatchEditConfig>();
  127. var updateBatch = await GetFirstAsync(it => it.Code == code);//根据code获取配置
  128. if (updateBatch != null)
  129. {
  130. //找到对应字段
  131. batchEdiConfig = await Context.Queryable<BatchEditConfig>().Where(it => it.UId == updateBatch.Id && it.Status == CommonStatusConst.ENABLE)
  132. .ToListAsync();
  133. }
  134. return batchEdiConfig;
  135. }
  136. /// <inheritdoc/>
  137. public List<SqlSugarTableInfo> GetTables()
  138. {
  139. return SqlSugarUtils.GetTablesByAttribute<BatchEditAttribute>();
  140. }
  141. /// <inheritdoc/>
  142. public async Task<List<BatchEditConfig>> ConfigList(BaseIdInput input)
  143. {
  144. return await Context.Queryable<BatchEditConfig>().Where(u => u.UId == input.Id).OrderByDescending(it => it.Status).ToListAsync();
  145. }
  146. /// <inheritdoc/>
  147. public async Task<Dictionary<string, object>> GetUpdateBatchConfigDict(string code, List<BatchEditColumn> columns)
  148. {
  149. var dic = new Dictionary<string, object>();
  150. var configs = await Columns(code);
  151. foreach (var item in columns)
  152. {
  153. var config = configs.Where(it => it.ColumnName == item.TableColumn).FirstOrDefault();
  154. if (config == null) throw Oops.Bah("不存在的列");
  155. dic.Add(item.TableColumn, item.ColumnValue.ToString());
  156. }
  157. return dic;
  158. }
  159. #region 方法
  160. /// <summary>
  161. /// 获取配置
  162. /// </summary>
  163. /// <param name="columnInfo"></param>
  164. private BatchEditConfig GetUpdateBatchConfig(SqlSugarColumnInfo columnInfo)
  165. {
  166. var netType = SqlSugarUtils.ConvertDataType(columnInfo.DataType);
  167. return new BatchEditConfig
  168. {
  169. ColumnName = columnInfo.ColumnName,
  170. ColumnComment = string.IsNullOrWhiteSpace(columnInfo.ColumnDescription) ? columnInfo.ColumnName : columnInfo.ColumnDescription,
  171. NetType = netType,
  172. DataType = SqlSugarUtils.DataTypeToEff(netType),
  173. Status = CommonStatusConst.DISABLED
  174. };
  175. }
  176. /// <summary>
  177. /// 检查输入参数
  178. /// </summary>
  179. /// <param name="updateBatch"></param>
  180. private async Task CheckInput(BatchEdit updateBatch)
  181. {
  182. if (updateBatch.Id == 0)
  183. {
  184. var isExist = await IsAnyAsync(it => it.Code == updateBatch.Code);
  185. if (isExist) throw Oops.Bah("唯一编码不能重复");
  186. }
  187. }
  188. #endregion 方法
  189. }