using System;
using System.Collections.Generic;
using Library;
using LitJson;
using System.Linq;
using MySystem.PxcModels;

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

        public void Start(JobMqMsg jobInfo)
        {
            string content = "";
            try
            {
                DoRefund();
                // string Msg = "success";
                // jobInfo.Status = Msg == "success" ? 1 : 0;
                // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
                // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(content))
                {
                    Dictionary<string, string> data = new Dictionary<string, string>();
                    data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    data.Add("ErrMsg", ex.ToString());
                    function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
                }
                else
                {
                    function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
                }
            }
        }

        private void DoRefund()
        {
            bool op = true;
            while (op)
            {
                RefundItem item = RedisDbconn.Instance.RPop<RefundItem>("ConsumerOrdersStat");
                if (item != null)
                {
                    bool success = false;
                    WebCMSEntities db = new WebCMSEntities();
                    if (item.PayMode == 1)
                    {
                        string resultString = new AlipayFunction().Refund(item.TradeNo, item.RefundMoney.ToString("f2"), "202202BB4fc8c2c03c914f2f87b6c678a77d7C03");
                        JsonData jsonObj = JsonMapper.ToObject(resultString);
                        if (jsonObj["alipay_trade_refund_response"]["code"].ToString() == "10000")
                        { 
                            success = true;
                        }
                    }
                    else if (item.PayMode == 2)
                    {
                        string AppId = "wxe2c051b3e46c0f6f";
                        string MchId = "1611167423";
                        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 = new WeChatFunction().Refund(AppId, MchId, SubMchId, item.RefundMoney, item.TotalMoney, item.TradeNo, RefundNo);
                        if (dic.ContainsKey("status"))
                        {
                            if (dic["status"] == "SUCCESS")
                            {
                                success = true;
                            }
                        }
                    }
                    if (success)
                    {
                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == item.ProfitId);
                        if (order != null)
                        {
                            order.Status = 1;
                            db.SaveChanges();
                        }
                    }
                    db.Dispose();
                }
                else
                {
                    op = false;
                }
            }
        }
    }
}