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.Main;
using LitJson;
using Library;
using System.Data;
using MySystem.Service.Main;
using LKBAAlipayConfig;

namespace MySystem.Areas.Api.Controllers.v1
{
    [Area("Api")]
    [Route("/v1/qrcodeplatemain/[controller]/[action]")]
    public class MerchantDepositOrderController : BaseController
    {
        public MerchantDepositOrderController(IHttpContextAccessor accessor) : base(accessor)
        {
        }
        #region 商户激活-确认支付
        // [Authorize]
        public JsonResult Pay(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = PayDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson PayDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式(1 支付宝)
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            int Id = int.Parse(function.CheckInt(data["Id"].ToString())); //订单记录Id
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            var query = MerchantDepositOrderService.Query(Id);
            if (query.Id > 0)
            {
                Dictionary<string, object> fields = new Dictionary<string, object>();
                fields.Add("Sort", PayMode); //支付方式(1 支付宝)
                MerchantDepositOrderService.Edit(fields, Id, false);
                string PayData = "";
                if (PayMode == 1)
                {
                    string TotalPrice = query.ActPayPrice.ToString();
                    function.WriteLog(query.OrderNo, "支付宝支付日志");
                    function.WriteLog(TotalPrice.ToString(), "支付宝支付日志");
                    function.WriteLog("商户激活—确认订单", "支付宝支付日志");
                    string ProductName = "商户激活";
                    PayData = new Alipay.AlipayPublicClass(_accessor.HttpContext).GetAlipayInfo(query.OrderNo, TotalPrice, ProductName, LKBAliPayInfo.AppId, LKBAliPayInfo.PrivateKey, SpHost + "/Api/Alipay/NoticePay2");
                    function.WriteLog(PayData, "支付宝支付日志");
                }
                Obj.Add("PayData", PayData); //支付宝微信SDK所需数据
            }
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion


        #region 商户激活—确认订单
        // [Authorize]
        public JsonResult ConfirmOrder(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = ConfirmOrderDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson ConfirmOrderDo(string value)
        {
            function.WriteLog(value, "商户激活—确认订单");
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            MerchantInfo merchant = MerchantInfoService.Query(MerchantId);
            string OrderNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
            string check = RedisDbconn.Instance.Get<string>("ConfirmOrder:" + MerchantId + "_" + OrderNo);
            if (!string.IsNullOrEmpty(check))
            {
                return new AppResultJson() { Status = "-1", Info = "请勿重复下单！" };
            }
            RedisDbconn.Instance.Set("ConfirmOrder:" + MerchantId + "_" + OrderNo, "1");
            RedisDbconn.Instance.SetExpire("ConfirmOrder:" + MerchantId + "_" + OrderNo, 10);
            var IsBuy = MerchantDepositOrderService.Query(" 1=1 and Status>0 and MerchantId=" + MerchantId + "");
            if (IsBuy.Id > 0) return new AppResultJson() { Status = "-1", Info = "请勿重复下单！" };
            decimal ActPayPrice = 365; //服务费总额
            var Id = MerchantDepositOrderUtil.AddMerchantDepositOrder(OrderNo, merchant.UserId, MerchantId, ActPayPrice);
            var query = MerchantDepositOrderService.Query(Id);
            Obj.Add("Id", Id); //Id
            Obj.Add("CreateDate", query.CreateDate); //创建时间
            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion


        #region 已激活商户-活动时间
        // [Authorize]
        public JsonResult Info(string value)
        {
            value = DesDecrypt(value);
            JsonData data = JsonMapper.ToObject(value);
            AppResultJson result = InfoDo(value);
            return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
        }
        private AppResultJson InfoDo(string value)
        {
            JsonData data = JsonMapper.ToObject(value);
            int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            var CreateDate = "";
            var StartDate = "";
            var EndDate = "";
            var Status = -1;
            var query = MerchantDepositOrderService.Query(" 1=1 and Status>0 and MerchantId=" + MerchantId + "");
            if (query.Id > 0)
            {
                CreateDate = DateTime.Parse(query.UpdateDate.ToString()).Year + "年" + DateTime.Parse(query.UpdateDate.ToString()).Month + "月" + DateTime.Parse(query.UpdateDate.ToString()).Day + "日";
                StartDate = DateTime.Parse(query.UpdateDate.ToString()).AddMonths(1).Year + "年" + DateTime.Parse(query.UpdateDate.ToString()).AddMonths(1).Month + "月";
                EndDate = DateTime.Parse(query.UpdateDate.ToString()).AddMonths(11).Year + "年" + DateTime.Parse(query.UpdateDate.ToString()).AddMonths(11).Month + "月";
                Status = MerchantInfoService.Query(MerchantId).ExamineStatus;
            }
            Obj.Add("CreateDate", CreateDate); //参与时间
            Obj.Add("StartDate", StartDate); //考核开始时间
            Obj.Add("EndDate", EndDate); //考核结束时间
            Obj.Add("Status", Status); //考核状态(-1 未参与 0 考核中 1 已通过 2 未通过 101 待领取 100 已领取 99 发放中)

            return new AppResultJson() { Status = "1", Info = "", Data = Obj };
        }
        #endregion

    }
}
