using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class UserService : Repository<Models.User>
    {
        public List<User> GetListNav()
        {
            return base.AsSugarClient().Queryable<User>()
                .Includes(m => m.RoleInfo, info => info.RightList)
                .ToList();
        }
        public Models.User GetModel(string userCode,string userPw)
        {
            //return base.GetFirst(t=>t.Code.Equals(userCode) && SqlFunc.IsNull(t.Password,"") .Equals(userPw));  
            return base.AsSugarClient().Queryable<User>()
                .Includes(m => m.RoleInfo, info => info.RightList)
                .Where(t => t.Code.Equals(userCode) && SqlFunc.IsNull(t.Password, "").Equals(userPw))
                .First();
        }
        public bool ModifyPw(int userId,string newPW)
        {
            //更新一个字段
            var result = base.AsSugarClient().Updateable<User>().SetColumns("Password", newPW).Where("id=" + userId ).ExecuteCommand();
            //更新多个字段
            //var result = base.AsSugarClient().Updateable<User>().SetColumns(p => new User { Password = newPW, Name = Name }).Where("id=" + userId).ExecuteCommand();
            return result>0;
        }
        //获取所有子
        public List<Role> GetRoleItems()
        {
            var db = base.Change<Role>();//切换仓仓（新功能）
            return db.GetList();
        } 
        //分页
        //public List<Models.User> GetOrderPage(Expression<Func<Models.User, bool>> where, int pagesize, int pageindex)
        //{
        //    return base.GetPageList(where, new SqlSugar.PageModel() { PageIndex = pageindex, PageSize = pagesize }); //使用自已的仓储方法
        //}

        //调用仓储扩展方法
        public List<Models.User> GetOrderByJson(string Json)
        {
            return base.CommQuery(Json);
        }
    }
}
