using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Entity.Missive; using GxPress.Repository.Interface.Missive; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; using SqlKata; namespace GxPress.Repository.Implement.Missive { public class MissiveCcRepository : IMissiveCcRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly IDistributedCache _cache; private readonly string _connectionString; private readonly string _databaseTypeStr; public MissiveCcRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper, IDistributedCache cache) { _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; _cache = cache; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(MissiveCc missiveCc) { return await _repository.InsertAsync(missiveCc); } /// /// list 添加站内信抄送人 /// /// /// public async Task InsertAsync(List missiveCcs) { if (missiveCcs.Count == 0) return false; var sql = "INSERT INTO `ccpph`.`tede_missive_cc`(`Guid`,`CreatedDate`,`LastModifiedDate`,`MissiveId`,`UserId`,`UserName`)VALUES"; foreach (var item in missiveCcs) { var guId = Guid.NewGuid().ToString(); var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); sql += $"('{guId}','{createdDate}','{createdDate}',{item.MissiveId},{item.UserId},''),"; } sql = sql.Remove(sql.Length - 1, 1); var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection.ExecuteScalarAsync(sql); return items > 0; } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task UpdateAsync(MissiveCc missiveCc) { return await _repository.UpdateAsync(missiveCc); } public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } public async Task UpdateAsync(Query query) { return await _repository.UpdateAsync(query) > 0; } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } } }