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.Core.SelfServiceMachine.Process.InPat
{
    class UploadFee:AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            cBus.cancleRegister("2405", out errMsg);
            //从医保平台获取患者详细的医保登记信息
            if (mIS.queryRegisterInfo(1, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "查询患者云平台登记信息", outParam).ToString();
                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("2302", out errMsg);
            //再删除医保传送表的数据
            mIS.deleteFee(out errMsg);

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

            JArray jaFeeDetail = JArray.Parse(JsonHelper.getDestValue(JObject.Parse(outParam), "data"));
            //按指定条数分割后上传，保存，更新
            if (hBus.uploadFeeToCenter("2301", 10, jaFeeDetail, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "上传费用", outParam).ToString();
                return Exception(outParam);
            }
            else
            {
                outParam = JsonHelper.setIrisReturnValue(0, "住院费用上传成功", null).ToString();
                outParam = input.ToString();
                return Success();
            }
        }
    }
        
}
