12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.SystemLabel;
- using GxPress.Request.SystemLabel;
- using Microsoft.Extensions.Options;
- using System.Transactions;
- using Dapper;
- namespace GxPress.Repository.Implement.SystemLabel
- {
- public class SystemLableMediaRepository : ISystemLableMediaRepository
- {
- private readonly Repository<Entity.SystemLabel.SystemLableMedia> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- private readonly Repository<Entity.SystemLabel.SystemLableMedia> mediaLableRepository;
- public SystemLableMediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType =
- StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Entity.SystemLabel.SystemLableMedia>(database);
- _mapper = mapper;
- mediaLableRepository = new Repository<Entity.SystemLabel.SystemLableMedia>(database);
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<Entity.SystemLabel.SystemLableMedia> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- /// <summary>
- /// 根据标签ID获取媒体ID
- /// </summary>
- /// <param name="lableId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetMediaIdsAsync(int lableId, int typeValue)
- {
- return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.SystemLabel.SystemLableMedia.LableId), lableId).Where(nameof(Entity.SystemLabel.SystemLableMedia.TypeValue), typeValue).Select(nameof(Entity.SystemLabel.SystemLableMedia.MediaId)));
- }
- /// <summary>
- /// 根据分类获取媒体ID
- /// </summary>
- /// <param name="lableId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetMediaIdsByTypeValueAsync(int typeValue)
- {
- return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.SystemLabel.SystemLableMedia.TypeValue), typeValue).Select(nameof(Entity.SystemLabel.SystemLableMedia.MediaId)));
- }
- public async Task<bool> DeleteAsync(List<int> ids)
- {
- return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLableMedia.Id), ids)) > 0;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> InsertAsync(SystemLableMediaRequest request)
- {
- try
- {
- using (var transactions = new TransactionScope())
- {
- await _repository.DeleteAsync(Q.Where(nameof(Entity.SystemLabel.SystemLableMedia.LableId), request.LableId));
- var sql = $@"INSERT INTO `tede_system_lable_media`(`Guid`,`CreatedDate`,`LastModifiedDate`,`LableId`,`MediaId`,`TypeValue`) VALUES";
- var createdDate = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
- foreach (var item in request.MediaIds)
- {
- var guid = System.Guid.NewGuid().ToString();
- sql += $@"('{guid}','{createdDate}','{createdDate}',{request.LableId},{item},{request.TypeValue}),";
- }
- sql = sql.Remove(sql.Length - 1, 1);
- var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- await connection.ExecuteAsync(sql);
- transactions.Complete();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- }
- }
|