using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using Microsoft.Extensions.Options;
using Datory;
using System;
using Dapper;

namespace GxPress.Repository.Implement
{
    public class NoticeCcRepository : INoticeCcRepository
    {
        private readonly Repository<NoticeCc> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypeStr;
        public NoticeCcRepository(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<NoticeCc>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 根据通知ID获取抄送人
        /// </summary>
        /// <param name="noticeId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<NoticeCc>> GetNoticeCcByNoticeIdAsync(int noticeId)
        {
            return await _repository.GetAllAsync(Q.Where(nameof(NoticeCc.NoticeId), noticeId));
        }

        public async Task<bool> DeleteAsync(int noticeId)
        {
            return await _repository.DeleteAsync(Q.Where(nameof(NoticeCc.NoticeId), noticeId)) > 0;
        }

        public async Task<int> InsertAsync(NoticeCc noticeCc)
        {
            return await _repository.InsertAsync(noticeCc);
        }
        /// <summary>
        /// 添加通知抄送人
        /// </summary>
        /// <param name="noticeCcs"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(List<NoticeCc> noticeCcs)
        {
            if (noticeCcs.Count == 0)
                return false;
            var sql = "INSERT INTO `ccpph`.`tede_notice_cc`(`Guid`,`CreatedDate`,`LastModifiedDate`,`NoticeId`,`UserId`,`UserName`,`IsRead`,`NoticeCcType`)VALUES";
            foreach (var item in noticeCcs)
            {
                var guId = Guid.NewGuid().ToString();
                var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var isRead = item.IsRead == true ? 1 : 0;
                sql += $"('{guId}','{createdDate}','{createdDate}',{item.NoticeId},{item.UserId},'',{isRead},{item.NoticeCcType.GetHashCode()}),";
            }
            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;
        }
    }
}
