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("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;
        }

        public string test1()
        {
            DateTime start = DateTime.Parse("2025-01-01 00:00:00");
            List<string> list = new List<string>();
            list.Add("LKB01231000599995661");
            WebCMSEntities db = new WebCMSEntities();
            foreach(string sub in list)
            {
                PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == sub && m.BindingState == 1 && m.BindingTime >= start);
                if(pos != null)
                {
                    MerchantAddInfo addMer = db.MerchantAddInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new MerchantAddInfo();
                    PosPushDataNewHelper.Bind(addMer, pos);
                }
            }
            db.Dispose();
            return "ok";
        }
    }
}