using System;
using Library;
using System.Collections.Generic;
using System.Security.Cryptography;

/// <summary>
/// 广电卡相关接口
/// </summary>
namespace MySystem
{
    public class RadioCardHelper
    {
        public readonly static RadioCardHelper Instance = new RadioCardHelper();
        private RadioCardHelper()
        {
        }

        //测试
        // string pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCGvhfAXw0yoPgMwBfX6jgDT6XBjWqOFcGmsm/pfnGlqGSsMYZNjMLzwOMWfhB8IO7dyh4nu3caXjZTbBUJLQtIKcOoM/ZFZZiOXBW2xDg5WPSswmtCvaSmct9rrd0OGvZtNvgimcNjnHbiE1/1LPxwKHWyzdxzRul5NJdg0Vdw1QIDAQAB";
        // string priKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJVLd0ePGZ6fKK1TwcGIxvknF7liDhGyedk3IRxEwTP6P+YS7Y9WaKxDTxIc77ECFSWlvZIPKOXXufMTzwkEGvuAKO+kD9C5R2D+4U3IhztrzcHif8Pn5+I2fVUGuXisUJ+bPxEKSjw7sn5S94uCIsy7o79rHAovAwn+2veLw8xDAgMBAAECgYAqZ8gWnE/jnUOZj9vOFAKoTHFRp8dlQXOYvi76b5ENKXyy5hdyClIq8+dmtq9icLv++IfTD9lHB7J6l9rLrc3XyNmdRSzxSxOT/kPYv1qZoNCqAlTFjWwLNw8tkarcc9NbNXg9UOS/UE97Hs1vlEThOB12cz+5wEF62l+LvZCxeQJBAMrgiujwoKddhCNGuVr3Gac98j4PBqvgtakvYUzvF78gJ0ffngb0J6s1rmGgmaohJc5YE8RwHa4AbIAUjhsdTx0CQQC8YyPgtD44i6+IQMba5zZH1nISSI5yQlPv8dgLXRLDMTnWXx9F7JxXLzAmeJdpkZrdvv1tLzClqDStRClr28rfAkEArFuD6E62TjpWzpmBU9TmiH1KVorxggiQjUHhijj+lbO9a4SBfyrioMQkeHC23CD5O4r8kEEoFfHqPA46hLKryQJAcCHOvDRtCLlUbRSpiKixLYdedLWNXENQtPTN9M2cmmSVmZ0a/0L8OATVc0C6V1MczDqfnDrHiQMa/7sfsJyuHwJBAMLGVR4buHGR/zrnO7FJa4zPw0KK+xUSttDQ9T2+JzV+RcaffmwSm511q+lAZW8e+SrT08WBKhEY8bYUEV95VAM=";
        // string orgNo = "YCJG10010003";
        // string apiKey = "12345678";
        // string host = "https://bmp-test.cbnyunchang.com";

        //生产
        string pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZ+zsPxpoM85OFlHOqN3aauub5PqC8lmfFQKgWz7XITCPsWdYrBY/Niu29tSNchYZq+wrboyhrhCx9PacWeF5hQ0qkOIw/vcLQJe4RWXj3eBMfbqj7a2dJlcFI12TP0m1lCaI5z9lZ1FHtZDzU0lBBiRUCRNsJa/ktrt1MjU7nAQIDAQAB";
        string priKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJBIQq3hyV0EseFd6X+nzRc++bZoljQqqou2NPUW5RD+wLBTk08LGwhGq7hR9WlG95aQ3NHbqNT/Tt8eiH9a8k7FzXHDzGAnGpYTh1fCzSx+lzmRA0E8WUH2CdjSLiNxb4Dxvqcrht5TpgFp2NRaGIIAvB+h8QZ1Uhjux5LGiJaJAgMBAAECgYBl65WmrSOIYOOi9qsWMRvwetz9HL5WFXmp/clFBKpFqh/FrxQVNxweaYWuS78XYjlNlAFZAVkvNTaq6C+1/zHAl3YmRowVz2ut1WoTT5MjZBJsR1d7SnSKp/ZuquKGGPG9l1jbUgY6CfekUhvKzUdk3/k7r3zUzUzEWq4dDdM/XQJBAP2SwEDg78nJxKMBmjKYwEpYYYL6DxTYje9J07/Ms7L8W7OtIj+Xm6I0siBWLe4Y5tVtBzaohxnbzoKfNODaNFMCQQCRqb+5fD02I+Sa62gTQepb1u1HgguS1KfFB0inmwMs+VEFYs73N8LC3PSzONhOLlIV/bzs4CvpnMHyF16qie4zAkEA9GBR2DTv41EIqPAKY2jDKOM0Vr4XeseZQlusLtRg+sYqumbI8vBxlUmjwXZMnYJyNI32vb59NXaY91nn5Ctm8QJAYSwkwtCm5R88ujygyXgHqAzRbgrLPcCvyzj4txcqHy6TZkxsVW/VdbmBZK5fXoh4KkODbWJ+6PGyUJZWiN5LEQJAelIs1KLSVQ+GAzDslXIyxYhTP9yLcxAMbCuQ3tY0jbsyYyB/WuxTvIdwGfGp/GLht36/6RqsRc9JtAyMeXkE1g==";
        string orgNo = "YCJG10010003";
        string apiKey = "n^ZbEjK%";
        string host = "https://bmp.cbnyunchang.com";


        #region 广电卡查询开卡订单数据
        public string QueryCardOrderInfo(string queryBeginTime,string queryEndTime,string pageNo,string pageSize)
        {
            string timestamp = function.getTimeStamp(); //时间戳，精确到秒。签名用参数
            SortedList<string, string> req = new SortedList<string, string>();
            req.Add("queryBeginTime", queryBeginTime); //工号开通时间，开始时间。格式：yyyy-MM-dd
            req.Add("queryEndTime", queryEndTime); //工号开通时间，结束时间。格式：yyyy-MM-dd
            req.Add("pageNo", pageNo); //当前页
            req.Add("pageSize", pageSize);//每页大小
            req.Add("brhNo", orgNo);
            req.Add("timestamp", timestamp);
            string signstr = function.BuildQueryString(req);
            function.WriteLog(DateTime.Now.ToString(), "广电卡查询开卡订单数据");
            function.WriteLog("代签名串\n" + signstr, "广电卡查询开卡订单数据");
            req.Add("sign", Sign(signstr));
            string reqstr = Newtonsoft.Json.JsonConvert.SerializeObject(req);
            function.WriteLog("请求参数\n" + reqstr, "广电卡查询开卡订单数据");
            string reqUrl = host + "/ycsim-business/openapi/query_card_order_info";
            function.WriteLog("请求地址\n" + reqUrl, "广电卡查询开卡订单数据");
            string result = function.PostWebRequest(reqUrl, reqstr, "application/json");
            function.WriteLog("响应数据\n" + result, "广电卡查询开卡订单数据");
            return result;
        }
        #endregion

        private string Sign(string message)
        {
            byte[] keyData = Convert.FromBase64String(priKey);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
            var rsa = RSA.Create();
            rsa.ImportPkcs8PrivateKey(keyData, out _);
            return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
        }

    }
}