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.PxcModels;
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 tmp()
        {
            string result = "";
            // string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
            // DataTable dt = CustomerSqlConn.dtable("select UserId from HelpProfitAmountSummary where TradeMonth>MaxAmount and TradeMonth='202301'", conn);
            // foreach(DataRow dr in dt.Rows)
            // {
            //     int UserId = int.Parse(dr["UserId"].ToString());
            //     decimal HelpAmount = 0; //当天总交易额
            //     DataTable dtsub = CustomerSqlConn.dtable("SELECT SUM(TradeAmount) TradeAmount FROM HelpProfitMerTradeSummay WHERE MerchantId IN(SELECT MerchantId FROM HelpProfitMerIds WHERE UserId=" + UserId + ") AND TradeDate='20230101'", conn);
            //     if(dtsub.Rows.Count > 0)
            //     {
            //         HelpAmount = decimal.Parse(function.CheckNum(dtsub.Rows[0]["TradeAmount"].ToString()));
            //     }
            //     result += test(UserId, HelpAmount);
            //     result += "\n\n";
            // }
            return result;
        }

        public string test(int UserId, decimal Amount)
        {
            // StatHelpProfitService.Instance.ResetMaxTradeAmountGo("202301");
            string result = "";
            // string conn = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
            // // int UserId = 145842;
            // Dictionary<string, decimal> dic = new Dictionary<string, decimal>();
            // dic.Add("31", Amount);
            // foreach(string day in dic.Keys)
            // {
            //     DataTable dt = CustomerSqlConn.dtable("SELECT MerchantId FROM HelpProfitMerIds WHERE UserId=" + UserId + " order by RAND()", conn);
            //     decimal total = dic[day];
            //     decimal avg = total / dt.Rows.Count;
            //     int min = ((int)avg - 100) * 100;
            //     if(min < 1) min = 1;
            //     int max = ((int)avg + 100) * 100;
            //     foreach(DataRow dr in dt.Rows)
            //     {
            //         string MerchantId = dr["MerchantId"].ToString();
            //         decimal amount = (decimal)function.get_Random(min, max) / 100;
            //         if(total > 0)
            //         {
            //             if(total < amount)
            //             {
            //                 amount = total;
            //             }
            //             DataTable check = CustomerSqlConn.dtable("SELECT Id,TradeAmount FROM HelpProfitMerTradeSummay WHERE MerchantId=" + MerchantId + " and TradeDate='202301" + day + "'", conn);
            //             if(check.Rows.Count > 0)
            //             {
            //                 if(day == "30")
            //                 {
            //                     decimal today = decimal.Parse(check.Rows[0]["TradeAmount"].ToString());
            //                     if(today < amount)
            //                     {
            //                         amount -= today;
            //                     }
            //                     else
            //                     {
            //                         amount = 0;
            //                     }
            //                 }
            //                 if(amount > 0) result += "update HelpProfitMerTradeSummay set TradeAmount=" + amount + " where Id=" + check.Rows[0]["Id"].ToString() + ";\n";
            //             }
            //             else
            //             {
            //                 if(amount > 0) result += "INSERT INTO HelpProfitMerTradeSummay (`TradeAmount`, `TradeDate`, `TradeMonth`, `MerchantId`) VALUES (" + amount + ", '202301" + day + "', '202301', " + MerchantId + ");\n";
            //             }
            //             check = CustomerSqlConn.dtable("SELECT Id FROM HelpProfitMerTradeMonthSummay WHERE MerchantId=" + MerchantId + " and TradeMonth='202301'", conn);
            //             if(check.Rows.Count > 0)
            //             {
            //                 if(amount > 0) result += "update HelpProfitMerTradeMonthSummay set TradeAmount=TradeAmount+" + amount + " where Id=" + check.Rows[0][0].ToString() + ";\n";
            //             }
            //             else
            //             {
            //                 if(amount > 0) result += "INSERT INTO HelpProfitMerTradeMonthSummay (`TradeAmount`, `TradeMonth`, `MerchantId`) VALUES (" + amount + ", '202301', " + MerchantId + ");\n";
            //             }
            //             total -= amount;
            //         }
            //         else
            //         {
            //             break;
            //         }
            //     }
            // }
            return result;
        }
    }
}