using System;
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.Request.OrganizationUser;
using Microsoft.Extensions.Options;

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

        public async Task<PagedList<Entity.tede2.Organization.OrganizationUser>> GetSearchAllAsync(OrganizationUserSearchRequest request)
        {
            var query = Q.NewQuery();
            if (!string.IsNullOrEmpty(request.KeyWord))
            {
                query.OrWhereLike(nameof(Entity.tede2.Organization.OrganizationUser.UserName), $"%request.KeyWord%");
                query.OrWhereLike(nameof(Entity.tede2.Organization.OrganizationUser.LoginAccountNumber), $"%request.KeyWord%");
            }
            if (request.OrganizationId > 0)
                query.Where(nameof(Entity.tede2.Organization.OrganizationUser.OrganizationId), request.OrganizationId);
            var result = new PagedList<Entity.tede2.Organization.OrganizationUser>();
            result.Total = await _repository.CountAsync(query);
            result.Items = await _repository.GetAllAsync(query);
            return result;
        }
        public async Task<bool> UpdateAsync(OrganizationUserUpdateRequest request)
        {
            var model = await _repository.GetAsync(request.Id);
            if (!string.IsNullOrEmpty(request.LoginAccountNumber))
                model.LoginAccountNumber = request.LoginAccountNumber;
            if (!string.IsNullOrEmpty(request.UserName))
                model.UserName = request.UserName;
            if (!string.IsNullOrEmpty(request.Phone))
                model.Phone = request.Phone;
            if (!string.IsNullOrEmpty(request.MacAddress))
                model.MacAddress = request.MacAddress;
            if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
            {
                if (DateTime.TryParse(request.BeginTime, out var beginTme) && DateTime.TryParse(request.EndTime, out var endTime))
                {
                    model.BeginTime = beginTme;
                    model.EndTime = endTime;
                }
            }
            if (!string.IsNullOrEmpty(request.OpenTime))
            {
                if (DateTime.TryParse(request.BeginTime, out var OpenTime))
                    model.OpenTime = OpenTime;
            }
            if (request.OpenStatus > 0)
                model.OpenStatus = request.OpenStatus;
            if (request.IsClose > 0)
                model.IsClose = request.IsClose == 1;
            if (request.IsDisable > 0)
                model.IsDisable = request.IsDisable == 1;
            if (request.OrganizationId > 0)
                model.OrganizationId = request.OrganizationId;
            return await _repository.UpdateAsync(model);
        }

        public async Task<bool> InsertAsync(Entity.tede2.Organization.OrganizationUser model)
        {
            return await _repository.InsertAsync(model) > 0;
        }

        public async Task<IEnumerable<Entity.tede2.Organization.OrganizationUser>> GetAllAsync(List<int> ids)
        {
            var list = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.tede2.Organization.OrganizationUser.Id), ids));
            return list;
        }
        public async Task<bool> DeleteAsync(List<int> ids)
        {
            return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.tede2.Organization.OrganizationUser.Id), ids)) > 0;
        }

        public async Task<bool> UpdateDisableAsync(OrganizationUserDisableUpdateRequest request)
        {
            return await _repository.UpdateAsync(Q.WhereIn(nameof(Entity.tede2.Organization.OrganizationUser.Id), request.Ids).Set(nameof(Entity.tede2.Organization.OrganizationUser.IsDisable), request.IsDisable)) > 0;
        }
    }
}