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 MerchantParamSetController : BaseController
    {
        public MerchantParamSetController(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);
            Dictionary<string, object> Obj = DetailDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
        }
        public Dictionary<string, object> DetailDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            MerchantParamSet query = MerchantParamSetDbconn.Instance.Get(Id);
            if (query == null)
            {
                query = new MerchantParamSet();
                query.IsAll = 1;
            }
            Obj.Add("IsAll", query.IsAll); //是否收全额
            Obj.Add("MinPayMoney", query.MinPayMoney); //订单参与门槛
            Obj.Add("GetPercent", query.GetPercent); //商家实收比例
            Obj.Add("ProfitDays", query.ProfitDays); //分红期限(天)
            Obj.Add("DiviPercent", query.DiviPercent); //最大分红比例
            Obj.Add("DiviPersons", query.DiviPersons); //单笔订单分红人数
            return Obj;
        }
        #endregion



        #region 商户-活动配置
        [Authorize]
        public JsonResult Set(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = SetDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        public AppResultJson SetDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int IsAll = int.Parse(function.CheckInt(data["IsAll"].ToString())); //是否收全额
            decimal MinPayMoney = decimal.Parse(function.CheckNum(data["MinPayMoney"].ToString())); //订单参与门槛
            decimal GetPercent = decimal.Parse(function.CheckNum(data["GetPercent"].ToString())); //商家实收比例
            int ProfitDays = int.Parse(function.CheckInt(data["ProfitDays"].ToString())); //分红期限(天)
            decimal DiviPercent = decimal.Parse(function.CheckNum(data["DiviPercent"].ToString())); //最大分红比例
            int DiviPersons = int.Parse(function.CheckInt(data["DiviPersons"].ToString())); //单笔订单分红人数
            string MobileCode = data["MobileCode"].ToString(); //短信验证码
            if (string.IsNullOrEmpty(data["IsAll"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写是否收全额" };
            }
            if (string.IsNullOrEmpty(data["MinPayMoney"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写订单参与门槛" };
            }
            if (string.IsNullOrEmpty(data["GetPercent"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写商家实收比例" };
            }
            if (string.IsNullOrEmpty(data["ProfitDays"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写分红期限(天)" };
            }
            if (!function.IsInt(data["ProfitDays"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写正确的分红期限(天)" };
            }
            if (string.IsNullOrEmpty(data["DiviPercent"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写最大分红比例" };
            }
            if (string.IsNullOrEmpty(data["DiviPersons"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写单笔订单分红人数" };
            }
            if (!function.IsInt(data["DiviPersons"].ToString()))
            {
                return new AppResultJson() { Status = "-1", Info = "请填写正确的单笔订单分红人数" };
            }
            int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
            MerchantInfo merchant = maindb.MerchantInfo.FirstOrDefault(m => m.Id == Id);
            MobileCodeCheck mobilecheck = RedisDbconn.Instance.Get<MobileCodeCheck>("MobileCodeCheck:" + merchant.Mobile);
            if (mobilecheck == null)
            {
                return new AppResultJson() { Status = "-1", Info = "短信验证码不正确" };
            }
            if (mobilecheck.CheckCode != MobileCode)
            {
                return new AppResultJson() { Status = "-1", Info = "短信验证码不正确" };
            }
            RedisDbconn.Instance.Clear("MobileCodeCheck:" + merchant.Mobile);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            MerchantParamSet query = maindb.MerchantParamSet.FirstOrDefault(m => m.Id == Id);
            if(query == null)
            {
                query = maindb.MerchantParamSet.Add(new MerchantParamSet()
                {
                    Id = Id,
                    CreateDate = DateTime.Now,
                }).Entity;
                maindb.SaveChanges();
            }
            query.UpdateDate = DateTime.Now; //修改时间
            query.IsAll = IsAll; //是否收全额
            query.MinPayMoney = MinPayMoney; //订单参与门槛
            query.GetPercent = GetPercent; //商家实收比例
            query.ProfitDays = ProfitDays; //分红期限(天)
            query.DiviPercent = DiviPercent; //最大分红比例
            query.DiviPersons = DiviPersons; //单笔订单分红人数
            maindb.SaveChanges();
            Obj.Add("UpdateDate", query.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //更新时间
            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

    }
}