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 MerchantInfoController : BaseController
    {
        public MerchantInfoController(IHttpContextAccessor accessor) : base(accessor)
        {
        }
        
        #region 商户激活-银联达标商户列表
        [Authorize]
        public JsonResult UnionPayQualifiedMerchants(string value)
        {
            value = PublicFunction.DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            Dictionary<string, object> Other = new Dictionary<string, object>();
            List<Dictionary<string, object>> dataList = UnionPayQualifiedMerchantsDo(value, out Other);
            return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList, Other = Other });
        }
        private List<Dictionary<string, object>> UnionPayQualifiedMerchantsDo(string value, out Dictionary<string, object> Other)
        {
            JsonData data = JsonMapper.ToObject(value);
            string MerchantName = data["MerchantName"].ToString(); //商户名称
            string MctNo = data["MctNo"].ToString(); //商户号
            int pageSize = int.Parse(function.CheckInt(data["page_size"].ToString()));
            int pageNum = int.Parse(function.CheckInt(data["page_num"].ToString()));
            string condition = " and IsAct=1 and ExamineStatus=1 and Months=10";
            if (!string.IsNullOrEmpty(MerchantName))
            {
                condition += " and Name like '%" + MerchantName + "%'";
            }
            if (!string.IsNullOrEmpty(MctNo))
            {
                var merAddInfo = MerchantAddInfoService.Query(" and MchtNo=" + MctNo + "");
                condition += " and Id=" + merAddInfo.Id + "";
            }
            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 = MerchantInfoService.List(relationData, condition, out count, pageNum, pageSize);
            foreach (Dictionary<string, object> subdata in source)
            {
                Dictionary<string, object> curData = new Dictionary<string, object>();
                var MerchantId = int.Parse(subdata["Id"].ToString());
                var merAddInfo = MerchantAddInfoService.Query(MerchantId);
                var Status = -1;
                var order = MerchantDepositOrderService.Query(" and Status>0 and MerchantId=" + MerchantId + "");
                if (order.Id > 0) Status = int.Parse(subdata["Status"].ToString());
                curData.Add("Id", MerchantId); //Id
                curData.Add("MerchantName", merAddInfo.CertMerchantName); //商户名称
                curData.Add("MctNo", merAddInfo.MchtNo); //商户号
                curData.Add("UpdateDate", order.UpdateDate.ToString() == null ? "" : DateTime.Parse(order.UpdateDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss")); //激活时间
                curData.Add("PayMode", order.Sort); //支付方式(1 支付宝 2 微信)
                curData.Add("Status", Status); //达标状态(-1 未参与 0 考核中 1 已通过 2 未通过 101 待领取 100 已领取 99 发放中)
                curData.Add("Months", subdata["Months"].ToString()); //达标月数
                dataList.Add(curData);
            }
            Other.Add("Count", count); //总数
            return dataList;
        }
        #endregion



    }
}
