using System;
using System.Collections.Generic;
using System.Linq;
using Library;
using MySystem.MpMainModels2;

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

        //绑定数据
        public static void Bind(SpModels.BindRecord mer, string kind)
        {
            try
            {
                string ProductType = mer.ProductType;
                if(kind.Contains("好哒二维码")) ProductType = "20";
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", mer.MerSnNo); //机具sn
                dataContent.Add("mer_no", mer.MerNo); //商户号
                dataContent.Add("id_card", ""); //商户身份证
                dataContent.Add("mer_name", mer.MerName); //商户姓名
                dataContent.Add("mer_mobile", ""); //商户手机号
                dataContent.Add("request_id", function.MD5_32(mer.MerSnNo + mer.MerNo)); //流水号
                dataContent.Add("bind_time", mer.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
                dataContent.Add("brand", int.Parse(function.CheckInt(ProductType))); //品牌
                if(kind.Contains("好哒二维码"))
                {
                    dataContent.Add("user_id", mer.QueryCount);
                    Push("qr_bind", dataContent);
                }
                else
                {
                    Push("bind", dataContent);
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
            }
        }

        //押金数据
        public static void Deposit(SpModels.ActivateRecord act)
        {
            try
            {
                List<string> BrandIds = new List<string>();
                decimal TradeAmount = decimal.Parse(function.CheckNum(act.SeoTitle));
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", act.SnNo); //机具sn
                dataContent.Add("mer_no", act.MerNo); //商户号
                dataContent.Add("request_id", getRequestId(act.SnNo + act.MerNo, "deposit")); //流水号
                dataContent.Add("deposit_amount", TradeAmount); //押金金额
                dataContent.Add("trade_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                dataContent.Add("card_type", 1); //卡类型
                dataContent.Add("brand", int.Parse(function.CheckInt(act.ProductType))); //品牌
                Push("deposit", dataContent);
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送押金数据异常");
            }
        }

        //交易数据
        public static void Trade(ConsumerOrders trade, string kind)
        {
            try
            {//{"pos_sn":"LKB01231000305000491","mer_no":"","request_id":"2025011015514890534136704","trade_amount":3.0,"trade_time":"2025-01-10 15:51:49","brand":100,"card_type":1,"qr_pay_flag":0,"fee_rate":0.63,"fee_amt":3}
                int brand = 18;
                if(kind.Contains("好哒语音王")) brand = 18;
                if(kind.Contains("收款王M820")) brand = 19;
                if(kind.Contains("收款王M826")) brand = 20;
                if(kind.Contains("S312")) brand = 21;
                if(kind.Contains("好哒扫码通")) brand = 29;
                if(kind.Contains("好哒二维码")) brand = 20;
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", trade.SnNo); //机具sn
                dataContent.Add("mer_no", trade.TradePayNo); //商户号
                dataContent.Add("request_id", trade.OrderNo); //流水号
                dataContent.Add("trade_amount", trade.PayMoney); //交易金额
                dataContent.Add("trade_time", trade.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                dataContent.Add("brand", brand); //品牌
                // dataContent.Add("is_act", trade.IsAct); //品牌
                // dataContent.Add("card_type", 1); //卡类型
                // if(kind.Contains("好哒二维码"))
                // {
                //     dataContent.Add("qr_pay_flag", 1); //云闪付标记
                // }
                // else
                // {
                //     dataContent.Add("qr_pay_flag", 0); //云闪付标记
                // }
                int fee_rate_num = trade.Sort;
                if(fee_rate_num == 0) fee_rate_num = 38;
                decimal fee_rate = (decimal)fee_rate_num / 100;
                dataContent.Add("fee_rate", fee_rate); // 费率
                // dataContent.Add("fee_amt", trade.IsAct == 1 ? 0 : 3); // 秒到费
                Push("lkb_trade", dataContent);
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送交易数据异常");
            }
        }

        public static void Trade(MySystem.MpMainModels.ConsumerOrders trade)
        {
            try
            {
                int brand = 100;
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", trade.SnNo); //机具sn
                dataContent.Add("mer_no", trade.TradePayNo); //商户号
                dataContent.Add("request_id", trade.OrderNo + trade.Id + "1"); //流水号
                dataContent.Add("trade_amount", trade.PayMoney); //交易金额
                dataContent.Add("trade_time", trade.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                dataContent.Add("brand", brand); //品牌
                dataContent.Add("is_act", trade.IsAct); //品牌
                dataContent.Add("card_type", 1); //卡类型
                dataContent.Add("qr_pay_flag", 0); //云闪付标记
                dataContent.Add("fee_rate", trade.IsAct == 1 ? 0.6M : 0.63M); // 费率
                dataContent.Add("fee_amt", trade.IsAct == 1 ? 0 : 3); // 秒到费
                Push("trade", dataContent);
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送交易数据异常");
            }
        }

        public static void Push(string dataType, Dictionary<string, object> dataContent)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            data.Add("data_type", dataType);
            data.Add("data_content", dataContent);
            RedisDbconn.Instance.AddList("KxsPosDataQueue", Newtonsoft.Json.JsonConvert.SerializeObject(data));
        }

        public static string getRequestId(int id, string type)
        {
            if(id > 0)
            {
                return function.MD5_32(id.ToString() + type);
            }
            return DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
        }
        public static string getRequestId(string str, string type)
        {
            if(!string.IsNullOrEmpty(str))
            {
                return function.MD5_32(str + type);
            }
            return DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
        }
    }
}