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

namespace PTMedicalInsurance.Business
{
    /// <summary>
    /// 住院费用明细上传
    /// </summary>
    class InpatientFeeUploadProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            string errMsg = "";
            //从医保平台获取患者详细的医保登记信息
            if (mIS.queryRegisterInfo(1, out outParam) != 0)
            {
                return Exception("查询患者云平台登记信息", outParam);
            }
            JObject joReg = JObject.Parse(outParam);
            Global.pat.medType = JsonHelper.getDestValue(joReg, "data.MedicalType");
            Global.pat.insuType = JsonHelper.getDestValue(joReg, "data.InsuType");
            //先取消中心费用传送
            cBus.cancleFeeUpload(TradeEnum.InpatientFeeCancel, out errMsg);
            //再删除医保传送表的数据
            mIS.deleteFee(out errMsg);

            #region【住院费用上传前调用3101事前服务】
            ////事前分析
            if (Global.curEvt.ext.isOpenAnalysis)
            {
                if (hBus.PreAnalysis("4", "", out errMsg) != 0)
                {
                    return Exception("事前分析", errMsg);
                }
            }
            #endregion

            //开始进行费用传送
            //调用HIS费用查询信息
            if (hIS.getHisFee(Global.pat, out outParam) != 0)
            {
                return Exception("获取HIS费用", outParam);
            }
            //调用医保平台转换
            JObject joHisFee = JObject.Parse(outParam);
            if (mIS.convertHisFeeWithInsuCode(joHisFee, out outParam) != 0)
            {
                return Exception("转换HIS费用", outParam);
            }

            JArray jaFeeDetail = JArray.Parse(JsonHelper.getDestValue(JObject.Parse(outParam), "data"));
            //按指定条数分割后上传，保存，更新
            if (hBus.uploadFeeToCenter(TradeEnum.InpatientFeeUpload, 50, jaFeeDetail, out outParam) != 0)
            {
                return Exception("上传费用", outParam);
            }
            else
            {
                return IrisReturn("住院费用上传成功", null);
            }
        }
    }
}
