using System;
using System.Collections.Generic;
using System.Linq;
using Library;
using MySystem.Models.Main;

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

        //绑定数据
        public static void Bind(MerchantAddInfo mer, MerchantInfo m)
        {
            try
            {
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                // dataContent.Add("pos_sn", pos.PosSn); //机具sn
                dataContent.Add("mer_no", mer.MchtNo); //商户号
                dataContent.Add("id_card", mer.IdCardNumber); //商户身份证
                dataContent.Add("mer_name", mer.CertMerchantName); //商户姓名
                dataContent.Add("mer_mobile", mer.MobilePhone); //商户手机号
                dataContent.Add("request_id", getRequestId(mer.Id, "bind")); //流水号
                dataContent.Add("bind_time", mer.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
                if(mer.BrandId == 1)
                {
                    dataContent.Add("brand", 29); //品牌
                }
                else
                {
                    dataContent.Add("brand", 101); //品牌
                }
                dataContent.Add("user_id", m.UserId); //创客Id
                Push("qr_bind", dataContent);
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
            }
        }

        //交易数据
        public static void Trade(ConsumerOrders trade, MerchantAddInfo mer)
        {
            try
            {
                int QrPayFlag = 0;
                int BankCardType = 1;
                List<string> BrandIds = new List<string>();
                decimal TradeAmount = trade.PayMoney;
                Dictionary<string, object> dataContent = new Dictionary<string, object>();
                dataContent.Add("pos_sn", trade.SnNo); //机具sn
                dataContent.Add("mer_no", mer.MchtNo); //商户号
                dataContent.Add("request_id", trade.OrderNo); //流水号
                dataContent.Add("trade_amount", TradeAmount); //交易金额
                dataContent.Add("trade_time", trade.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
                dataContent.Add("brand", 101); //品牌
                // dataContent.Add("card_type", BankCardType); //卡类型
                // dataContent.Add("qr_pay_flag", QrPayFlag); //云闪付标记
                dataContent.Add("fee_rate", trade.IsAct == 1 ? 0.6M : 0.63M);
                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 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);
            KxsRedisDbconn.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);
        }
    }
}