/******************************************************************************
 * 文件名称: CardReader.cs
 * 文件说明: 读卡的封装，包括社保卡，身份证，电子凭证等等
 * 当前版本: V1.0
 * 创建日期: 2022-06-20
 *
 * * 2020-06-20: 增加 CardReader 类
 * *****  2020-06-20: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-20: 增加 CardReader 方法重载，国家版电子凭证使用
 * *****  2020-06-20: 增加 Init 方法，SSCard环境初始化
 * *****  2020-06-20: 增加 ReadCardBas 方法，读社保卡
 * *****  2020-06-20: 增加 VerifyPIN 方法，验证密码
 * *****  2020-06-20: 增加 ChangePIN 方法，修改密码
 * *****  2020-06-20: 增加 ReadSFZ 方法，读身份证
 * *****  2020-06-20: 增加 GetQRBase 方法，读二维码
 * *****  2020-06-20: 增加 NationEcTrans 方法，读电子凭证（国家版）


******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using PTMedicalInsurance.Variables;
using System.Windows.Forms;

namespace PTMedicalInsurance.Common
{
    /// <summary>
    /// 通用读卡动态库调用
    /// </summary>
    class CardReader
    {
        const string DllPath = @"D:\CardDLL\SSCard.dll";

        [DllImport("SSCard.dll", EntryPoint = "Init", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int Init(StringBuilder pURL, StringBuilder pUser);

        [DllImport("SSCard.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadCardBas(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "VerifyPIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int VerifyPIN(StringBuilder pOutBuff, int nOutBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "ChangePIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ChangePIN(StringBuilder pOutBuff, int nOutBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadSFZ(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);

        [DllImport("SSCard.dll", EntryPoint = "GetQRBase", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int GetQRBase(int nTimeout, StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);

        //电子凭证
        [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr NationEcTrans(StringBuilder strUrl, StringBuilder InData, StringBuilder OutData);

        private string URL;
        private string User;
        public CardReader()
        {

        }
        public CardReader(string URL, string User)
        {
            this.URL = URL;
            this.User = User;
        }

        public int Init()
        {
            string errMsg = "";
            int result = -1;
            try
            {
                StringBuilder sbURL = new StringBuilder(URL);
                StringBuilder sbUSER = new StringBuilder(User);

                result = Init(sbURL, sbUSER);
                return result;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return result;

            }
            finally
            {
                Global.writeLog("初始化", URL + ":" + User, result.ToString() + ":" + errMsg);
            }
        }

        public int ReadCardBas(out string basInfo, out string regInfo)
        {
            basInfo = ""; regInfo = "";
            int rtn = -1;
            try
            {
                StringBuilder sbBasInfo = new StringBuilder(1024);
                StringBuilder sbRegInfo = new StringBuilder(1024);

                rtn = ReadCardBas(sbBasInfo, 1024, sbRegInfo, 1024);
                basInfo = sbBasInfo.ToString();
                regInfo = sbRegInfo.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                basInfo = "ReadCardBas异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("读社保卡(" + URL + ")", regInfo, basInfo);
            }
        }

        public int VerifyPIN(out string outBuff)
        {
            outBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                rtn = VerifyPIN(sbOutBuff, 1024);
                outBuff = sbOutBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                outBuff = "VerifyPIN:" + ex.Message;
                return rtn;
            }
        }

        public int ChangePIN(out string outBuff)
        {
            outBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                rtn = ChangePIN(sbOutBuff, 1024);
                outBuff = sbOutBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                outBuff = "ChangePIN:" + ex.Message;
                return rtn;
            }
        }

        public int ReadSFZ(out string OutBuff, out string SignBuff)
        {
            OutBuff = ""; SignBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                StringBuilder sbSignBuff = new StringBuilder(1024);

                rtn = ReadSFZ(sbOutBuff, 1024, sbSignBuff, 1024);
                OutBuff = sbOutBuff.ToString();
                SignBuff = sbSignBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                OutBuff = "ReadSFZ 异常:" + ex.Message;
                return rtn;
            }
        }

        public int GetQRBase(int timeout, out string OutBuff, out string SignBuff)
        {
            OutBuff = ""; SignBuff = "";
            int rtn = -1;
            try
            {
                StringBuilder sbOutBuff = new StringBuilder(1024);
                StringBuilder sbSignBuff = new StringBuilder(1024);

                rtn = GetQRBase(timeout, sbOutBuff, 1024, sbSignBuff, 1024);
                OutBuff = sbOutBuff.ToString();
                SignBuff = sbSignBuff.ToString();
                return rtn;
            }
            catch (Exception ex)
            {
                OutBuff = "GetQRBase 异常:" + ex.Message;
                return rtn;
            }
        }

        public string NationEcTrans(string URL, string InData, out string OutData)
        {
            OutData = "";
            try
            {
                StringBuilder sbURL = new StringBuilder(URL);
                StringBuilder sbInData = new StringBuilder(InData);
                StringBuilder sbOutData = new StringBuilder(1024);

                IntPtr pRtn = NationEcTrans(sbURL, sbInData, sbOutData);
                OutData = sbOutData.ToString();
                string rtn = Marshal.PtrToStringAnsi(pRtn);
                return rtn;
            }
            catch (Exception ex)
            {
                OutData = "NationEcTrans 异常:" + ex.Message;
                return "-1";
            }
            finally
            {
                Global.writeLog("电子凭证(" + URL + ")", InData, OutData);
            }
        }

    }

    /// <summary>
    /// 黑龙江读卡动态库调用
    /// </summary>
    class CardReader_HLJ
    {
        string progID = "SCardDriverHLJ.SCard";
        System.Type ComType;
        object ComInstance;

        public CardReader_HLJ()
        {
            ComType = Type.GetTypeFromProgID(progID);
            // 创建Com的实例
            if (ComType != null)
            {

                //创建实例
                ComInstance = Activator.CreateInstance(ComType);

            }
        }

        public string readCardBas(int type, string hospNO)
        {
            if (ComType == null)
            {
                return "ComType为空！";
            }

            if (ComInstance == null)
            {
                return "ComInstance为空！";
            }
            try
            {
                //设置需要设置的参数值
                object[] ParamArray = new object[2];
                ParamArray[0] = type;
                ParamArray[1] = hospNO;
                ParameterModifier[] ParamMods = new ParameterModifier[1];
                ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
                ParamMods[0][0] = true;
                ParamMods[0][1] = true; // 设置第二个参数为返回参数，调用含有ParameterModifier数组的重载函数

                object o = ComType.InvokeMember("iReadCardBas", // 接口函数名
                 BindingFlags.Default | BindingFlags.InvokeMethod,
                null,
                ComInstance, // 调用的COM组件
                ParamArray, // 参数数组
                ParamMods, // 指定返回参数的ParameterModifier数组
                null,
                null);

                //string Msg = "加载成功：" + ParamArray[1].ToString();
                //Global.inf.writeLog(Msg + "___" + ParamArray[0].ToString());
                return o.ToString();
            }
            catch (Exception ex)
            {
                return "异常:" + ex.Message;
            }
        }
    }

    /// <summary>
    /// 河南读卡动态库调用
    /// </summary>
    class CardReader_HN
    {
        //电子凭证
        [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr NationEcTrans(StringBuilder strUrl, StringBuilder InData, StringBuilder OutData);
        //社保卡-本地
        [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadCardBas(int iType, StringBuilder pDevInfo, StringBuilder pOutInfo);
        //社保卡-异地1
        [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardBas_HSM_Step1", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadCardBas_HSM_Step1(int iType, StringBuilder pDevInfo, StringBuilder pOutInfo);
        //社保卡-异地2
        [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardBas_HSM_Step2", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadCardBas_HSM_Step2(StringBuilder pKey, StringBuilder pOutInfo);
        //社保卡-PIN校验
        [DllImport("SSCardDriver.dll", EntryPoint = "iVerifyPIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int VerifyPIN(int iType, StringBuilder pOutInfo);

        public int ReadCardBas(int iType, string sDevInfo, out string sOutInfo)
        {
            sOutInfo = ""; 
            int rtn = -1;
            try
            {
                StringBuilder bDevInfo = new StringBuilder(sDevInfo);                
                StringBuilder bOutInfo = new StringBuilder(1024);

                rtn = ReadCardBas(iType, bDevInfo, bOutInfo);
                sOutInfo = bOutInfo.ToString();
                
                return rtn;
            }
            catch (Exception ex)
            {
                sOutInfo = "ReadCardBas异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用社保卡ReadCardBas函数：",sDevInfo, sOutInfo);
            }
        }

        public int VerifyPIN(int iType, out string sOutInfo)
        {
            sOutInfo = "";
            int rtn = -1;
            try
            {
                StringBuilder bOutInfo = new StringBuilder(1024);

                rtn = VerifyPIN(iType, bOutInfo);
                sOutInfo = bOutInfo.ToString();

                return rtn;
            }
            catch (Exception ex)
            {
                sOutInfo = "iVerifyPIN异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用社保卡iVerifyPIN函数：", iType.ToString(), sOutInfo);
            }
        }

        public int ReadCardBas_HSM_Step1(int iType, string sDevInfo, out string sOutInfo)
        {
            sOutInfo = "";
            int rtn = -1;
            try
            {
                StringBuilder bDevInfo = new StringBuilder(sDevInfo);
                StringBuilder bOutInfo = new StringBuilder(1024);

                rtn = ReadCardBas_HSM_Step1(iType, bDevInfo, bOutInfo);
                sOutInfo = bOutInfo.ToString();

                return rtn;
            }
            catch (Exception ex)
            {
                sOutInfo = "ReadCardBas_HSM_Step1异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用社保卡ReadCardBas_HSM_Step1函数：", iType+"|"+ sDevInfo, sOutInfo);
            }
        }

        public int ReadCardBas_HSM_Step2(string pKey, out string sOutInfo)
        {
            sOutInfo = "";
            int rtn = -1;
            try
            {
                StringBuilder bPKey = new StringBuilder(pKey);
                StringBuilder bOutInfo = new StringBuilder(1024);

                rtn = ReadCardBas_HSM_Step2(bPKey, bOutInfo);
                sOutInfo = bOutInfo.ToString();

                return rtn;
            }
            catch (Exception ex)
            {
                sOutInfo = "ReadCardBas_HSM_Step2异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用社保卡ReadCardBas_HSM_Step2函数：", pKey, sOutInfo);
            }
        }

        public string NationEcTrans(string URL, string InData, out string OutData)
        {
            OutData = "";            
            try
            {
                StringBuilder sbURL = new StringBuilder(URL);
                StringBuilder sbInData = new StringBuilder(InData);
                StringBuilder sbOutData = new StringBuilder(1024);

                IntPtr pRtn = NationEcTrans(sbURL, sbInData, sbOutData);
                OutData = sbOutData.ToString();

                //MessageBox.Show("电子凭证NationEcTrans返回的：" + pRtn);

                string rtn = Marshal.PtrToStringAnsi(pRtn);
                return rtn;
            }
            catch (Exception ex)
            {
                OutData = "NationEcTrans 异常:" + ex.Message;
                return "";
            }
            finally
            {
                Global.writeLog("电子凭证(" + URL + ")", InData, OutData);
            }
        }
    }

    /// <summary>
    /// 陕西读卡动态库调用
    /// </summary>
    class CardReader_SX
    {
        //电子凭证
        [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr NationEcTrans(StringBuilder strUrl, StringBuilder InData, StringBuilder OutData);

        //电子凭证
        [DllImport("readcard.dll", EntryPoint = "Certificate", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr Certificate(StringBuilder pname, StringBuilder paae011, StringBuilder porgId, StringBuilder paab034, StringBuilder pRetstr, StringBuilder perrinfo);

        //社保卡
        [DllImport("readcard.dll", EntryPoint = "ReadSSECard_s", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadSSECard_s(StringBuilder pRetstr, StringBuilder perrinfo);

        //结算校验社保卡
        [DllImport("readcard.dll", EntryPoint = "ReadydSSECard", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadydSSECard(StringBuilder pxfmx, StringBuilder paab034, StringBuilder pbaa001, StringBuilder pRetstr, StringBuilder perrinfo);

        //结算校验身份证
        [DllImport("readcard.dll", EntryPoint = "readIDcard", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int ReadIDcard(StringBuilder pidcard, StringBuilder pName, StringBuilder pRetstr, StringBuilder perrinfo);


        public int ReadCard(out string sRetstr, out string sErrinfo)
        {
            sRetstr = "";
            sErrinfo = "";
            int rtn = -1;
            try
            {
                StringBuilder pRetstr = new StringBuilder(1024);
                StringBuilder perrinfo = new StringBuilder(1024);

                rtn = ReadSSECard_s(pRetstr, perrinfo);
                sRetstr = pRetstr.ToString();
                sErrinfo = perrinfo.ToString();

                return rtn;
            }
            catch (Exception ex)
            {
                sErrinfo = "调用社保卡ReadSSECard_s函数异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用社保卡ReadSSECard_s函数：", "", sRetstr);
            }
        }

        public int ReaddCard_JS(string sXFMX, out string sRetstr, out string sErrinfo)
        {
            sRetstr = "";
            sErrinfo = "";
            int rtn = -1;
            try
            {
                StringBuilder xfmx = new StringBuilder(sXFMX);
                StringBuilder aab034 = new StringBuilder(Global.pat.mdtrtarea_admvs);
                StringBuilder baa001 = new StringBuilder(Global.pat.insuplc_admdvs);
                StringBuilder pRetstr = new StringBuilder(1024);
                StringBuilder perrinfo = new StringBuilder(1024);

                rtn = ReadydSSECard(xfmx, aab034, baa001, pRetstr, perrinfo);
                sRetstr = pRetstr.ToString();
                sErrinfo = perrinfo.ToString();

                return rtn;
            }
            catch (Exception ex)
            {
                sErrinfo = "调用社保卡ReadydSSECard函数异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用社保卡ReadydSSECard函数：", sXFMX, sRetstr);
            }
        }

        public int ReadIDcard_JS(string sSFZH,string sXM, out string sRetstr, out string sErrinfo)
        {
            sRetstr = "";
            sErrinfo = "";
            int rtn = -1;
            try
            {                
                StringBuilder xSFZH = new StringBuilder(sSFZH);
                StringBuilder xXM = new StringBuilder(sXM);
                StringBuilder pRetstr = new StringBuilder(1024);
                StringBuilder perrinfo = new StringBuilder(1024);

                rtn = ReadIDcard(xSFZH, xXM, pRetstr, perrinfo);
                sRetstr = pRetstr.ToString();
                sErrinfo = perrinfo.ToString();

                return rtn;
            }
            catch (Exception ex)
            {
                sErrinfo = "调用身份证ReadIDCard函数异常:" + ex.Message;
                return rtn;
            }
            finally
            {
                Global.writeLog("调用身份证ReadIDCard函数：", sXM + sSFZH, sRetstr);
            }
        }

        public string NationEcTrans(string URL, string InData, out string OutData)
        {
            OutData = "";
            try
            {
                StringBuilder sbURL = new StringBuilder(URL);
                StringBuilder sbInData = new StringBuilder(InData);
                StringBuilder sbOutData = new StringBuilder(1024);

                IntPtr pRtn = NationEcTrans(sbURL, sbInData, sbOutData);
                OutData = sbOutData.ToString();

                //MessageBox.Show("电子凭证NationEcTrans返回的：" + pRtn);

                string rtn = Marshal.PtrToStringAnsi(pRtn);
                return rtn;
            }
            catch (Exception ex)
            {
                OutData = "NationEcTrans 异常:" + ex.Message;
                return "";
            }
            finally
            {
                Global.writeLog("电子凭证(" + URL + ")", InData, OutData);
            }
        }

        public int CertDecode(string name, string aae011, string orgId, string aab034, out string Retstr, out string errinfo)        
        {
            Retstr = "";
            errinfo = "";

            try
            {
                StringBuilder sName = new StringBuilder(name);
                StringBuilder sAae011 = new StringBuilder(aae011);
                StringBuilder sOrgId = new StringBuilder(orgId);
                StringBuilder sAab034 = new StringBuilder(aab034);

                StringBuilder sRetstr = new StringBuilder(1024);
                StringBuilder sErrinfo = new StringBuilder(1024);

                //MessageBox.Show(sName.ToString() + sAae011.ToString() + sOrgId.ToString() + sAab034.ToString());

                IntPtr pRtn = Certificate(sName, sAae011, sOrgId, sAab034, sRetstr, sErrinfo);
                Retstr = sRetstr.ToString();
                errinfo = sErrinfo.ToString();

                //MessageBox.Show("电子凭证Certificate返回的：" + pRtn);

                if ((pRtn.ToString() == "0") && (errinfo == "") && (Retstr != ""))                
                    return 0;
                else
                    return -1;

                //string rtn = Marshal.PtrToStringAnsi(pRtn);
                //return int.Parse(rtn);
            }
            catch (Exception ex)
            {
                Retstr = "Certificate 异常:" + ex.Message;
                return -1;
            }
            finally
            {
                Global.writeLog("电子凭证Certificate", name + "|" + aae011 + "|" + orgId + "|" + aab034, "sCardInfo:" + Retstr + "|" + "Error:" + errinfo);
            }
        }

    }
}
