﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using Cartinla.TaonyNet.Sys.Common;
using Cartinla.TaonyNet.Sys.Entity;
using DotNet.Framework.Utility;
using Webdiyer.WebControls.Mvc;

namespace Cartinla.TaonyNet.Sys.BLL
{
    public class UserService : IUserService
    {
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>返回用户对象</returns>
        public UserInfo GetUser(int userId)
        {
            using (var dbContext = new SysDbContext())
            {
                return dbContext.Users.FirstOrDefault(n => n.Id == userId);
            }
        }

        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns>验证是否成功</returns>
        public UserInfo ValidateUser(UserInfo user)
        {
            using (var dbContext = new SysDbContext())
            {
                string enPwd = EncryptUtil.MD5(user.LoginName + user.LoginPwd);
                return dbContext.FindList<UserInfo>().SingleOrDefault(n => n.LoginName == user.LoginName && n.LoginPwd == enPwd);
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Add(UserInfo user)
        {
            using (var dbContext = new SysDbContext())
            {
                dbContext.Users.Add(user);
                return dbContext.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Update(UserInfo user)
        {
            using (var dbContext = new SysDbContext())
            {
                dbContext.Users.AddOrUpdate(user);
                return dbContext.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<UserInfo> GetList(Expression<Func<UserInfo, bool>> where)
        {
            using (var dbContext = new SysDbContext())
            {
                return null;
            }
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public PagedList<UserInfo> GetPageList(int pageSize, int pageIndex, Expression<Func<UserInfo, bool>> where)
        {
            using (var dbContext = new SysDbContext())
            {
                if (null != where)
                {
                    return dbContext.Users.Where(where.Compile()).OrderByDescending(u => u.CreateTime).ToPagedList(pageIndex, pageSize);
                }
                return dbContext.Users.OrderByDescending(u => u.CreateTime).ToPagedList(pageIndex, pageSize);
            }
        }


        /// <summary>
        /// 验证登录名是否存在
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <returns>是否存在</returns>
        public bool ExistLoginName(string loginName)
        {
            using (var dbContext = new SysDbContext())
            {
                return dbContext.Users.Count(n => n.LoginName == loginName) > 0;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(int userId)
        {
            using (var dbContext = new SysDbContext())
            {
                var model = GetUser(userId);
                dbContext.Delete(model);
                return dbContext.SaveChanges() > 0;
            }
        }
    }
}
