using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using MineTec.ProManager.Commd;
using MineTec.ProManager.UserInfo.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MineTec.ProManager.UserInfo
{
    public class UserInfoAppService : AsyncCrudAppService<Entitys.UserInfo, UserInfoDto, Guid, PagedResultRequestDto,
                             CreateUpdateUserInfoDto, CreateUpdateUserInfoDto>, IUserInfoAppService

    {
        public UserInfoAppService(IRepository<Entitys.UserInfo, Guid> repository)
            : base(repository)
        {

        }

        public override Task<UserInfoDto> CreateAsync(CreateUpdateUserInfoDto input)
        {
            var sin = input;
            return base.CreateAsync(input);
        }

        public async Task<PagedResultDto<UserInfoDto>> GetLoginUser(GetLoginUser input)
        {
            var query = base.CreateFilteredQuery(input).WhereIf(input.Account.Length > 0, t => t.IsDelete == 0 && t.IsLeaving == 0 && t.Account == input.Account && t.PassWord == input.PassWord);
            var usercount = query.Count();
            var userlist = query.ToList();
            return new PagedResultDto<UserInfoDto>()
            {
                TotalCount = usercount,
                Items = ObjectMapper.Map<List<UserInfoDto>>(userlist)
            };
        }


        public async Task<UserInfoOutput> GetAllUsers(GetAllUsers input)
        {

            var result = new UserInfoOutput();
            try
            {
                var query = base.CreateFilteredQuery(input).WhereIf(!string.IsNullOrEmpty(input.Account), t => t.Account.Contains(input.Account)).WhereIf(!string.IsNullOrEmpty(input.UserName), t => t.UserName.Contains(input.UserName));
                var usercount = query.Count();
                var userlist = query.ToList();
                var list = ObjectMapper.Map<List<UserInfoDto>>(userlist);
                result.code = 0;
                result.count = usercount;
                result.data = list;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }

            return result;
        }
    }
}