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

    {
        private readonly IRepository<Entitys.UserInfo, Guid> _UserRepository;

        public UserInfoAppService(IRepository<Entitys.UserInfo, Guid> repository, IRepository<Entitys.UserInfo, Guid> UserRepository)
            : base(repository)
        {
            _UserRepository = UserRepository;
        }

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

        public OutputBase CreateUser(CreateUpdateUserInfoDto input)
        {
            var result = new OutputBase();
            try
            {
                var entity = _UserRepository.GetAll().FirstOrDefault(a => a.Account == input.Account && a.IsDelete == 0);
                if (entity == null)
                {
                    _UserRepository.InsertAndGetId(ObjectMapper.Map<Entitys.UserInfo>(input));
                    //base.CreateAsync(input);
                    result.code = 1;//成功
                    result.msg = "保存成功！";
                }
                else
                {
                    result.code = 2;//重复
                    result.msg = "已经存在相同的用户账号，请修改！";
                }
            }
            catch (Exception e)
            {
                result.code = 0;//失败
                result.msg = e.Message;
            }
            return result;
        }

        public 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 UserInfoOutput GetAllUsers(GetAllUsers input)
        {

            var result = new UserInfoOutput();
            try
            {
                var query = base.CreateFilteredQuery(input).Where(t => t.IsDelete != 1 && t.IsLeaving != 1).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;
        }
    }
}