using LangGuan.Command.Model;
using LangGuan.Command.Model.EntityModel;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace LangGuan.Services
{
    /// 
    /// 
    /// 
    public class EmployeeService : BaseService
    {
        /// 
        /// 
        /// 
        /// 
        public EmployeeService(IConfiguration config) : base(config, nameof(Employee))
        {
        }
        /// 
        /// 登陆
        /// 
        /// 
        /// 
        /// 
        public async Task Login(string Account, string Pwd)
        {
            Expression> expression = filter => filter.Account == Account && filter.Pwd == Pwd;
            var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
            if (entity != null) return new ApiResult() { code = 0, data = new { account = Account, nickName = entity.NickName }, msg = "" };
            else
                return new ApiResult() { code = 1, msg = "登陆失败,请检查用户名跟密码" };
        }
        /// 
        /// 更新密码
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task Update(string Account, string Pwd, string NewPwd)
        {
            Expression> expression = filter => filter.Account == Account && filter.Pwd == Pwd;
            var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
            if (entity != null)
            {
                entity.Pwd = NewPwd;
                await base.UpdateAsync(entity.Id, entity);
                return new ApiResult() { code = 0, msg = "" };
            }
            return new ApiResult() { code = 1, msg = "旧密码有误" };
        }
        /// 
        /// 新加用户
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task Add(string Account, string Pwd, string NickName)
        {
            Expression> expression = filter => filter.Account == Account;
            var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault();
            if (entity != null)
            {
                return new ApiResult() { code = 1, msg = $"已经存在{Account},无法重复添加" };
            }
            else
            {
                await base.CreateAsync(new Employee()
                {
                    Account = Account,
                    Pwd = Pwd,
                    NickName = NickName
                });
                return new ApiResult() { code = 0, msg = "" };
            }
        }
        /// 
        /// 列表页
        /// 
        /// 
        /// 
        public async Task GetList(RqeustPaging request)
        {
            request.pageSize = request.pageSize == 0 ? 10 : request.pageSize;
            Expression> exp = num => true;
            var query = await base.GetListWithExp(exp);
            var total = query.Count();
            var items = query.OrderByDescending(s => s.CreateDateTime)
                .Skip(request.pageSize * (request.current - 1)).Take(request.pageSize)
                .Select(s => new { s.NickName, s.Account, s.Id }).ToList();
            return new ApiResult()
            {
                code = 0,
                data = new { total = total, items = items }
            };
        }
    }
}