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

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


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

        public void StartListen()
        {
            while(true)
            {
                try
                {
                    string content = RedisDbconn.Instance.RPop<string>("ToAlipayAccountQueue");
                    if(!string.IsNullOrEmpty(content))
                    {
                        string[] idlist = content.Split(',');
                        foreach(string id in idlist)
                        {
                            int IdNum = int.Parse(id);
                            StartListenDo(IdNum);
                        }
                    }
                    else
                    {
                        Thread.Sleep(60000);
                    }
                }
                catch(Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "退还押金到支付宝余额异常");
                    Thread.Sleep(600000);
                }
            }
        }

        public void StartListenDo(int Id)
        {
            WebCMSEntities db = new WebCMSEntities();
            MerchantDepositReturns item = db.MerchantDepositReturns.FirstOrDefault(m => m.Id == Id);
            if(item != null)
            {
                PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new PosMerchantInfo();
                string Name = item.OperateMan;
                string AccountNo = item.AlipayAccountNo;
                decimal ReturnAmount = item.ReturnAmount * 0.92M;
                // decimal ReturnAmount = item.ReturnAmount / 1000;
                // if(ReturnAmount < 0.1M)
                // {
                //     ReturnAmount = 0.1M;
                // }
                string Amount = ReturnAmount.ToString("f2");
                PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
                string result = new Alipay.AlipayPublicMethod().TransferToAccount(set.AlipayAppId, set.AlipayPrivateKey,set.AlipayPublicKey, AccountNo, Amount, "达标奖到账", Name, "机具SN:" + mer.KqSnNo + ";商户姓名:" + mer.MerRealName);
                if(result.StartsWith("success"))
                {
                    item.Status = 1;
                    mer.StandardStatus = 1;
                }
                else
                {
                    item.Status = 2;
                    item.SeoDescription = result;
                    mer.StandardStatus = 101;
                    mer.SeoDescription = result;
                }
                db.SaveChanges();
            }
            db.Dispose();
        }
    }
}