using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Entity.Topic; using GxPress.Repository.Interface; using Microsoft.Extensions.Options; using Datory; using SqlKata; using Dapper; using System; using System.Linq; namespace GxPress.Repository.Implement { public class TopicAddresseeRepository : ITopicAddresseeRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public TopicAddresseeRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(TopicAddressee request) { return await _repository.InsertAsync(request); } /// /// 添加话题收件人 /// /// /// public async Task InsertAsync(IEnumerable request) { var sql = "INSERT INTO `ccpph`.`tede_topic_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`TopicId`,`UserId`,`UserName`,`IsRead`,`IsAdmin`,`IsUpload`)VALUES"; if (request.Count() == 0) return true; foreach (var item in request) { var isAdmin = item.IsAdmin ? 1 : 0; sql += $"('{Guid.NewGuid().ToString()}','{DateTime.Now.ToString()}','{DateTime.Now.ToString()}',{item.TopicId},{item.UserId},'',0,{isAdmin},0),"; } sql = sql.Remove(sql.Length - 1, 1); var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); return await connection.ExecuteScalarAsync(sql) > 0; } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } /// /// 根据topicId删除 /// /// /// public async Task DeletByTopicIdAsync(int topicId) { return await _repository.DeleteAsync(Q.Where(nameof(Entity.Topic.TopicAddressee.TopicId), topicId)) > 0; } public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } } }