using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMCryptoUtils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SMCryptoUtilsTest
{
    internal class Program
    {
        static void Main(string[] args)
        {

            //应用ID
            String appId = "57E2D561E97141A3871DEFB410ADD920";
            //应用秘钥
            String appSecret = "F30D0D261BA04063A0BCAEF19F8ADBCC";
            //应用私钥
            String privateKey = "AKyhfvPbT+tIJHJkZSDILrw7t+FOG1U58UcupYYTzCOj";
            //平台公钥（为了测试用例能验签成功，平台公钥当前赋值为应用公钥的值，应用公钥存放于处方中心不对外提供，实际上拿到的平台公钥是用于验签处方中心返回的数据，不能用于此demo）
            String publicKey = "BIFd2+2CgjuPAj5FMj5L/L3azTWu86suPtlIJkCo8zjQ44R7SQUUkTgZGdVelRQCM5pW+x9tZGzDPaUNbfD499w=";

            JObject requestData = new JObject();
            requestData.Add("appId", appId);
            requestData.Add("encType", "SM4");
            requestData.Add("signType", "SM2");
            requestData.Add("timestamp", (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds);

            JObject data = new JObject();
            //  data.Add("userName", "张三");
            /***********说明：此次data对象替换为实际传递的业务数据结构，以下结构仅供测试参考。***********/
            data = JObject.Parse("{\n" +
                "    \"insuPlcNo\": \"350100\",\n" +
                "    \"hospRxno\": \"3504874534935555553312259665232132\",\n" +
                "    \"prscTime\": \"2022-06-06 10:10:00\",\n" +
                "    \"valiDays\": 1,\n" +
                "    \"valiEndTime\": \"2022-06-07 10:10:00\",\n" +
                "    \"authNo\": \"\",\n" +
                "    \"bizTypeCode\": \"01\",\n" +
                "    \"diseinfo\": [\n" +
                "        {\n" +
                "            \"diagCode\": \"E11.900\",\n" +
                "            \"diagDept\": \"心血管内科\",\n" +
                "            \"diagDrName\": \"吴丽雅\",\n" +
                "            \"diagDrNo\": \"PTYS0000818231\",\n" +
                "            \"diagName\": \"急性胃炎\",\n" +
                "            \"diagSrtNo\": \"1\",\n" +
                "            \"diagTime\": \"2022-04-07 10:10:00\",\n" +
                "            \"diagType\": \"1\",\n" +
                "            \"maindiagFlag\": \"1\",\n" +
                "            \"tcmDiseCode\": \"\",\n" +
                "            \"tcmDiseName\": \"\",\n" +
                "            \"tcmsymp\": \"\",\n" +
                "            \"tcmsympCode\": \"\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"ecToken\": \"\",\n" +
                "    \"initRxno\": \"\",\n" +
                "    \"longRxFlag\": \"1\",\n" +
                "    \"maxReptCnt\": 0,\n" +
                "    \"mdtrtCertType\": \"02\",\n" +
                "    \"mdtrtCertNo\": \"110101198811090049\",\n" +
                "    \"cardSn\":\"\",\n" +
                "    \"mdtrtinfo\": {\n" +
                "        \"algsHis\": \"注射过敏\",\n" +
                "        \"certno\": \"110101198811090049\",\n" +
                "        \"diseCodg\": \"\",\n" +
                "        \"diseCondDscr\": \"\",\n" +
                "        \"diseName\": \"\",\n" +
                "        \"drCode\": \"YS9083982313\",\n" +
                "        \"drDeptCode\": \"KSBM89981\",\n" +
                "        \"drDeptName\": \"外科\",\n" +
                "        \"drProfttlCodg\": \"ZCBM29931\",\n" +
                "        \"drProfttlName\": \"主任医师\",\n" +
                "        \"extras\": {\n" +
                "            \"1\": \"1\"\n" +
                "        },\n" +
                "        \"fixmedinsCode\": \"H35010200364\",\n" +
                "        \"fixmedinsName\": \"福建中医药大学附属第二人民医院\",\n" +
                "        \"fstdiagFlag\": \"0\",\n" +
                "        \"gend\": \"1\",\n" +
                "        \"gesoVal\": 0,\n" +
                "        \"hiFeesetlName\": \"本地医疗保险\",\n" +
                "        \"hiFeesetlType\": \"2\",\n" +
                "        \"insutype\": \"310\",\n" +
                "        \"iptOtpNo\": \"ZYMZ8909232131231\",\n" +
                "        \"maindiagCode\": \"E11.900\",\n" +
                "        \"maindiagName\": \"急性胃炎\",\n" +
                "        \"mdtrtId\": \"JZID888888\",\n" +
                "        \"mdtrtTime\": \"2022-05-19 14:10:00\",\n" +
                "        \"medType\": \"11\",\n" +
                "        \"medTypeName\": \"普通门诊\",\n" +
                "        \"medfeeSumamt\": 36.88,\n" +
                "        \"nwbAge\": \"\",\n" +
                "        \"nwbFlag\": \"0\",\n" +
                "        \"otpIptFlag\": \"1\",\n" +
                "        \"patnAge\": 27,\n" +
                "        \"patnHgt\": 180,\n" +
                "        \"patnName\": \"张三三\",\n" +
                "        \"patnWt\": 80,\n" +
                "        \"pharCertType\": \"01\",\n" +
                "        \"pharCertno\": \"350401198510101010\",\n" +
                "        \"pharChkTime\": \"2022-04-07 10:10:01\",\n" +
                "        \"pharCode\": \"PTYS000088232\",\n" +
                "        \"pharDeptCode\": \"KS989831\",\n" +
                "        \"pharDeptName\": \"内科\",\n" +
                "        \"pharName\": \"林玉英\",\n" +
                "        \"pharPracCertNo\": \"YSBH99032313\",\n" +
                "        \"pharProfttlCodg\": \"01\",\n" +
                "        \"pharProfttlName\": \"高级药师\",\n" +
                "        \"prscDeptCode\": \"KFKS98231\",\n" +
                "        \"prscDeptName\": \"内科\",\n" +
                "        \"prscDrCertType\": \"01\",\n" +
                "        \"prscDrCertno\": \"350422196510101010\",\n" +
                "        \"prscDrName\": \"吴丽雅\",\n" +
                "        \"psnCertType\": \"01\",\n" +
                "        \"psnNo\": \"RYBH38384\",\n" +
                "        \"rgstFee\": 15.88,\n" +
                "        \"spDiseFlag\": \"1\",\n" +
                "        \"suckPrdFlag\": \"0\"\n" +
                "    },\n" +
                "    \"minInrvDays\": 2,\n" +
                "    \"reptFlag\": \"0\",\n" +
                "    \"reptdCnt\": 0,\n" +
                "    \"rxCotnFlag\": \"0\",\n" +
                "    \"rxDoscnt\": 1,\n" +
                "    \"rxDosePrdDays\": 3,\n" +
                "    \"rxDosunt\": \"包\",\n" +
                "    \"rxDrordDscr\": \"多喝水\",\n" +
                "    \"rxDrugCnt\": 1,\n" +
                "    \"rxFrquCodg\": \"13\",\n" +
                "    \"rxFrquName\": \"每天三次（tid）\",\n" +
                "    \"rxTypeCode\": \"1\",\n" +
                "    \"rxUsedWayCodg\": \"1\",\n" +
                "    \"rxUsedWayName\": \"口服\",\n" +
                "    \"rxdrugdetail\": [\n" +
                "        {\n" +
                "            \"basMednFlag\": \"1\",\n" +
                "            \"chemname\": \"恩替卡韦口服溶液\",\n" +
                "            \"diseCodg\": \"BZML000000001\",\n" +
                "            \"drugCnt\": 1,\n" +
                "            \"drugDosform\": \"片剂\",\n" +
                "            \"drugDosunt\": \"盒\",\n" +
                "            \"drugGenname\": \"恩替卡韦口服溶液\",\n" +
                "            \"drugPric\": 1.5,\n" +
                "            \"drugProdname\": \"恩替卡韦口服溶液\",\n" +
                "            \"drugSpec\": \"1支/盒\",\n" +
                "            \"drugSumamt\": 27,\n" +
                "            \"drugTotlcnt\": \"1\",\n" +
                "            \"drugTotlcntEmp\": \"盒\",\n" +
                "            \"drugstdcode\": \"BWM99-03131\",\n" +
                "            \"extras\": {},\n" +
                "            \"fixmedinsHilistId\": \"\",\n" +
                "            \"hospApprFlag\": \"1\",\n" +
                "            \"hospPrepFlag\": \"0\",\n" +
                "            \"impDrugFlag\": \"0\",\n" +
                "            \"mainMedcFlag\": \"\",\n" +
                "            \"medListCodg\": \"XJ05AFE013X001010108569\",\n" +
                "            \"medcBegntime\": \"2022-04-08 10:10:00\",\n" +
                "            \"medcDays\": 0,\n" +
                "            \"medcEndtime\": \"2022-04-08 10:10:00\",\n" +
                "            \"medcWayCodg\": \"1\",\n" +
                "            \"medcWayDscr\": \"口服\",\n" +
                "            \"mednTypeCode\": \"0100\",\n" +
                "            \"mednTypeName\": \"抗生素类抗感染药物\",\n" +
                "            \"prdrName\": \"成都第一制药有限公司\",\n" +
                "            \"prodBarc\": \"111111111\",\n" +
                "            \"rxItemTypeCode\": \"11\",\n" +
                "            \"rxItemTypeName\": \"西药\",\n" +
                "            \"sinDoscnt\": 2,\n" +
                "            \"sinDosunt\": \"片\",\n" +
                "            \"tcmdrugTypeCode\": \"1\",\n" +
                "            \"tcmdrugTypeName\": \"\",\n" +
                "            \"tcmherbFoote\": \"\",\n" +
                "            \"urgtFlag\": \"\",\n" +
                "            \"usedFrquCodg\": \"13\",\n" +
                "            \"usedFrquName\": \"每天三次（tid)\"\n" +
                "        }\n" +
                "    ]\n" +
                "}");
            
            SMUtil sMUtil = new SMUtil();

            //加密
            String encData = sMUtil.encrypt(JsonConvert.SerializeObject(data, Formatting.None), appId, appSecret);
            Console.WriteLine("加密：" + encData);

            JObject signDto = (JObject)JObject.Parse(JsonConvert.SerializeObject(requestData, Formatting.None));
            signDto.Add("data", data);

            //加签
            String signData = sMUtil.sign(signDto, appSecret, privateKey);
            Console.WriteLine("加签：" + signData);
            //报文
            requestData.Add("encData", encData);
            requestData.Add("signData", signData);
            Console.WriteLine("报文：" + JsonConvert.SerializeObject(requestData, Formatting.None));
            //解密
            String decData = sMUtil.decrypt(encData, appId, appSecret);
            Console.WriteLine("解密：" + decData);
            //验签
            Boolean isVerify = sMUtil.verify(signDto, appSecret, publicKey, signData);
            Console.WriteLine("验签：" + isVerify);
            Console.ReadKey();
        }
    }
}
