using JNPF.Common.Configuration; using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.LinqBuilder; using JNPF.Systems.Entitys.Dto.DbBackup; using JNPF.Systems.Entitys.System; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace JNPF.Systems; /// /// 数据备份 /// 版 本:V3.2 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2021-06-01. /// [ApiDescriptionSettings(Tag = "System", Name = "DataBackup", Order = 207)] [Route("api/system/[controller]")] public class DbBackupService : IDynamicApiController, ITransient { /// /// 服务基础仓储. /// private readonly ISqlSugarRepository _repository; /// /// 用户管理. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public DbBackupService( ISqlSugarRepository repository, IUserManager userManager) { _repository = repository; _userManager = userManager; } #region GET /// /// 列表. /// /// 请求参数. /// [HttpGet("")] public async Task GetList([FromQuery] PageInputBase input) { var queryWhere = LinqExpression.And(); if (!string.IsNullOrEmpty(input.keyword)) queryWhere = queryWhere.And(m => m.FileName.Contains(input.keyword) || m.FilePath.Contains(input.keyword)); var list = await _repository.AsQueryable().Where(queryWhere).OrderBy(x => x.CreatorTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize); var pageList = new SqlSugarPagedList() { list = list.list.Adapt>(), pagination = list.pagination }; return PageResult.SqlSugarPageResult(pageList); } #endregion #region POST /// /// 创建备份(不支持跨库备份). /// /// [HttpPost("")] public async Task Create() { await DbBackup(); } /// /// 删除. /// /// 主键值. /// [HttpDelete("{id}")] public async Task Delete(string id) { if (await _repository.IsAnyAsync(m => m.Id == id && m.DeleteMark == null)) throw Oops.Oh(ErrorCode.COM1005); await _repository.AsUpdateable().SetColumns(it => new DbBackupEntity() { DeleteTime = DateTime.Now, DeleteMark = 1, DeleteUserId = _userManager.UserId }).Where(it => it.Id.Equals(id)).ExecuteCommandAsync(); } #endregion #region PrivateMethod /// /// 备份数据. /// private async Task DbBackup() { var fileName = SnowflakeIdHelper.NextId() + ".bak"; var filePath = Path.Combine(FileVariable.DataBackupFilePath, fileName); // 备份数据 var dataBase = App.Configuration["ConnectionStrings:DBName"]; _repository.AsSugarClient().DbMaintenance.BackupDataBase(dataBase, filePath); // 备份记录 DbBackupEntity entity = new DbBackupEntity(); entity.Id = SnowflakeIdHelper.NextId(); entity.CreatorTime = DateTime.Now; entity.CreatorUserId = _userManager.UserId; entity.EnabledMark = 1; entity.FileName = fileName; entity.FilePath = "/api/Common/Download?encryption=" + _userManager.UserId + "|" + fileName + "|dataBackup"; entity.FileSize = new FileInfo(filePath).Length.ToString(); await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnEntityAsync(); } /// /// 还原. /// /// 路径 private void DbRestore(string disk) { var dataBase = App.Configuration["ConnectionStrings:DBName"]; _repository.AsSugarClient().DbMaintenance.CreateDatabase(dataBase, disk); } #endregion }