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<MissiveCc> _repository;
        private readonly IMapper _mapper;
        private readonly IDistributedCache _cache;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public MissiveCcRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
        {
            _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<MissiveCc>(database);
            _mapper = mapper;
            _cache = cache;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<int> InsertAsync(MissiveCc missiveCc)
        {
            return await _repository.InsertAsync(missiveCc);
        }
        /// <summary>
        /// list 添加站内信抄送人
        /// </summary>
        /// <param name="missiveCcs"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(List<MissiveCc> 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<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
            var database = new Database(databaseType, _connectionString);
            var connection = database.GetConnection();
            var items = await connection.ExecuteScalarAsync<int>(sql);
            return items > 0;
        }
        public async Task<MissiveCc> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }

        public async Task<bool> UpdateAsync(MissiveCc missiveCc)
        {
            return await _repository.UpdateAsync(missiveCc);
        }

        public async Task<bool> DeleteAsync(Query query)
        {
            return await _repository.DeleteAsync(query) > 0;
        }

        public async Task<bool> UpdateAsync(Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }

        public async Task<IEnumerable<MissiveCc>> GetAllAsync(Query query)
        {
            return await _repository.GetAllAsync(query);
        }
    }
}
