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 MySystem.MainModels;
using LitJson;
using Library;

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




        #region 商户-提交意见反馈
        [Authorize]
        public JsonResult Add(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = AddDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson AddDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string Content = data["Content"].ToString(); //回复内容
            int BackKind = int.Parse(function.CheckInt(data["BackKind"].ToString())); //反馈类型
            int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //商户Id
            if (string.IsNullOrEmpty(data["Content"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写回复内容" };
            }
            if (data["Content"].ToString().Length > 100)
            {
                return new AppResultJson() { Status = "-1", Info = "回复内容最多100个字符" };
            }
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            maindb.UserBack.Add(new UserBack()
            {
                CreateDate = DateTime.Now, //创建时间
                UpdateDate = DateTime.Now, //修改时间
                Content = Content, //回复内容
                BackKind = BackKind, //反馈类型
                UserId = UserId,
            });
            maindb.SaveChanges();
            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

    }
}
