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

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

        //绑定数据
        public static void Bind(PosMerchantInfo mer)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", mer.KqSnNo); //机具sn
                dataContent.Add("mer_no", mer.KqMerNo); //商户号
                dataContent.Add("id_card", mer.MerIdcardNo); //商户身份证
                dataContent.Add("mer_name", mer.MerchantName); //商户姓名
                dataContent.Add("mer_mobile", mer.MerchantMobile); //商户手机号
                dataContent.Add("request_id", mer.CreateDate.Value.ToString("yyyyMMddHHmmssfff") + function.get_Random(8)); //流水号
                dataContent.Add("bind_time", mer.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
                dataContent.Add("brand", mer.BrandId); //品牌
                Push("bind", dataContent);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
            }
        }

        //解绑数据
        public static void UnBind(PosMerchantInfo mer)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", mer.KqSnNo); //机具sn
                dataContent.Add("mer_no", mer.KqMerNo); //商户号
                dataContent.Add("request_id", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8)); //流水号
                dataContent.Add("un_bind_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
                dataContent.Add("brand", mer.BrandId); //品牌
                Push("un_bind", dataContent);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送解绑数据异常");
            }
        }

        //换绑数据
        public static void ChangeBind(PosMerchantInfo mer, PosMachinesTwo source, PosMachinesTwo pos)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("request_id", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8)); //流水号
                dataContent.Add("mer_no", mer.KqMerNo); //商户号
                dataContent.Add("source_pos_sn", source.PosSn); //来源机具sn
                dataContent.Add("source_brand", source.BrandId); //来源品牌
                dataContent.Add("to_pos_sn", pos.PosSn); //兑换机具sn
                dataContent.Add("to_brand", pos.BrandId); //兑换品牌
                dataContent.Add("change_bind_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //换绑时间
                Push("change_bind", dataContent);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送换绑数据异常");
            }
        }

        //押金数据
        public static void Deposit(PosMerchantInfo mer, decimal amt)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", mer.KqSnNo); //机具sn
                dataContent.Add("mer_no", mer.KqMerNo); //商户号
                dataContent.Add("request_id", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8)); //流水号
                dataContent.Add("deposit_amount", amt); //押金金额
                dataContent.Add("trade_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                dataContent.Add("brand", mer.BrandId); //品牌
                Push("deposit", dataContent);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送押金数据异常");
            }
        }

        //激活数据
        public static void Active(PosMerchantInfo mer)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", mer.KqSnNo); //机具sn
                dataContent.Add("mobile", mer.KqMerNo); //商户号
                dataContent.Add("request_id", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8)); //流水号
                dataContent.Add("activation_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //激活时间
                dataContent.Add("brand", mer.BrandId); //品牌
                Push("active", dataContent);
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送激活数据异常");
            }
        }

        //交易数据
        public static void Trade(TradeRecord trade)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", trade.SnNo); //机具sn
                dataContent.Add("mer_no", trade.MerNo); //商户号
                dataContent.Add("request_id", trade.TradeSerialNo); //流水号
                dataContent.Add("trade_amount", trade.TradeAmount); //交易金额
                dataContent.Add("trade_time", trade.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                dataContent.Add("brand", trade.BrandId); //品牌
                dataContent.Add("card_type", trade.BankCardType); //卡类型
                dataContent.Add("qr_pay_flag", trade.QrPayFlag); //云闪付标记
                Push("trade", dataContent);
            }
            catch (Exception ex)
            {
                Utils.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));
        }
    }
}