using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.MainModels;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("Api/v1/[controller]/[action]")]
    public class UserRankWhiteController : BaseController
    {
        public UserRankWhiteController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }




        #region 创客-首页-我的创客-创客当前职级信息
        [Authorize]
        public JsonResult Detail(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["Id"].ToString())); //用户
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            UserRankWhite query = maindb.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
            if (query != null)
            {
                Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //调整时间
                Obj.Add("ExpiredDate", query.UpdateDate == null ? "" : query.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //到期时间
                if (query.UpdateDate < DateTime.Now)
                { 
                    return Json(new AppResultJson() { Status = "-1", Info = "", Data = Obj });
                }
                return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
            }
            return Json(new AppResultJson() { Status = "-1", Info = "", Data = Obj });
        }
        #endregion



        #region 创客-首页-我的创客-设置职级
        [Authorize]
        public JsonResult SetLevel(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = SetLevelDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson SetLevelDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //用户
            int UserLevel = int.Parse(function.CheckInt(data["UserLevel"].ToString())); //职级
            string ExpireDate = data["ExpireDate"].ToString(); //到期时间
            DateTime time = DateTime.Parse(ExpireDate + "-01 00:00:00").AddMonths(1);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            UserRankWhite query = maindb.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
            if (query == null)
            {
                query = maindb.UserRankWhite.Add(new UserRankWhite()
                {
                    CreateDate = DateTime.Now, //设置时间
                    UpdateDate = time, //过期时间
                    Rank = UserLevel,
                    UserId = UserId, //用户
                    Id = UserId,
                }).Entity;
            }
            else
            {
                query.CreateDate = DateTime.Now; //设置时间
                query.UpdateDate = time; //过期时间
                query.Rank = UserLevel;
            }
            maindb.SaveChanges();
            Obj.Add("Id", query.Id); //Id
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion



        #region 检查签名是否合法，合法返回1，不合法返回提示信息

        /// <summary>
        /// 检查签名是否合法，合法返回1，不合法返回提示信息
        /// </summary>
        /// <param name="value">请求的参数(json字符串)</param>
        /// <param name="signField">要签名的字段</param>
        /// <returns></returns>
        private string CheckSign(string value, string[] signField)
        {
            JsonData json = JsonMapper.ToObject(value);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            for (int i = 0; i < signField.Length; i++)
            {
                dic.Add(signField[i], json[signField[i]].ToString());
            }
            string sign = json["sign"].ToString(); //客户端签名字符串
            return new Sign().sign(dic, sign);
        }

        #endregion

    }
}
