using System;
using System.Collections.Generic;
using Library;
using LitJson;
using System.Linq;
using System.Threading;
using MySystem.Models;

namespace MySystem
{
    public class ConsumerOrdersReturnDoService
    {
        public readonly static ConsumerOrdersReturnDoService Instance = new ConsumerOrdersReturnDoService();
        private ConsumerOrdersReturnDoService()
        { }

        public void Start()
        {
            Thread th = new Thread(DoRefund);
            th.IsBackground = true;
            th.Start();
        }

        private void DoRefund()
        {
            bool op = true;
            while (op)
            {
                DateTime start = DateTime.Now.AddDays(-1);
                WebCMSEntities db = new WebCMSEntities();
                var list = db.ConsumerProfit.Select(m => new { m.Id, m.Status, m.CreateDate }).Where(m => m.CreateDate >= start && m.Status == 0).Take(20).ToList();
                foreach(var sub in list)
                {
                    try
                    {
                        ConsumerProfit item = db.ConsumerProfit.FirstOrDefault(m => m.Id == sub.Id);
                        if(item != null)
                        {
                            bool success = false;
                            if (item.QueryCount == 1)
                            {
                                // string resultString = new AlipayFunction().Refund(item.SeoTitle, item.GetMoney.ToString("f2"), "202202BB4fc8c2c03c914f2f87b6c678a77d7C03");
                                // JsonData jsonObj = JsonMapper.ToObject(resultString);
                                // if (jsonObj["alipay_trade_refund_response"]["code"].ToString() == "10000")
                                // {
                                //     success = true;
                                // }
                            }
                            else if (item.QueryCount == 2)
                            {
                                MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantAddInfo();
                                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantInfo();
                                string SubMchId = merchantAdd.SubMchid;
                                string Description = merchant.Name;
                                string RefundNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
                                Dictionary<string, string> dic = WeChatFunction.Instance.Refund(SubMchId, item.GetMoney, decimal.Parse(function.CheckNum(item.SeoKeyword)), item.SeoTitle, RefundNo);
                                if (dic.ContainsKey("status"))
                                {
                                    if (dic["status"] == "SUCCESS")
                                    {
                                        success = true;
                                    }
                                }
                            }
                            if (success)
                            {
                                item.Status = 1;
                                ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == item.OrderId);
                                if (order != null)
                                {
                                    order.Status = 2;
                                }
                                db.SaveChanges();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "退款返现队列异常");
                    }
                }
                db.Dispose();
            }
        }
    }
}