using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Data;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MySystem.Models;
using System.Threading;
using Library;
using LitJson;

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

        public string test()
        {
            string txt = "ok";
            // txt = WeChatFunction.Instance.QueryProfitShare("1646405429","4200001807202306098649105270","2023060910241258564572510");
            // ProfitHelper.Instance.ReturnStat(Id, 2);
            WebCMSEntities db = new WebCMSEntities();
            // List<int> oids = new List<int>();
            // if(id > 0)
            // {
            //     oids.Add(id);
            // }
            // else
            // {
            //     oids.Add(15);
            //     oids.Add(16);
            //     oids.Add(17);
            //     oids.Add(18);
            //     oids.Add(19);
            //     oids.Add(20);
            // }
            // foreach(int oid in oids)
            // {
            //     ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == oid);
            //     if(order != null)
            //     {
            //         RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:List", order.Id.ToString());
            //         // RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:" + order.MerchantId, order);
            //     }
            // }

            // string content = function.ReadInstance("/wx.log");
            // string[] contents = content.Split('\n');
            // foreach(string sub in contents)
            // {
            //     RedisDbconn.Instance.AddList("WeChatPayBack", sub);
            // }

            // DataTable dt = CustomerSqlConn.dtable("select Id from ConsumerProfit where CreateDate<='2024-01-26 11:25:00' and SeoDescription is not null order by Id desc limit 2000", MysqlConn.SqlConnStr);
            // foreach(DataRow dr in dt.Rows)
            // {
            //     int Id = int.Parse(dr["Id"].ToString());
            //     ConsumerProfit profit = db.ConsumerProfit.FirstOrDefault(m => m.Id == Id);
            //     if(profit != null)
            //     {
            //         ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == profit.OrderId) ?? new ConsumerOrders();
            //         MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == profit.MerchantId) ?? new MerchantAddInfo();
            //         if(order.PayMode == 1)
            //         {
            //             string result = AlipayFunction.Instance.Refund(order.SeoTitle, profit.SeoDescription, profit.GetMoney.ToString(), "门店消费补贴", merchantAdd.AlipayAuthToken);
            //         }
            //         else
            //         {
            //             Dictionary<string, string> dic = WeChatFunction.Instance.Refund(merchantAdd.SubMchid, profit.GetMoney, order.PayMoney, order.OrderNo, profit.SeoDescription, "门店消费补贴");
            //         }
            //     }
            // }
            // db.Dispose();

            return txt;
        }

        public string testAlipay()
        {
            // string content = AlipayFunction.Instance.QuerySignStatus("2023061317351531100060045");
            // List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
            // Parameters.Add(new RoyaltyParameters()
            // {
            //     Account = "2088141704579033",
            //     Amount = "0.01",
            // });
            // string content = AlipayFunction.Instance.Settle("2023061615441556447868073", "2023061622001451251436120885", Parameters);
            // string content = AlipayFunction.Instance.BindSettleRelation(DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), "2088141704579033");
            // string content = AlipayFunction.Instance.Refund("2023061622001451251436120885", "0.01", "门店消费补贴");
            // string content = AlipayFunction.Instance.QuerySettle("2023071214274372011988068", "2023071222001457121457238496", "202307BB6d61386d17104395831e3ea6309f0X57");
            // MerchantConfirmService.Instance.StartDo(68, 2);
            // string content = AlipayFunction.Instance.BindSettleRelation(DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), AlipayFunction.Instance.Pid, "202307BB6d61386d17104395831e3ea6309f0X57");

            List<string> oids = new List<string>();
            oids.Add("157404");
            foreach(string oid in oids)
            {
                RedisDbconn.Instance.AddList("AddWeChatDiviQueue", oid);
            }
            return "ok";
        }

        public string test1()
        {            
            // ProfitHelper.Instance.DoProfit();
            List<string> list = new List<string>();
            list.Add("LKB01230900199998516");
            WebCMSEntities db = new WebCMSEntities();
            foreach(string sub in list)
            {
                bool op = db.PosMachinesTwo.Any(m => m.PosSn == sub);
                if(!op)
                {
                    //insert into PosMachinesTwo (CreateDate,SourceStoreId,StoreId,PosSn,SourcePosSn) values (now(),2,2,'
                    db.PosMachinesTwo.Add(new PosMachinesTwo()
                    {
                        CreateDate = DateTime.Now,
                        SourceStoreId = 2,
                        StoreId = 2,
                        PosSn = sub,
                        SourcePosSn = sub,
                    });
                    db.SaveChanges();
                }
            }
            db.Dispose();
            return "ok";
        }
    
    }
}