using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text.RegularExpressions;
using MySystem.PxcModels;
using Library;

namespace MySystem
{
    public class PublicFunction
    {
        public static decimal NumberFormat(decimal number, int floatCount = 2)
        {
            string str = number.ToString();
            if (str.Contains("."))
            {
                string[] list = str.Split('.');
                if (list[1].Length > floatCount)
                {
                    str = list[0] + "." + list[1].Substring(0, floatCount);
                }
            }
            else
            {
                str += ".00";
            }
            return decimal.Parse(str);
        }

        public static string GetPublicParam(WebCMSEntities db, string Key)
        {
            CustomTagSet set = db.CustomTagSet.FirstOrDefault(m => m.Tags == Key);
            if(set != null)
            {
                return set.Contents;
            }
            return "";
        }

        public static int FeeSetRecord(WebCMSEntities db, PosMachinesTwo pos, PosMerchantInfo mer, decimal ChangeFee)
        {
            PosMachinesFeeChangeRecord query = db.PosMachinesFeeChangeRecord.FirstOrDefault(m => m.PosId == pos.Id && m.Status == 0);
            if(query == null)
            {
                query = db.PosMachinesFeeChangeRecord.Add(new PosMachinesFeeChangeRecord()
                {
                    CreateDate = DateTime.Now, //创建时间
                    Sort = pos.BrandId, //品牌Id
                    UserId = pos.BuyUserId, //创客
                    PosId = pos.Id, //机具Id
                    PosSn = pos.PosSn,
                    MerNo = mer.MerchantNo,
                    MerchantId = mer.Id,
                    PosUserId = pos.BuyUserId, //机具所属人
                    ChangeFee = ChangeFee, //调整费率
                }).Entity;
                db.SaveChanges();
            }
            query.UpdateDate = DateTime.Now; //创建时间
            query.Sort = pos.BrandId; //品牌Id
            query.UserId = pos.BuyUserId; //创客
            query.PosId = pos.Id; //机具Id
            query.PosSn = pos.PosSn;
            query.MerNo = mer.MerchantNo;
            query.MerchantId = mer.Id;
            query.PosUserId = pos.BuyUserId; //机具所属人
            query.ChangeFee = ChangeFee; //调整费率
            db.SaveChanges();
            return query.Id;
        }


        #region 发送分表数据

        public static void SplitTradeRecord(TradeRecord obj, string TradeMonth)
        {
            RedisDbconn.Instance.AddList("AddTradeRecordQueue", Newtonsoft.Json.JsonConvert.SerializeObject(obj) + "#cut#" + TradeMonth);
        }

        public static void SplitUserAccountRecord(UserAccountRecord obj, string TradeMonth)
        {
            RedisDbconn.Instance.AddList("AddUserAccountRecordQueue", Newtonsoft.Json.JsonConvert.SerializeObject(obj) + "#cut#" + TradeMonth);
        }

        #endregion
    }
}
