using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MySystem.MainModels;
using System.Linq;
using Library;
using LitJson;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace MySystem.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Error()
        {
            string isapi = Request.Headers["Api"].ToString();
            if (isapi != "1")
            {
                if (Response.StatusCode == 500)
                {
                    return Redirect("/public/errpage/pc/500.html");
                }
                else if (Response.StatusCode == 502)
                {
                    return Redirect("/public/errpage/pc/502.html");
                }
                else if (Response.StatusCode == 404)
                {
                    return Redirect("/public/errpage/pc/404.html");
                }
                else if (Response.StatusCode == 401)
                {
                    return Content("401");
                }
            }
            return View();
        }

        public string auth(string app_id, string source, string application_type, string app_auth_code, string state)
        {
            return app_id + "\n" + source + "\n" + application_type + "\n" + app_auth_code + "\n" + state + "\n" + Request.QueryString.ToString();
        }

        public string test()
        {
            string result = "";
            WebCMSEntities maindb = new WebCMSEntities();
            List<string> nos = new List<string>();
            foreach(string no in nos)
            {
                ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.OrderNo == no);
                if (order != null)
                {
                    List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, 1, 999999);
                    if (suborders.Count > 0)
                    {
                        ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == order.Id);
                        if (suborder != null)
                        {
                            order.CurDivi = suborder.CurDivi;
                            maindb.SaveChanges();
                            RedisDbconn.Instance.RemoveFromList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, suborder);
                            order.ReturnFlag = 0;
                            maindb.SaveChanges();
                        }
                    }
                }
            }
            maindb.Dispose();

            return result;
        }

        public string test2()
        {
            string result = "ok";
            WebCMSEntities maindb = new WebCMSEntities();
            List<string> nos = new List<string>();
            nos.Add("2023122118000851456243816,288.4");
            nos.Add("2023122117593143700062399,341.04");
            nos.Add("2023122117325908930632327,254.15");
            nos.Add("2023122116415701143090181,312.26");
            nos.Add("2023122116413711877723909,276.83");
            nos.Add("2023122114085649698036050,342.45");
            nos.Add("2023122313533734425618726,299.78");
            nos.Add("2023122313282248497454670,246.97");
            nos.Add("2023122311000256069736155,261.82");
            nos.Add("2023122310594073141191302,304.84");
            nos.Add("2023122520210853072569713,191.59");
            nos.Add("2023122518244789260165229,186.53");
            nos.Add("2023122515274323574090329,275.95");
            nos.Add("2023122515252336206794004,261.82");
            nos.Add("2023122619342154468765968,178.31");
            nos.Add("2023122618011435195726036,258.07");
            nos.Add("2023122618001597671800401,248.51");
            nos.Add("2023122615422945713502403,191.84");
            nos.Add("2023122613362000858247931,227.61");
            nos.Add("2023122820200788070426381,198.57");
            nos.Add("2023122820125421416300639,237.29");
            nos.Add("2023122820123371996899445,182.56");
            nos.Add("2023122814020384897788563,167.38");
            nos.Add("2023122811030268206294741,228.91");
            nos.Add("2023122810460690412361230,210.46");
            nos.Add("2023122810364031620851479,202.11");
            nos.Add("2023122811124893906532985,224.09");
            nos.Add("2023122917185120082183642,152.23");
            nos.Add("2023122911233689014204746,163.27");
            nos.Add("2023122911214057116568112,122.39");
            nos.Add("2023122911184819662469868,180.80");
            foreach(string sub in nos)
            {
                string[] data = sub.Split(',');
                string no = data[0];
                decimal CurDivi = decimal.Parse(data[1]);
                ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.OrderNo == no);
                if (order != null)
                {
                    JsonData ProfitInfo = JsonMapper.ToObject(order.SeoDescription);
                    int ProfitDays = int.Parse(function.CheckInt(ProfitInfo["ProfitDays"].ToString())); //活动有效时间
                    if (order.IsAct == 1)
                    {
                        if (order.MaxDivi > order.CurDivi)
                        {
                            List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId);
                            if (suborders.Count > 0)
                            {
                                ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == order.Id);
                                if (suborder == null)
                                {
                                    order.CurDivi = CurDivi;
                                    order.SeoDescription = Regex.Replace(function.CheckNull(order.SeoDescription), "\"UpdateDate\":\".*?\"", "\"UpdateDate\":\"" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "\"");
                                    order.SeoDescription = Regex.Replace(function.CheckNull(order.SeoDescription), "\"ProfitDays\":.*?,", "\"ProfitDays\":254,");
                                    RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, order);
                                    order.ReturnFlag = 1;
                                    maindb.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            maindb.Dispose();

            return result;
        }

        public string makeCode()
        {
            string code = "";
            List<string> list = new List<string>();
            string start = "LKB0123090019999";
            for(int i = 0;i<=9999;i++)
            {
                string num = i.ToString();
                for(int j = 0;j< 4 - i.ToString().Length;j++)
                {
                    num = "0" + num;
                }
                string sn = dbconn.Encrypt3DES(start + num + function.get_Random(8), "l2k0b2#3");
                sn = sn.Substring(0, sn.Length - 1);
                code += System.Web.HttpUtility.UrlEncode(sn) + "\n";
            }
            return code;
        }
    }
}