using Library;
using System.Collections.Generic;
using System;
using System.Linq;
using LitJson;
using MySystem.PxcModels;
namespace MySystem
{
    /// <summary>
    /// 调整机具服务费工具类
    /// </summary>
    public class ChangePosFeeUtil
    {      



        #region 金控-设置押金
        public static string SetJKDeposit(string SnIds, string DepositId)
        {
            // string DepositId = "299";
            string check = RedisDbconn.Instance.Get<string>("SetJKPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetJKPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetJKPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                //判断是否存在关联数据
                string content = "";
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "金控-设置押金-返回报文");
                content = PublicImportDataService.Instance.LkbSetDeposit(PosSn, decimal.Parse(DepositId));
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "金控-设置押金-返回报文");
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "000000")
                {
                    return jsonObj["message"].ToString();
                }
                content = jsonObj["data"].ToString();
                content = PublicImportDataService.Instance.LkbDecrypt(content);
                JsonData contentObj = JsonMapper.ToObject(content);
                if (contentObj["respCode"].ToString() != "00")
                {
                    return contentObj["respMsg"].ToString();
                }
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "金控-设置押金-返回报文");
            }
            return "设置成功";
        }
        #endregion



        #region 开店宝-设置押金
        public static string SetKDBDeposit(string SnIds, string DepositId)
        {
            // string DepositId = "473";
            string check = RedisDbconn.Instance.Get<string>("SetKDBPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetKDBPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetKDBPosDepositWait:" + SnIds, 10);
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "开店宝-设置押金-返回报文");
                string content = PublicImportDataService.Instance.ModifyDeposit(PosSn, DepositId);
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "开店宝-设置押金-返回报文");
            }
            return "设置成功";
        }
        #endregion



        #region 乐刷-设置押金
        public static string SetLSDeposit(string SnIds, string DepositId, string BrandId = "4")
        {
            // string DepositId = "300";
            string check = RedisDbconn.Instance.Get<string>("SetLSPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetLSPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetLSPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                string OldId = "", NewId = "";
                if (DepositId == "100")
                {
                    NewId = "704067039039856640";
                }
                else if (DepositId == "200")
                {
                    NewId = "709820183485095936";
                }
                else if (DepositId == "300")
                {
                    NewId = "709820390742437888";
                }
                string content = PublicImportDataService.Instance.QueryActiveForConfig(PosSn, int.Parse(BrandId));
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() == "0")
                {
                    OldId = jsonObj["data"]["posMarket"]["id"].ToString();
                }
                jsonObj = JsonMapper.ToObject(content);
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + OldId + ":" + NewId, "乐刷-设置押金-返回报文");
                content = PublicImportDataService.Instance.SetDepositForLS(PosSn, OldId, NewId, int.Parse(BrandId));
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "乐刷-设置押金-返回报文");
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "0")
                {
                    return jsonObj["msg"].ToString();
                }
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "乐刷-设置押金-返回报文");
                
            }
            return "设置成功";
        }
        #endregion



        #region 立刷-设置押金
        public static string SetLISDeposit(string SnIds, string DepositId, string BrandId = "6")
        {
            string check = RedisDbconn.Instance.Get<string>("SetLISPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetLISPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetLISPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "立刷-设置押金-返回报文");
                string content = "";
                if(BrandId == "6")
                {
                    content = PublicImportDataService.Instance.SetLiSDeposit(PosSn, int.Parse(DepositId));
                }
                else
                {
                    content = PublicImportDataService.Instance.SetLiSWeDeposit(PosSn, int.Parse(DepositId));
                }
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "立刷-设置押金-返回报文");
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["ret_code"].ToString() != "00")
                {
                    return jsonObj["ret_msg"].ToString();
                }
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "立刷-设置押金-返回报文");
                
            }
            return "设置成功";
        }
        #endregion



        #region 盛付通-设置押金
        public static string SetSFTDeposit(string SnIds, string DepositId)
        {
            string check = RedisDbconn.Instance.Get<string>("SetSFTPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetSFTPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetSFTPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "盛付通-设置押金-返回报文");
                string DepositIdString = DepositId;
                if(DepositIdString == "0") DepositIdString = "000";
                // if(DepositId == "0")
                // {
                //     PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
                //     if(!string.IsNullOrEmpty(merchant.KqMerNo))
                //     {
                //         string back = PublicImportDataService.Instance.SftSnBindOrUnbind(PosSn, merchant.KqMerNo);
                //         Utils.WriteLog("back:" + back, "盛付通-设置押金-返回报文");
                //     }
                // }
                string content = PublicImportDataService.Instance.SetDepositForSft(PosSn, "2026POS" + DepositIdString);
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "盛付通-设置押金-返回报文");
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["respCode"].ToString() != "00")
                {
                    return jsonObj["errorMsg"].ToString();
                }
            }
            return "设置成功";
        }
        #endregion



        #region 海科-设置押金
        public static string SetHKDeposit(string SnIds, string DepositId, string BrandId = "8")
        {
            // string DepositId = "300";
            string check = RedisDbconn.Instance.Get<string>("SetHKPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetHKPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetHkPosDepositWait:" + SnIds, 10);
            WebCMSEntities db = new WebCMSEntities();
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                string OldId = "", NewId = "";
                if (DepositId == "100")
                {
                    NewId = BrandId == "8" ? "202207011718129" : "202207011718197";
                }
                else if (DepositId == "200")
                {
                    NewId = BrandId == "8" ? "747200504467566592" : "747199934126108672";
                }
                else if (DepositId == "300")
                {
                    NewId = BrandId == "8" ? "747193472108732416" : "747193820156276736";
                }
                string content = PublicImportDataService.Instance.QueryActiveForConfigHK(PosSn, int.Parse(BrandId));
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() == "0")
                {
                    OldId = jsonObj["data"]["posMarket"]["id"].ToString();
                }
                jsonObj = JsonMapper.ToObject(content);
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + OldId + ":" + NewId, "海科-设置押金-返回报文");
                content = PublicImportDataService.Instance.SetDepositForHK(PosSn, OldId, NewId, int.Parse(BrandId));
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "海科-设置押金-返回报文");
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "0")
                {
                    return jsonObj["msg"].ToString();
                }
            }
            return "设置成功";
        }
        #endregion



        #region 联动-设置押金
        public static string SetLDDeposit(string SnIds, string DepositId, string BrandId = "10")
        {
            // string DepositId = "299";
            string check = RedisDbconn.Instance.Get<string>("SetLDPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetLDPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetLDPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "联动-设置押金-返回报文");
                string content = PublicImportDataService.Instance.LDServiceFee(PosSn, int.Parse(DepositId), int.Parse(BrandId));
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "联动-设置押金-返回报文");
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "000000")
                {
                    return jsonObj["msg"].ToString();
                }
            }
            return "设置成功";
        }
        #endregion



        #region 联动掌中宝-设置押金
        public static string SetLDZZBDeposit(string SnIds, string DepositId)
        {
            string check = RedisDbconn.Instance.Get<string>("SetLDZZBPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetLDZZBPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetLDZZBPosDepositWait:" + SnIds, 10);
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "联动掌中宝设置押金返回报文");
                string content = SetDepositPostService.Instance.LDZZBServiceFee(PosSn, int.Parse(DepositId));
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "联动掌中宝设置押金返回报文");
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "00")
                {
                    return jsonObj["msg"].ToString();
                }
            }
            return "设置成功";
        }
        #endregion



        #region 盒易付-设置押金
        public static string SetHYFDeposit(string SnIds, string DepositId, string UserId = "0")
        {
            // string DepositId = "299";
            string check = RedisDbconn.Instance.Get<string>("SetHYFPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetHYFPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetHYFPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "盒易付-设置押金-返回报文");
                string content = PublicImportDataService.Instance.BoxServiceFee(PosSn, DepositId, int.Parse(UserId));
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "盒易付-设置押金-返回报文");
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["resultCode"].ToString() != "1")
                {
                    return jsonObj["errorDesc"].ToString();
                }
            }
            return "设置成功";
        }
        #endregion



        #region POSP-设置押金
        public static string SetPosPDeposit(string SnIds, string DepositId)
        {
            string policy_code = "";
            string policy_name = "";
            if(DepositId == "0")
            {
                policy_code = "LKB_000";
                policy_name = "联客宝";
            }
            else if(DepositId == "99")
            {
                policy_code = "LKB_99";
                policy_name = "联客宝";
            }
            else if(DepositId == "199")
            {
                policy_code = "LKB_199";
                policy_name = "联客宝";
            }
            else if(DepositId == "299")
            {
                policy_code = "LKB_299";
                policy_name = "联客宝299押金";
            }
            string check = RedisDbconn.Instance.Get<string>("SetPosPDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetPosPDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetPosPDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "POSP-设置押金-返回报文");
                List<string> PosList = new List<string>();
                PosList.Add(PosSn);
                string content = PublicImportDataService.Instance.PospServiceFee(PosList, policy_code, policy_name);
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "POSP-设置押金-返回报文");
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "200")
                {
                    return "设置失败";
                }
            }
            return "设置成功";
        }
        #endregion
    
    
    
        #region 拉卡拉-设置押金
        public static string SetLKLDeposit(string SnIds, string DepositId, int BrandId)
        {
            string check = RedisDbconn.Instance.Get<string>("SetLKLPosDepositWait:" + SnIds);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetLKLPosDepositWait:" + SnIds, SnIds);
            RedisDbconn.Instance.SetExpire("SetLKLPosDepositWait:" + SnIds, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            string[] SnIdList = SnIds.Split(',');
            foreach (string PosSn in SnIdList)
            {
                //判断是否存在关联数据
                string content = "";
                Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "拉卡拉-设置押金-返回报文");
                content = PublicImportDataService.Instance.LkLSetDeposit(PosSn, decimal.Parse(DepositId), BrandId);
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "拉卡拉-设置押金-返回报文");
                if (content == "fail")
                {
                    return "机具" + PosSn + "设置失败";
                }
                JsonData jsonObj = JsonMapper.ToObject(content);
                if (jsonObj["code"].ToString() != "000000")
                {
                    return jsonObj["message"].ToString();
                }
                content = jsonObj["data"].ToString();
                content = PublicImportDataService.Instance.LklDecrypt(content);
                if (!content.ToLower().Contains("success"))
                {
                    return content;
                }
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "拉卡拉-设置押金-返回报文");
            }
            return "设置成功";
        }
        #endregion



        #region 星驿付-设置押金
        public static string SetXyfDeposit(string PosSn, string DepositId)
        {
            string check = RedisDbconn.Instance.Get<string>("SetXyfPosDepositWait:" + PosSn);
            if (!string.IsNullOrEmpty(check))
            {
                return "操作频繁，请稍后再试";
            }
            RedisDbconn.Instance.Set("SetXyfPosDepositWait:" + PosSn, PosSn);
            RedisDbconn.Instance.SetExpire("SetXyfPosDepositWait:" + PosSn, 10);
            Dictionary<string, object> Obj = new Dictionary<string, object>();
            //判断是否存在关联数据
            Utils.WriteLog(DateTime.Now.ToString() + ":请求参数，" + PosSn + ":" + DepositId, "星驿付-设置押金-返回报文");
            string content = PublicImportDataService.Instance.xyfServiceFee(PosSn, DepositId);
            Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "星驿付-设置押金-返回报文");
            if (content == "fail")
            {
                return "机具" + PosSn + "设置失败";
            }
            JsonData jsonObj = JsonMapper.ToObject(content);
            if (jsonObj["code"].ToString() != "000000")
            {
                return jsonObj["msg"].ToString();
            }
            Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "星驿付-设置押金-返回报文");
            return "设置成功";
        }
        #endregion
    
    
    
    
    
    }
}