/******************************************************************************
 * 文件名称: CenterBusiness.cs
 * 文件说明: 中心交易封装
 * 当前版本: V1.0
 * 创建日期: 2022-06-20
 *
 * * 2020-06-20: 增加 CenterBusiness 类
 * *****  2020-06-20: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-20: 增加 CardReader 方法重载，国家版电子凭证使用
 * *****  2020-06-20: 增加 Init 方法，SSCard环境初始化



******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Common;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;

namespace PTMedicalInsurance.Business
{
    class CenterBusiness
    {
        private InvokeHelper invoker = new InvokeHelper();
        /// <summary>
        /// 签到
        /// </summary>
        /// <returns></returns>
        public int signIn(out JObject joRtn)
        {
            string outParam = "";
            string inParam = "";
            try
            {
                dynamic jsonSignIn = new JObject();
                dynamic jsonInput = new JObject();
                jsonSignIn.opter_no = Global.curEvt.signno;
                jsonSignIn.mac = Global.curEvt.mac;
                jsonSignIn.ip = Global.curEvt.ip;
                jsonInput.signin = jsonSignIn;

                inParam = JsonHelper.setCenterInpar("9001", jsonInput.ToString());
                
                outParam = invoker.invokeCenterService("9001", inParam).ToString();
                joRtn = JObject.Parse(outParam);
                if (joRtn["infcode"].ToString() == "0")
                {
                    Global.curEvt.signno = joRtn["output"]["signinoutb"]["sign_no"].ToString();
                    return 0;
                }
                else
                {
                    //joRtn = JsonHelper.getIrisExceptionJson(-100, "签到异常", (string)joRtn["err_msg"]);
                    return -1;
                }

            }
            catch (Exception ex)
            {
                joRtn = new JObject();
                joRtn.Add("infcode", -1);
                joRtn.Add("err_msg", outParam + ex.Message);
                //joRtn = JsonHelper.getIrisExceptionJson(-100,"签到异常", outParam + ex.Message);
                return -2;
            }

        }

        /// <summary>
        /// 住院患者登记
        /// </summary>
        /// <returns></returns>
        public int inPatReg(string inParam,out string outparam)
        {
            outparam = "";
            try
            {

                return 0;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }


        public int cancleRegister(string funNO, out string outparam)
        {
            outparam = "";
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.psn_no = Global.pat.psn_no;
                if (funNO == "2202")
                {
                    joTmp.ipt_otp_no = Global.pat.adm_Dr.ToString();
                }
                JObject joData = new JObject();
                joData.Add("data", joTmp);
                //好后上传给中心
                //string inpar = GlobalVariables.SetJsonParam("2405", joTmp).ToString();

                JObject joRtn = invoker.invokeCenterService(funNO, JsonHelper.setCenterInpar(funNO, joData).ToString());
                if (JsonHelper.parseCenterRtnValue(joRtn, out errMsg) != 0)
                {
                    outparam = errMsg;
                    return -1;
                }
                else
                {
                    outparam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outparam = "取消登记异常" + funNO + ":" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 取消费用传送
        /// </summary>
        /// <param name="pat"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int cancleFeeUpload(string funNO,out string errMsg)
        {
            errMsg = "";
            try
            {
                //组织入参
                dynamic joTmp = new JObject();
                if (funNO == "2205")
                {
                    joTmp.chrg_bchno = "0000";
                }
                else
                {
                    joTmp.feedetl_sn = "0000";
                }
                
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.psn_no = Global.pat.psn_no;
                joTmp.expContent = "";
                JObject joData = new JObject();
                if (funNO == "2205")
                {
                    joData.Add("data", joTmp);
                }
                else
                {
                    JArray jaData = new JArray();
                    jaData.Add(joTmp);
                    joData.Add("data", jaData);
                }
                    

                //组织好后上传给中心
                JObject joRtn = invoker.invokeCenterService(funNO, JsonHelper.setCenterInpar(funNO, joData.ToString()));
                if (JsonHelper.parseCenterRtnValue(joRtn, out errMsg) != 0)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                errMsg = "cancleFeeUpload异常：" + ex.Message;
                return -1;
            }
            
            
            

        }

        public int cancleSettlement(string funNO, out string outparam)
        {
            outparam = "";
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.psn_no = Global.pat.psn_no;
                joTmp.setl_id = Global.pat.settlID;
                joTmp.expContent = "";
                JObject joData = new JObject();
                joData.Add("data", joTmp);
                JObject joRtn = invoker.invokeCenterService(funNO, JsonHelper.setCenterInpar(funNO, joData).ToString());
                if (JsonHelper.parseCenterRtnValue(joRtn, out errMsg) != 0)
                {
                    outparam = errMsg;
                    return -1;
                }
                else
                {
                    outparam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outparam = "取消结算异常" + funNO + ":" + ex.Message;
                return -1;
            }
        }
    }
}
