using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.VipEquity;
using GxPress.Request.VipEquity;
using GxPress.Result.VipEquity;
using Microsoft.Extensions.Options;

namespace GxPress.Repository.Implement.VipEquity
{
    public class VipEquityRepository : IVipEquityRepository
    {
        private readonly Repository<Entity.tede2.VipEquity.VipEquity> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public VipEquityRepository(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.tede2.VipEquity.VipEquity>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        public async Task<IEnumerable<VipEquityResult>> GetAllAsync()
        {
            var result = await _repository.GetAllAsync(Q.OrderByDesc(nameof(Entity.tede2.VipEquity.VipEquity.CreatedDate)));
            var items = result.Select(n => _mapper.Map<VipEquityResult>(n)).ToList();
            foreach (var item in items)
            {
                item.VipTypeValue = ((GxPress.EnumConst.VipCardTypeConst)item.VipType).GetDescriptionOriginal();
            }
            return items;
        }

        public async Task<bool> UpdateAsync(VipEquityUpdateRequest request)
        {
            var model = await _repository.GetAsync(request.Id);
            if (request.IosPrice > 0)
                model.IosPrice = request.IosPrice;
            if (request.OtherPrice > 0)
                model.OtherPrice = request.OtherPrice;
            if (request.VipType > 0)
                model.VipType = request.VipType;
            return await _repository.UpdateAsync(model);
        }
        public async Task<bool> InsertAsync(Entity.tede2.VipEquity.VipEquity model)
        {
            return await _repository.InsertAsync(model) > 0;
        }
        /// <summary>
        /// 根据Id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }
        /// <summary>
        /// /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.tede2.VipEquity.VipEquity> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }
    }
}