using System;
using System.Collections.Generic;
using Library;
using LitJson;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Net;
using MySystem.Models;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;

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

        public string AppId = "wx6e074b8e07b6577e";
        public string AppSecret = "883f9ef7831d8ca82168c3fd6dcfb750";
        public string MchId = "1613112281";
        public string MchName = "四川省润观米科技有限公司";
        public string plateMchId = "1647817950";
        public string serialNo = "14E9FD7F0981BEA23F94137607521CB51C4F76EF";
        public string getSerialNo = "61D99F7218B487788E35D6C4E3ED398E0979D3F6";

        #region 服务商平台商户进件提交申请单API

        public string MerchantCreate(MerchantAddInfo info, MerchantInfo merchant)
        {
            // pubkey = GetPublicKey();

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("business_code", info.BusinessCode); //业务申请编号

            WebCMSEntities dbpxc = new WebCMSEntities();
            //超级管理员信息
            Dictionary<string, object> contact_info = new Dictionary<string, object>();
            contact_info.Add("contact_type", "LEGAL"); //超级管理员姓名
            contact_info.Add("contact_name", RSAEncrypt(info.ContactName)); //超级管理员姓名
            contact_info.Add("contact_id_number", RSAEncrypt(info.ContactIdNumber)); //超级管理员身份证件号码
            // contact_info.Add("openid", info.OpenId); //超级管理员微信openid
            contact_info.Add("mobile_phone", RSAEncrypt(info.MobilePhone)); //联系手机
            contact_info.Add("contact_email", RSAEncrypt(info.ContactEmail)); //联系邮箱
            result.Add("contact_info", contact_info);

            //主体资料
            Dictionary<string, object> subject_info = new Dictionary<string, object>();
            subject_info.Add("subject_type", info.SubjectType); //主体类型

            Dictionary<string, object> business_license_info = new Dictionary<string, object>();
            business_license_info.Add("license_copy", GetMediaId("/" + info.LicenseCopy)); //营业执照照片
            business_license_info.Add("license_number", info.LicenseNumber); //注册号/统一社会信用代码
            business_license_info.Add("merchant_name", merchant.Name); //商户名称
            business_license_info.Add("legal_person", info.LegalPerson); //个体户经营者/法人姓名
            subject_info.Add("business_license_info", business_license_info); //营业执照

            // Dictionary<string, object> certificate_info = new Dictionary<string, object>();
            // certificate_info.Add("cert_copy", GetMediaId("/" + info.CertCopy)); //登记证书照片
            // certificate_info.Add("cert_type", info.CertType); //登记证书类型
            // certificate_info.Add("cert_number", info.CertNumber); //证书号
            // certificate_info.Add("merchant_name", info.CertMerchantName); //商户名称
            // certificate_info.Add("company_address", info.CompanyAddress); //注册地址
            // certificate_info.Add("legal_person", info.CertLegalPerson); //法人姓名
            // certificate_info.Add("period_begin", CheckForever(info.PeriodBegin)); //有效期限开始日期
            // certificate_info.Add("period_end", CheckForever(info.PeriodEnd)); //有效期限结束日期
            // subject_info.Add("certificate_info", certificate_info); //登记证书

            // Dictionary<string, object> organization_info = new Dictionary<string, object>();
            // organization_info.Add("organization_copy", GetMediaId("/" + info.OrganizationCopy)); //组织机构代码证照片
            // organization_info.Add("organization_code", info.OrganizationCode); //组织机构代码
            // organization_info.Add("org_period_begin", CheckForever(info.OrgPeriodBegin)); //组织机构代码证有效期开始日期
            // organization_info.Add("org_period_end", CheckForever(info.OrgPeriodEnd)); //组织机构代码证有效期结束日期
            // subject_info.Add("organization_info", organization_info); //组织机构代码证

            // subject_info.Add("certificate_letter_copy", GetMediaId("/" + info.CertificateLetterCopy)); //单位证明函照片

            Dictionary<string, object> identity_info = new Dictionary<string, object>();
            info.IdDocType = "IDENTIFICATION_TYPE_IDCARD";
            identity_info.Add("id_holder_type", "LEGAL"); //证件持有人类型
            identity_info.Add("id_doc_type", info.IdDocType); //证件类型
            Dictionary<string, object> id_card_info = new Dictionary<string, object>();
            id_card_info.Add("id_card_copy", GetMediaId("/" + info.IdCardCopy)); //身份证人像面照片
            id_card_info.Add("id_card_national", GetMediaId("/" + info.IdCardNational)); //身份证人像面照片
            id_card_info.Add("id_card_name", RSAEncrypt(info.IdCardName)); //身份证姓名
            id_card_info.Add("id_card_number", RSAEncrypt(info.IdCardNumber)); //身份证号码
            id_card_info.Add("id_card_address", RSAEncrypt(info.IdCardAddress)); //身份证居住地址
            id_card_info.Add("card_period_begin", CheckForever(info.CardPeriodBegin)); //身份证有效期开始时间
            id_card_info.Add("card_period_end", CheckForever(info.CardPeriodEnd)); //身份证有效期结束时间
            identity_info.Add("id_card_info", id_card_info); //身份证信息
            // identity_info.Add("owner", info.Owner == 1 ? true : false); //经营者/法人是否为受益人
            if(info.SubjectType == "SUBJECT_TYPE_ENTERPRISE")
            {
                identity_info.Add("owner", true);
            }
            // else
            // {
            //     identity_info.Add("owner", null);
            // }
            subject_info.Add("identity_info", identity_info); //经营者/法人身份证件

            // if (info.Owner == 0)
            // {
            //     Dictionary<string, object> ubo_info = new Dictionary<string, object>();
            //     ubo_info.Add("id_type", info.UboInfoIdType); //证件类型
            //     ubo_info.Add("id_card_copy", GetMediaId("/" + info.UboInfoIdCardCopy)); //身份证人像面照片
            //     ubo_info.Add("id_card_national", GetMediaId("/" + info.UboInfoIdCardNational)); //身份证国徽面照片
            //     ubo_info.Add("id_doc_copy", GetMediaId("/" + info.UboInfoIdDocCopy)); //证件照片
            //     ubo_info.Add("name", RSAEncrypt(info.UboInfoName)); //受益人姓名
            //     ubo_info.Add("id_number", RSAEncrypt(info.UboInfoIdNumber)); //证件号码
            //     ubo_info.Add("id_period_begin", CheckForever(info.UboInfoIdPeriodBegin)); //证件有效期开始时间
            //     ubo_info.Add("id_period_end", CheckForever(info.UboInfoIdPeriodEnd)); //证件有效期结束时间
            //     subject_info.Add("ubo_info", ubo_info); //最终受益人信息(UBO)
            // }

            result.Add("subject_info", subject_info);

            //经营资料
            Dictionary<string, object> business_info = new Dictionary<string, object>();
            business_info.Add("merchant_shortname", info.MerchantShortname); //商户简称
            business_info.Add("service_phone", info.ServicePhone); //客服电话

            Dictionary<string, object> sales_info = new Dictionary<string, object>();
            info.SalesScenesType = "SALES_SCENES_STORE";
            sales_info.Add("sales_scenes_type", info.SalesScenesType.Split(',').ToList()); //经营场景类型

            Dictionary<string, object> biz_store_info = new Dictionary<string, object>();
            biz_store_info.Add("biz_store_name", info.BizStoreName); //门店名称
            OpenBankCityTable BizAddress = dbpxc.OpenBankCityTable.FirstOrDefault(m => m.CityName.EndsWith(info.BizAddressCode)) ?? new OpenBankCityTable();
            biz_store_info.Add("biz_address_code", BizAddress.Code); //门店省市编码
            biz_store_info.Add("biz_store_address", info.BizStoreAddress); //门店地址
            List<string> store_entrance_pic = new List<string>();
            if(!string.IsNullOrEmpty(info.StoreEntrancePic))
            {
                string[] piclist = info.StoreEntrancePic.Split(',');
                foreach(string sub in piclist)
                {
                    store_entrance_pic.Add(GetMediaId("/" + sub));
                }
            }
            biz_store_info.Add("store_entrance_pic", store_entrance_pic); //门店门头照片
            List<string> indoor_pic = new List<string>();
            if(!string.IsNullOrEmpty(info.IndoorPic))
            {
                string[] piclist = info.IndoorPic.Split(',');
                foreach(string sub in piclist)
                {
                    indoor_pic.Add(GetMediaId("/" + sub));
                }
            }
            biz_store_info.Add("indoor_pic", indoor_pic); //店内环境照片
            biz_store_info.Add("biz_sub_appid", info.BizSubAppid); //线下场所对应的商家APPID
            sales_info.Add("biz_store_info", biz_store_info); //线下门店场景

            // Dictionary<string, object> mp_info = new Dictionary<string, object>();
            // mp_info.Add("mp_appid", info.MpAppid); //服务商公众号APPID
            // mp_info.Add("mp_sub_appid", info.MpSubAppid); //商家公众号APPID
            // mp_info.Add("mp_pics", GetMediaId("/" + info.MpPics)); //公众号页面截图
            // sales_info.Add("mp_info", mp_info); //公众号场景

            // Dictionary<string, object> mini_program_info = new Dictionary<string, object>();
            // mini_program_info.Add("mini_program_appid", info.MiniProgramAppid); //服务商小程序APPID
            // mini_program_info.Add("mini_program_sub_appid", info.MiniProgramSubAppid); //商家小程序APPID
            // mini_program_info.Add("mini_program_pics", GetMediaId("/" + info.MiniProgramPics)); //小程序截图
            // sales_info.Add("mini_program_info", mini_program_info); //小程序场景

            // Dictionary<string, object> app_info = new Dictionary<string, object>();
            // app_info.Add("app_appid", info.AppAppid); //服务商应用APPID
            // app_info.Add("app_sub_appid", info.AppSubAppid); //商家应用APPID
            // app_info.Add("app_pics", GetMediaId("/" + info.AppPics)); //商家应用APPID
            // sales_info.Add("app_info", app_info); //APP场景

            // Dictionary<string, object> web_info = new Dictionary<string, object>();
            // web_info.Add("domain", info.WebDomain); //互联网网站域名
            // web_info.Add("web_authorisation", GetMediaId("/" + info.WebAuthorisation)); //网站授权函
            // web_info.Add("web_appid", info.WebAppId); //互联网网站对应的商家APPID
            // sales_info.Add("web_info", web_info); //互联网网站场景

            // Dictionary<string, object> wework_info = new Dictionary<string, object>();
            // wework_info.Add("sub_corp_id", info.SubCorpId); //商家企业微信CorpID
            // wework_info.Add("wework_pics", GetMediaId("/" + info.WeworkPics)); //企业微信页面截图
            // sales_info.Add("wework_info", wework_info); //企业微信场景

            business_info.Add("sales_info", sales_info); //经营场景

            result.Add("business_info", business_info);

            //结算规则
            Dictionary<string, object> settlement_info = new Dictionary<string, object>();
            settlement_info.Add("settlement_id", info.SettlementId); //入驻结算规则ID
            string QualificationType = function.CheckNull(info.QualificationType);
            if (QualificationType.Contains(","))
            {
                QualificationType = QualificationType.Split(',')[0];
            }
            settlement_info.Add("qualification_type", QualificationType); //所属行业
            if (!string.IsNullOrEmpty(info.Qualifications))
            {
                List<string> Qualifications = new List<string>();
                Qualifications.Add(GetMediaId("/" + info.Qualifications));
                settlement_info.Add("qualifications", Qualifications); //特殊资质图片
            }
            settlement_info.Add("activities_id", info.ActivitiesId); //优惠费率活动ID
            settlement_info.Add("activities_rate", info.ActivitiesRate); //优惠费率活动值
            result.Add("settlement_info", settlement_info);

            //结算银行账户
            Dictionary<string, object> bank_account_info = new Dictionary<string, object>();
            bank_account_info.Add("bank_account_type", info.BankAccountType); //账户类型
            bank_account_info.Add("account_name", RSAEncrypt(info.AccountName)); //开户名称
            bank_account_info.Add("account_bank", info.AccountBank); //开户银行
            OpenBankCityTable BankAddress = dbpxc.OpenBankCityTable.FirstOrDefault(m => m.CityName.EndsWith(info.BankAddressCode)) ?? new OpenBankCityTable();
            bank_account_info.Add("bank_address_code", BankAddress.Code); //开户银行省市编码
            OpenBankTable BankBranch = dbpxc.OpenBankTable.FirstOrDefault(m => m.BankName == info.BankName) ?? new OpenBankTable();
            bank_account_info.Add("bank_branch_id", BankBranch.BankCode); //开户银行联行号
            bank_account_info.Add("bank_name", info.BankName); //开户银行全称（含支行）
            bank_account_info.Add("account_number", RSAEncrypt(info.AccountNumber)); //银行账号
            result.Add("bank_account_info", bank_account_info);
            dbpxc.Dispose();

            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            Dictionary<string, string> header = new Dictionary<string, string>();
            // 如何查看证书序列号？
            // 登陆商户平台【API安全】->【API证书】->【查看证书】，可查看商户API证书序列号。
            // 商户API证书和微信支付平台证书均可以使用第三方的证书解析工具，查看证书内容。或者使用openssl命令行工具查看证书序列号。
            // $ openssl x509 -in 1900009191_20180326_cert.pem -noout -serial
            // serial=1DDE55AD98ED71D6EDD4A4A16996DE7B47773A8C
            LogHelper.Instance.WriteLog(req, "服务商平台商户进件提交申请单API");
            string resp = postJson("https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/", req);
            LogHelper.Instance.WriteLog(resp + "\n\n", "服务商平台商户进件提交申请单API");
            return resp;
        }

        #endregion

        #region 服务商平台查询申请单状态API

        public Dictionary<string, object> QueryMerchant(string BusinessCode)
        {
            Dictionary<string, object> return_result = new Dictionary<string, object>();
            try
            {
                string result = postJson("https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/business_code/" + BusinessCode, "", "GET");
                
                //{\"applyment_id\":2000002247709762,\"applyment_state\":\"APPLYMENT_STATE_FINISHED\",\"applyment_state_msg\":\"商户入驻申请已完成\",\"audit_detail\":[],\"business_code\":\"0123456789\",\"sign_url\":\"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFv7zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyUXE1czkzb3JlUjIxZHpXbTF5Y2YAAgRjbe9hAwQAjScA\",\"sub_mchid\":\"1619775262\"}
                JsonData jsonObj = JsonMapper.ToObject(result);
                bool logFlag = true;
                if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_AUDITING" || jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_SIGNED" || jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_CONFIRMED")
                {
                    string check = RedisDbconn.Instance.Get<string>("WeChatQueryMerchant:" + BusinessCode + ":" + jsonObj["applyment_state"].ToString());
                    if(!string.IsNullOrEmpty(check))
                    {
                        logFlag = false;
                    }
                    else
                    {
                        RedisDbconn.Instance.Set("WeChatQueryMerchant:" + BusinessCode + ":" + jsonObj["applyment_state"].ToString(), result);
                        RedisDbconn.Instance.SetExpire("WeChatQueryMerchant:" + BusinessCode + ":" + jsonObj["applyment_state"].ToString(), 7200);
                    }
                }
                if(logFlag)
                {
                    LogHelper.Instance.WriteLog(BusinessCode, "服务商平台查询申请单状态API");
                    LogHelper.Instance.WriteLog(result + "\n\n", "服务商平台查询申请单状态API");
                }
                return_result.Add("applyment_id", jsonObj["applyment_id"].ToString()); //微信支付申请单号
                return_result.Add("applyment_state", jsonObj["applyment_state"].ToString()); //申请单状态
                return_result.Add("applyment_state_msg", jsonObj["applyment_state_msg"].ToString()); //申请状态描述
                if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_TO_BE_SIGNED" || jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_FINISHED")
                {
                    return_result.Add("sub_mchid", jsonObj["sub_mchid"].ToString());
                }
                if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
                {
                    List<Dictionary<string, string>> audit_detail = new List<Dictionary<string, string>>();
                    JsonData auditObj = jsonObj["audit_detail"]; //驳回原因详情
                    // for (int i = 0; i < auditObj.Count; i++)
                    // {
                    //     Dictionary<string, string> row = new Dictionary<string, string>();
                    //     // row.Add("field", auditObj[i]["field"].ToString()); //字段名
                    //     // row.Add("field_name", auditObj[i]["field_name"].ToString()); //字段名称
                    //     row.Add("reject_reason", auditObj[i]["reject_reason"].ToString()); //驳回原因
                    //     audit_detail.Add(row);
                    // }
                    return_result.Add("reject_reason", auditObj[0]["reject_reason"].ToString());
                    return_result.Add("audit_detail", audit_detail); //申请状态描述
                }
                if (result.Contains("\"sign_url\":"))
                { 
                    return_result.Add("sign_url", jsonObj["sign_url"].ToString()); //进件通过申请，待商家签约的地址
                }
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "服务商平台查询申请单状态API异常");
            }
            return return_result;
        }
        #endregion
        
        #region 服务商平台申请退款API

        public Dictionary<string, string> Refund(string SubMchId, decimal RefundMoney, decimal TotalMoney, string OrderNo, string RefundNo, string Reason = "")
        {
            Dictionary<string, string> return_result = new Dictionary<string, string>();
            int refund = (int)(RefundMoney * 100);
            int amount = (int)(TotalMoney * 100);
            //具体请求参数
            SortedList<string, object> par = new SortedList<string, object>();
            par.Add("sub_mchid", SubMchId); //子商户号
            par.Add("out_trade_no", OrderNo); //商户订单号
            par.Add("out_refund_no", RefundNo); //商户退款单号
            par.Add("reason", Reason); //退款原因
            Dictionary<string, object> amountDic = new Dictionary<string, object>();
            amountDic.Add("refund", refund); //退款金额
            amountDic.Add("total", amount); //原订单金额
            amountDic.Add("currency", "CNY"); //退款币种
            par.Add("amount", amountDic); //金额信息

            string req = Newtonsoft.Json.JsonConvert.SerializeObject(par);
            try
            {
                LogHelper.Instance.WriteLog(req, "服务商平台申请退款API");
                string result = postJson("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds", req);
                LogHelper.Instance.WriteLog(result + "\n\n", "服务商平台申请退款API");
                JsonData jsonObj = JsonMapper.ToObject(result);
                return_result.Add("refund_id", jsonObj["refund_id"].ToString()); //微信支付退款单号
                return_result.Add("channel", jsonObj["channel"].ToString()); //退款渠道
                return_result.Add("user_received_account", jsonObj["user_received_account"].ToString()); //退款入账账户
                return_result.Add("status", jsonObj["status"].ToString()); //退款状态
                return_result.Add("payer_total", jsonObj["amount"]["payer_total"].ToString()); //用户支付金额
                return_result.Add("payer_refund", jsonObj["amount"]["payer_refund"].ToString()); //用户退款金额
                return_result.Add("settlement_refund", jsonObj["amount"]["settlement_refund"].ToString()); //应结退款金额
                return_result.Add("settlement_total", jsonObj["amount"]["settlement_total"].ToString()); //应结订单金额
                return_result.Add("discount_refund", jsonObj["amount"]["discount_refund"].ToString()); //优惠退款金额
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "服务商平台申请退款API异常");
            }
            return return_result;
        }
        #endregion

        #region 服务商平台请求分账API

        public string ProfitShare(string SubMchId, string TradeNo, string OrderNo, List<ReceiverList> Receivers) //有活动分0.62%，没活动不分账
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("sub_mchid", SubMchId); //子商户号
            result.Add("appid", AppId); //应用ID
            result.Add("transaction_id", TradeNo); //微信订单号
            result.Add("out_order_no", OrderNo); //商户分账单号
            result.Add("receivers", Receivers); //分账接收方列表
            result.Add("unfreeze_unsplit", true); //是否解冻剩余未分资金
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            LogHelper.Instance.WriteLog(req, "服务商平台请求分账API");
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/orders", req);
            LogHelper.Instance.WriteLog(resp + "\n\n", "服务商平台请求分账API");
            return resp;
            //{"order_id":"30000104462023060249559178531","out_order_no":"2023060210583730173782834","receivers":[{"account":"1611167423","amount":3,"create_time":"2023-06-02T11:34:10+08:00","description":"服务费","detail_id":"36000104462023060269999025889","finish_time":"1970-01-01T08:00:00+08:00","result":"PENDING","type":"MERCHANT_ID"},{"account":"1645266943","amount":7,"create_time":"2023-06-02T11:34:10+08:00","description":"解冻给分账方","detail_id":"36000104462023060269999025890","finish_time":"1970-01-01T08:00:00+08:00","result":"PENDING","type":"MERCHANT_ID"}],"state":"PROCESSING","sub_mchid":"1645266943","transaction_id":"4200001833202306025522158377"}
        }
        #endregion

        #region 服务商平台添加分账接收方API

        public string AddReceive(string SubMchId, string TypeName, string Account, string Name, string Relation)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("sub_mchid", SubMchId); //子商户号
            result.Add("appid", AppId); //应用ID
            result.Add("type", TypeName); //分账接收方类型 MERCHANT_ID：商户ID  PERSONAL_OPENID：个人openid（由父商户APPID转换得到） PERSONAL_SUB_OPENID：个人sub_openid（由子商户APPID转换得到）
            result.Add("account", Account); //分账接收方账号
            if(!string.IsNullOrEmpty(Name))
            {
                result.Add("name", RSAEncrypt(Name)); //分账个人接收方姓名
            }
            result.Add("relation_type", Relation); //与分账方的关系类型 STORE：门店 STAFF：员工 STORE_OWNER：店主 PARTNER：合作伙伴 HEADQUARTER：总部 BRAND：品牌方 DISTRIBUTOR：分销商 USER：用户 SUPPLIER： 供应商 CUSTOM：自定义
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            LogHelper.Instance.WriteLog(req, "服务商平台添加分账接收方API");
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/receivers/add", req);
            LogHelper.Instance.WriteLog(resp + "\n\n", "服务商平台添加分账接收方API");
            return resp;
            //{"account":"1611167423","name":"x+8m5jM4x2jSZLDOpN46Js9JyeNgUXkFSmGjCBN45QcSYUNPN86TOheRF64WlwDeO45E/walWPeZSksNN/LfJiB2fZRVun1CJuJ7zxePma8L7iDbKS92i+d38rvOmNTjLhKX64O8oJR4HMtFw1HryxJlUFgM5/ickJ79WGrX3GaVArWisgEzwOyxGBfGSL3ofJhi5TdTnrxHMzqYWv43DRvI9Ja5eMgFbgrGcn+JiJVKQcYj1L8tZ6xQeQCXkY6ubyDiV9wQps2nQA1Oh+UdFcS13Ex4FUSYLCczGFAl4EV8TBJQleZeC1p4ZJquJjje0uMjI2xB6S41yUBoN86gkA==","relation_type":"PARTNER","sub_mchid":"1645266943","type":"MERCHANT_ID"}
        }
        #endregion

        #region 服务商平台查询分账结果API

        public string QueryProfitShare(string SubMchId, string TradeNo, string OrderNo)
        {
            string req = "";
            req += "sub_mchid=" + SubMchId + "&"; //子商户号
            req += "transaction_id=" + TradeNo + "&"; //微信订单号
            LogHelper.Instance.WriteLog(req, "服务商平台查询分账结果API");
            string resp = postJson("https://api.mch.weixin.qq.com/v3/profitsharing/orders/" + OrderNo + "?" + req, "", "GET");
            LogHelper.Instance.WriteLog(resp + "\n\n", "服务商平台查询分账结果API");
            return resp;
            //{"order_id":"30000404702023060849818491190","out_order_no":"2023060816062365164446474","receivers":[{"account":"1611167423","amount":3,"create_time":"2023-06-08T17:55:45+08:00","description":"服务费","detail_id":"36000404702023060870422622364","finish_time":"2023-06-08T17:56:16+08:00","result":"SUCCESS","type":"MERCHANT_ID"},{"account":"1646405429","amount":151,"create_time":"2023-06-08T17:55:45+08:00","description":"解冻给分账方","detail_id":"36000404702023060870422622365","finish_time":"2023-06-08T17:56:16+08:00","result":"SUCCESS","type":"MERCHANT_ID"}],"state":"FINISHED","sub_mchid":"1646405429","transaction_id":"4200001821202306083551994076"}
        }
        #endregion




        #region 敏感信息加密
        string pubkey = "MIID3DCCAsSgAwIBAgIUYdmfchi0h3iONdbE4+05jgl50/YwDQYJKoZIhvcNAQELBQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsTFFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3QgQ0EwHhcNMjIwMTIzMDYwMjA4WhcNMjcwMTIyMDYwMjA4WjBuMRgwFgYDVQQDDA9UZW5wYXkuY29tIHNpZ24xEzARBgNVBAoMClRlbnBheS5jb20xHTAbBgNVBAsMFFRlbnBheS5jb20gQ0EgQ2VudGVyMQswCQYDVQQGDAJDTjERMA8GA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igUF1/vjdqhr/Z9U6gKeEu/YN0lxH3JotNryuJNBeG/7XaHPWZcHSxMA/fl7NjaiZVpOcTyEFeAoG5ECgYBimNzthVzVAK5oN8ChIV23iou7wMVaPQ3eWdKhec0GAH+Jv5V8GHGlbZeBiUiBMo62fwtTru+j3zJgMum+PlZSHz903J2bSzo6x0N2/6/TYUjVsXFLlpS7zbMvJnh//9B8wGhnhzSfW38MA523CIw7sU+VUQQHx6/2vLwT84SfsQ=="; //私钥
        public string RSAEncrypt(string text)
        {
            byte[] publicKey = Convert.FromBase64String(pubkey);
            // var rsa = RSA.Create();
            // rsa.ImportRSAPublicKey(publicKey, out _);
            // var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);
            // return Convert.ToBase64String(buff);
            using (var x509 = new X509Certificate2(publicKey))
            {
                using (var rsa = x509.GetRSAPublicKey())
                {
                    var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);
                    return Convert.ToBase64String(buff);
                }
            }
        }
        // public string RSADecrypt(string text)
        // {
        //     // byte[] key = Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey);
        //     // byte[] nonce = Encoding.UTF8.GetBytes("1234567890ab");
        //     // byte[] ciphertext = Convert.FromBase64String(text);

        //     // AesGcm aes = new AesGcm(key);
        //     // byte[] plaintext = new byte[ciphertext.Length];
        //     // aes.Decrypt(nonce, ciphertext, plaintext, null);
        //     // return Encoding.UTF8.GetString(plaintext);
        // }
        public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
        {
            // GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
            // AeadParameters aeadParameters = new AeadParameters(
            //     new KeyParameter(Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey)), 
            //     128, 
            //     Encoding.UTF8.GetBytes(nonce), 
            //     Encoding.UTF8.GetBytes(associatedData));
            // gcmBlockCipher.Init(false, aeadParameters);
    
            // byte[] data = Convert.FromBase64String(ciphertext);
            // byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
            // int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
            // gcmBlockCipher.DoFinal(plaintext, length);
            // return Encoding.UTF8.GetString(plaintext);

            byte[] key = Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey); // 256-bit key
            byte[] nonceByte = Encoding.UTF8.GetBytes(nonce); // 96-bit nonce
            byte[] cipherByte = Convert.FromBase64String(ciphertext);
            byte[] associatedByte = Encoding.UTF8.GetBytes(associatedData);

            GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
            AeadParameters parameters = new AeadParameters(new KeyParameter(key), 128, nonceByte, associatedByte);
            cipher.Init(false, parameters);

            byte[] plaintext = new byte[cipher.GetOutputSize(cipherByte.Length)];
            int len = cipher.ProcessBytes(cipherByte, 0, cipherByte.Length, plaintext, 0);
            cipher.DoFinal(plaintext, len);
            return Encoding.UTF8.GetString(plaintext);
        }
        public string postJson(string url, string postData, string method = "POST")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
            request.Accept = "application/json";

            string Authorization = GetAuthorization(url, method, postData, prikey, MchId, serialNo);
            request.Headers.Add("Authorization", Authorization);
            request.Headers.Add("Wechatpay-Serial", getSerialNo);
            if (!string.IsNullOrEmpty(postData))
            {
                byte[] paramJsonBytes;
                paramJsonBytes = System.Text.Encoding.UTF8.GetBytes(postData);
                request.ContentLength = paramJsonBytes.Length;
                Stream writer;
                try
                {
                    writer = request.GetRequestStream();
                }
                catch (Exception ex)
                {
                    writer = null;
                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "http请求异常");
                }
                writer.Write(paramJsonBytes, 0, paramJsonBytes.Length);
                writer.Close();
            }

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }
            Stream resStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(resStream);
            string text = reader.ReadToEnd();
            return text;
            //{\"code\":\"PARAM_ERROR\",\"message\":\"请确认待处理的消息是否为加密后的密文\"}
            //{\"applyment_id\":2000002247709762}
        }

        #endregion

        #region 图片上传

        public string GetMediaId(string imgPath)
        {
            if (string.IsNullOrEmpty(imgPath))
            {
                return "";
            }
            if (imgPath.Contains(","))
            {
                imgPath = imgPath.Split(',')[0];
            }
            string key = "wechatpic:" + function.MD5_16(imgPath);
            string media_id = RedisDbconn.Instance.Get<string>(key);
            if (!string.IsNullOrEmpty(media_id))
            {
                return media_id;
            }
            string filePath = function.getPath(imgPath);
            var filename = Path.GetFileName(filePath);
            // FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            // Byte[] imgBytesIn = new Byte[fs.Length];
            // fs.Read(imgBytesIn, 0, imgBytesIn.Length);
            // fs.Close();
            byte[] imgBytesIn = PublicFunction.GetNetFileData(AppConfig.Base.OssHost + imgPath);

            byte[] hash = SHA256Managed.Create().ComputeHash(imgBytesIn);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("x2"));
            }
            var sha256 = builder.ToString();
            string metaStr = "{\"filename\":\""+ filename + "\",\"sha256\":\"" + sha256 + "\"}";
            media_id = UploadImgApi(metaStr, imgBytesIn, filename);
            RedisDbconn.Instance.Set(key, media_id);
            return media_id;
        }

        public string UploadImgApi(string metaStr, Byte[] imgBytesIn,string filename)
        {
            string url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";
            
            string merchantId = MchId;   //商户号
            string privateKey = prikey;
            #region 定义请求体中的内容 并转成二进制

            string boundary = "lc199aecd61b4653ef";
            string Enter = "\r\n";
            string campaignIDStr1
                = "--" + boundary
                + Enter
                + "Content-Disposition: form-data; name=\"meta\";"
                + Enter
                + "Content-Type:application/json;"
                + Enter
                + Enter
                + metaStr
                + Enter
                + "--" + boundary
                + Enter
                + "Content-Disposition:form-data;name=\"file\";filename=\""+ filename + "\";"
                + Enter
                + "Content-Type:image/jpeg"
                + Enter
                + Enter;
            byte[] byteData2
                = imgBytesIn;
            string campaignIDStr3 
                = Enter
                + "--" + boundary 
                + Enter;
            var byteData1 = System.Text.Encoding.UTF8.GetBytes(campaignIDStr1);

            var byteData3 = System.Text.Encoding.UTF8.GetBytes(campaignIDStr3);
            #endregion

            string transactionsResponse = UploadImg_postJson(url, byteData1, byteData2, byteData3, metaStr, privateKey, merchantId, serialNo, boundary, "POST");
            var result=JsonMapper.ToObject(transactionsResponse);
            return result["media_id"].ToString();
        }
        public string UploadImg_postJson(string url, byte[] b1, byte[] b2, byte[] b3, string metaStr, string privateKey, string merchantId, string serialNo, string boundary, string method = "POST")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            //request.ContentType = "application/json;charset=UTF-8";
            request.ContentType = "multipart/form-data;boundary=" + boundary;
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
            request.Accept = "application/json";
            string Authorization = GetAuthorization(url, method, metaStr, privateKey, merchantId, serialNo);
            request.Headers.Add("Authorization", Authorization);

            Stream writer;
            try
            {
                writer = request.GetRequestStream();
            }
            catch (Exception)
            {
                writer = null;
            }
            writer.Write(b1, 0, b1.Length);
            writer.Write(b2, 0, b2.Length);
            writer.Write(b3, 0, b3.Length);
            writer.Close();

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }
            Stream resStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(resStream);
            string text = reader.ReadToEnd();
            return text;
        }
        protected string GetAuthorization(string url, string method, string jsonParame, string privateKey, string merchantId, string serialNo)
        {
            var uri = new Uri(url);
            string urlPath = uri.PathAndQuery;
            string nonce = Guid.NewGuid().ToString();
            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
            //数据签名     HTTP请求方法\n接口地址的url\n请求时间戳\n请求随机串\n请求报文主体\n
            method = string.IsNullOrEmpty(method) ? "" : method;
            string message = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n", method, urlPath, timestamp, nonce, jsonParame);
            string signTxt = Sign(message, privateKey);

            //Authorization和格式
            string authorzationTxt = string.Format("WECHATPAY2-SHA256-RSA2048 mchid=\"{0}\",nonce_str=\"{1}\",timestamp=\"{2}\",serial_no=\"{3}\",signature=\"{4}\"",
                merchantId,
                nonce,
                timestamp,
                serialNo,
                signTxt
                );
            return authorzationTxt;
        }

        protected string Sign(string message, string privateKey)
        {
            byte[] keyData = Convert.FromBase64String(privateKey);
            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));

            // using (CngKey cngKey = CngKey.Import(keyData, CngKeyBlobFormat.Pkcs8PrivateBlob))
            // using (RSACng rsa = new RSACng(cngKey))
            // {
            //     return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
            // }
        }

        #endregion

        #region 获取文件sha256

        public string GetSha256(FileStream stream)
        { 
            using (SHA256 mySHA256 = SHA256.Create())
            {
                byte[] hashValue = mySHA256.ComputeHash(stream);
                return Encoding.UTF8.GetString(hashValue);
            }
        }

        #endregion

        #region 判断长期

        public string CheckForever(DateTime? time)
        {
            if (time == null)
            {
                return "";
            }
            if (time.Value.Year >= 2050)
            {
                return "长期";
            }
            return time.Value.ToString("yyyy-MM-dd");
        }

        #endregion


    }
}
