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.Models.Main1;
using MySystem.Service.Main1;
using LitJson;
using Library;

namespace MySystem.Areas.Api.Controllers.v1.Main1
{
    [Area("Api")]
    [Route("/v1/QrCodePlateMain/[controller]/[action]")]
    public class MerchantParamSetRecordController : BaseController
    {
        public MerchantParamSetRecordController(IHttpContextAccessor accessor) : base(accessor)
        {
        }


        #region 商户管理-商户管理-直连商户活动变更记录
        [Authorize]
        public JsonResult DirectMerchantParamSetRecordList(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            List<Dictionary<string, object>> dataList = DirectMerchantParamSetRecordListDo(value);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
        }
        private List<Dictionary<string, object>> DirectMerchantParamSetRecordListDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantId = data["MerchantId"].ToString(); //商户Id
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            string condition = "";
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            List<Dictionary<string, object>> source = MerchantParamSetRecordService.List(relationData, condition, pageNum, pageSize);
            var merSet = MerchantParamSetService.Query(int.Parse(MerchantId));
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                var Id = int.Parse(subdata["Id"].ToString()); //变更记录Id
                curData.Add("Id", Id); //变更记录Id
                var status = -1;
                if (merSet.Version == Id && merSet.IsAll == 0)
                {
                    status = 1;
                }
                if (subdata["BeforeIsAll"].ToString() == "0" && subdata["AfterIsAll"].ToString() == "1")
                {
                    status = 0;
                }
                if (merSet.Version != Id && subdata["BeforeIsAll"].ToString() == "0" && subdata["AfterIsAll"].ToString() == "0")
                {
                    status = -1;
                }
                curData.Add("Status", int.Parse(subdata["Status"].ToString())); //活动状态(1 使用中 -1 已失效 0 已关闭)
                curData.Add("MerchantName", MerchantAddInfoService.Query(int.Parse(subdata["MerchantId"].ToString())).CertMerchantName); //商户名称
                curData.Add("CreateDate", subdata["CreateDate"].ToString() == "" ? "" : DateTime.Parse(subdata["CreateDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //最后操作时间
                curData.Add("GetPercent", subdata["AfterGetPercent"].ToString()); //实收比例
                curData.Add("DiviPersons", subdata["AfterDiviPersons"].ToString()); //分红人数
                curData.Add("ProfitDays", subdata["AfterProfitDays"].ToString()); //分红天数
                curData.Add("MinPayMoney", subdata["AfterMinPayMoney"].ToString()); //参与门槛
                curData.Add("DiviPercent", subdata["AfterDiviPercent"].ToString()); //最大分红比例

                dataList.Add(curData);
            }
            return dataList;
        }
        #endregion

    }
}
