using Fuel.Core.Entity;
using FuelServer.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuel.Application.Service
{
    public class UserService : IUserService
    {
        public readonly IFreeSql _fsql;
        public UserService(IFreeSql fsql)
        {
            _fsql = fsql;
        }
        public users ValidateCredentials(string username, string password)
        {
            return _fsql.Select<users>().Where(_ => _.Account == username && _.Password == password).First();
        }
        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="userId">1 超级管理员</param>
        /// <returns></returns>
        public List<string> GetUserPermissions(int userId = 1)
        {
            var Permission = _fsql.Select<users, AdUserRole, AdRolePermission, AdPermission>()
  .LeftJoin((a, b, c, d) => a.Id == b.UserId)
  .LeftJoin((a, b, c, d) => b.RoleId == c.RoleId)
  .LeftJoin((a, b, c, d) => c.PermissionId == d.Id)
  .Where((a, b, c, d) => a.Id == userId)
  .ToList((a, b, c, d) => new { d });
            var permissionList = new List<string>();
            foreach (var permission in Permission)
            {
                if(permission.d != null)
                permissionList.Add(permission.d.Code);
            }
            permissionList.Add("Nozzle:uploadNozzle:POST");
            permissionList.Add("Nozzle:DeleteNozzle:Delete");
            return permissionList;

        }
        public List<string> GetSitePermissions(string Appid)
        {
            var Permission = _fsql.Select<AdApply, AdRoleApply, AdRole,AdRolePermission, AdPermission>()
 .LeftJoin((a, b, c, d,e) => a.Id == b.OauthApplyID)
 .LeftJoin((a, b, c, d, e) => b.OauthRoleID == c.Id)
 .LeftJoin((a, b, c, d, e) => c.Id == d.RoleId)
  .LeftJoin((a, b, c, d, e) => d.PermissionId == e.Id)
 .Where((a, b, c, d, e) => a.Appid == Appid)
 .ToList((a, b, c, d, e) => new { e });
            var permissionList = new List<string>();
            foreach (var permission in Permission)
            {
                if (permission.e != null)
                    permissionList.Add(permission.e.Code);
            }
            permissionList.Add("Nozzle:uploadNozzle:POST");
            permissionList.Add("Nozzle:DeleteNozzle:Delete");
            return permissionList;
        }
        public Task<users> GetUsers()
        {
            return null;
        }
    }
}
