using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.Organization;
using GxPress.Result.Organization;
using Microsoft.Extensions.Options;
using System;
namespace GxPress.Repository.Implement.Organization
{
    public class OrganizationRepository : IOrganizationRepository
    {
        private readonly Repository<Entity.tede2.Organization.Organization> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public OrganizationRepository(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.Organization.Organization>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        ///  机构列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedList<Entity.tede2.Organization.Organization>> GetAllAsync(OrganizationListRequest request)
        {
            var result = new PagedList<Entity.tede2.Organization.Organization>();
            var query = Q.NewQuery();
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.WhereLike(nameof(Entity.tede2.Organization.Organization.Name), $"%{request.KeyWord}%");
            result.Total = await _repository.CountAsync(query);
            result.Items = await _repository.GetAllAsync(query.OrderByDesc(nameof(Entity.tede2.Organization.Organization.CreatedDate)).ForPage(request.Page, request.PerPage));
            return result;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(OrganizationUpdateRequest request)
        {
            var organization = await _repository.GetAsync(request.Id);
            if (request.IsClose > 0)
                organization.IsClose = request.IsClose == 1;
            if (request.IsDisable > 0)
                organization.IsDisable = request.IsDisable == 1;
            if (request.FacilityCount > 0)
                organization.FacilityCount = request.FacilityCount;
            if (!string.IsNullOrEmpty(request.LinkName))
                organization.LinkName = request.LinkName;
            if (!string.IsNullOrEmpty(request.LinkPhone))
                organization.LinkPhone = request.LinkPhone;
            if (!string.IsNullOrEmpty(request.Address))
                organization.Address = request.Address;
            if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
            {
                organization.BeginTime = Convert.ToDateTime(request.BeginTime);
                organization.EndTime = Convert.ToDateTime(request.EndTime);
            }
            if (!string.IsNullOrEmpty(request.OpenTime))
                organization.OpenTime = Convert.ToDateTime(request.OpenTime);
            if (request.OpenStatus > 0)
                organization.OpenStatus = request.OpenStatus;
            return await _repository.UpdateAsync(organization);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(List<int> ids)
        {
            return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.tede2.Organization.Organization.Id), ids)) > 0;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> UpdateIsDisableAsync(OrganizationUpdateDisableRequest request)
        {
            return await _repository.UpdateAsync(Q.WhereIn(nameof(Entity.tede2.Organization.Organization.Id), request.Ids).Set(nameof(Entity.tede2.Organization.Organization.IsDisable), request.IsDisable)) > 0;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(Entity.tede2.Organization.Organization model)
        {
            return await _repository.InsertAsync(model) > 0;
        }
    }
}