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

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

        #region 商户激活-银联激活商户记录
        [Authorize]
        public JsonResult UnionPayActRecord(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = UnionPayActRecordDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        private List<Dictionary<string, object>> UnionPayActRecordDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantName = data["MerchantName"].ToString(); //商户名称
            string MctNo = data["MctNo"].ToString(); //商户号
            string UpdateDate = data["UpdateDate"].ToString(); //激活时间
            string PayMode = data["PayMode"].ToString(); //支付方式(1 支付宝 2 微信)
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            string condition = "";
            if (!string.IsNullOrEmpty(MerchantName))
            {
                var query = MerchantAddInfoService.Query(" and CertMerchantName='" + MerchantName + "'");
                condition += " and MerchantId='" + query.Id + "'";
            }
            if (!string.IsNullOrEmpty(MctNo))
            {
                var query = MerchantAddInfoService.Query(" and MchtNo='" + MctNo + "'");
                condition += " and MerchantId='" + query.Id + "'";
            }
            if (!string.IsNullOrEmpty(data["UpdateDate"].ToString()))
            {
                string[] datelist = UpdateDate.Split(new string[] { " - " }, StringSplitOptions.None);
                string start = datelist[0];
                string end = datelist[1];
                condition += " and UpdateDate>='" + start + " 00:00:00' and UpdateDate<='" + end + " 23:59:59'";
            }
            if (!string.IsNullOrEmpty(data["PayMode"].ToString()))
            {
                condition += " and Sort='" + PayMode + "'";
            }
            List<RelationData> relationData = new List<RelationData>();
            List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
            Other = new Dictionary<string, object>();
            int count = 0;
            List<Dictionary<string, object>> source = MerchantDepositOrderService.List(relationData, condition, out count, pageNum, pageSize);
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                var query = MerchantAddInfoService.Query(int.Parse(subdata["MerchantId"].ToString()));
                curData.Add("Id", subdata["Id"].ToString()); //Id
                curData.Add("MerchantName", query.CertMerchantName); //商户名称
                curData.Add("MctNo", subdata["MchtNo"].ToString()); //商户号
                curData.Add("UpdateDate", subdata["UpdateDate"].ToString() == "" ? "" : DateTime.Parse(subdata["UpdateDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //激活时间
                curData.Add("PayMode", int.Parse(subdata["Sort"].ToString())); //支付方式(1 支付宝 2 微信)
                curData.Add("ActPayPrice", decimal.Parse(subdata["ActPayPrice"].ToString())); //支付金额

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



    }
}
