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




        #region 消费者-分红记录
        [Authorize]
        public JsonResult List(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = ListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        public List<Dictionary<string, object>> ListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //消费者
            int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
            int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> query = new ConsumerProfitService().List(new List<FieldItem>(), " and ConsumerId=" + ConsumerId, PageNum, PageSize);
            foreach (var subdata in query)
            {
                MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(int.Parse(subdata["MerchantId"].ToString())) ?? new MerchantInfo();
                Dictionary<string, object> curData = new Dictionary<string, object>();
                curData.Add("GetMoney", subdata["GetMoney"].ToString()); //分红金额
                curData.Add("Id", subdata["Id"].ToString()); //Id
                curData.Add("CreateDate", subdata["CreateDate"].ToString()); //CreateDate
                curData.Add("MerchantName", merchant.Name); //商户名称
                curData.Add("OrderNo", subdata["SeoTitle"].ToString()); //订单号
                dataList.Add(curData);
            }
            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

    }
}
