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 MsgAlertController : BaseController
    {
        public MsgAlertController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }




        #region 通用-自定义弹窗消息
        [Authorize]
        public JsonResult MsgAlert(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = MsgAlertDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> MsgAlertDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            var query = maindb.MsgAlert.Where(m => m.Status > 0).OrderByDescending(m => m.Sort).ToList();
            foreach (var item in query)
            {
                Dictionary<string, object> Obj = new Dictionary<string, object>();
                string[] AppointUsers = new string[] { };
                var UserIds = "";
                var info = maindb.UserSwapWhite.Where(m => m.QueryCount == item.Id).ToList();
                Obj.Add("MsgType", item.MsgType); //消息类型
                Obj.Add("Title", item.Title); //标题
                Obj.Add("Content", item.Content); //简介
                Obj.Add("EffectStartDate", item.EffectStartDate == null ? "" : item.EffectStartDate.Value.ToString("yyyy/MM/dd HH:mm:ss")); //有效开始时间
                Obj.Add("EffectEndDate", item.EffectEndDate == null ? "" : item.EffectEndDate.Value.ToString("yyyy/MM/dd HH:mm:ss")); //有效结束时间
                Obj.Add("BgPic", DefaultPic(item.BgPic)); //背景图片
                Obj.Add("UrlParam", item.UrlParam); //跳转Url参数
                Obj.Add("Url", item.Url); //跳转Url
                Obj.Add("BtnText", item.BtnText); //按钮文字
                Obj.Add("AlertFlag", item.SeoKeyword); //弹窗标识
                Obj.Add("IsWeekend", item.IsWeekend); //是否周末
                Obj.Add("TimeByDay", item.TimeByDay); //每日次数
                Obj.Add("IsAppoint", item.AlertFlag); //是否指定对象(1 是 0 否)
                if (info.Count > 0)
                {
                    foreach (var items in info)
                    {
                        UserIds += items.UserId + ",";
                    }
                    AppointUsers = UserIds.TrimEnd(',').Split(',');
                    Obj.Add("AppointUsers", AppointUsers); //弹窗对象
                }
                Obj.Add("Id", item.Id); //Id
                dataList.Add(Obj);
            }
            return dataList;
        }
        #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

    }
}
