/*
 * 后台管理员
 */

using System;
using System.Web;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using MySystem.Models.Bs;
using Library;
using LitJson;
using Microsoft.AspNetCore.Authorization;
using MySystem.Service.Bs;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class SysAdminController : BaseController
    {
        public SysAdminController(IHttpContextAccessor accessor) : base(accessor)
        {
        }

        #region 登录

        public JsonResult Login(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData jsonObj = JsonMapper.ToObject(value);
            string userName = jsonObj["userName"].ToString(); //账号
            string pwd = jsonObj["pwd"].ToString(); //密码
            SysAdmin sys = SysAdminService.Query(userName, function.MD5_32(pwd));
            if(sys.Id == 0)
            {
                return Json(new AppResultJson() { Status = "-1", Info = "账号或密码不正确" });
            }
            int RoleId = int.Parse(function.CheckInt(sys.Role));
            SysAdminRole Role = SysAdminRoleService.Query(RoleId);
            string RightInfo = function.CheckNull(Role.RightInfo);
            Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
            obj.Add("rightList", new AdminRightList().GetRight(sys.Role, RightInfo)); //权限列表
            obj.Add("apiToken", PublicFunction.AppToken(sys.AdminName)); //后台所有接口API所需的token
            obj.Add("apiTokenExpiredDate", DateTime.Now.AddDays(10).ToString("yyyy-MM-dd HH:mm:ss"));
            string token = dbconn.Encrypt3DES(sys.Id.ToString() + "-" + function.ConvertDateTimeInt(DateTime.Now));
            RefreshTokens check = RefreshTokensService.Query(sys.Id);
            if(check.UserId == 0)
            {
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("UserId", sys.Id);
                Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
                Fields.Add("RefreshToken", token);
                RefreshTokensService.Add(Fields);
            }
            else
            {
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
                Fields.Add("RefreshToken", token);
                RefreshTokensService.Edit(Fields, sys.Id);
            }
            List<string> roles = new List<string>();
            roles.Add(sys.Role);
            obj.Add("roles", roles);
            obj.Add("realName", sys.RealName);
            obj.Add("refreshToken", token); //主token,用于刷新apiToken
            AppConfig.LoginSession.sysId = sys.Id;
            AppConfig.LoginSession.sysAdminName = sys.AdminName;
            AppConfig.LoginSession.sysRealName = sys.RealName;
            return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
        }

        #endregion

        #region 刷新token

        public JsonResult RefreshToken(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData jsonObj = JsonMapper.ToObject(value);
            string refreshToken = jsonObj["refreshToken"].ToString(); //账号        
            Dictionary<string, object> obj = new Dictionary<string, object>(); //返回字段
            string[] data = dbconn.Decrypt3DES(refreshToken).Split('-');
            int Id = int.Parse(data[0]);
            SysAdmin sys = SysAdminService.Query(Id);
            if(sys.Id == 0)
            {
                return Json(new AppResultJson() { Status = "-1", Info = "刷新失败" });
            }
            obj.Add("apiToken", PublicFunction.AppToken(sys.AdminName)); //后台所有接口API所需的token
            obj.Add("apiTokenExpiredDate", DateTime.Now.AddDays(10));
            string token = dbconn.Encrypt3DES(sys.Id.ToString() + "-" + function.ConvertDateTimeInt(DateTime.Now));
            RefreshTokens check = RefreshTokensService.Query(sys.Id);
            if(check.UserId == 0)
            {
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("UserId", sys.Id);
                Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
                Fields.Add("RefreshToken", token);
                RefreshTokensService.Add(Fields);
            }
            else
            {
                Dictionary<string, object> Fields = new Dictionary<string, object>();
                Fields.Add("ExpiredDate", DateTime.Now.AddDays(10));
                Fields.Add("RefreshToken", token);
                RefreshTokensService.Edit(Fields, sys.Id);
            }
            obj.Add("refreshToken", token); //主token,用于刷新apiToken
            return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
        }

        #endregion

    }
}