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

/**
 * 
 * 
 * 310100 职工基本医疗保险统筹基金 501000 一至六级残疾军人医疗补助基金
 * 310200 职工基本医疗保险个人账户基金 370100 企业补充医疗保险基金
 * 320100 公务员医疗补助基金 390100 城乡居民基本医疗保险基金
 * 330100 职工大额医疗费用补助基金 610100 医疗救助基金
 * 340100 离休人员医疗保障基金 999999 其他基金
 * 391100 城乡居民大病医疗保险资
*/

namespace PTMedicalInsurance.Business
{
    class LocalPayFundSplitService
    {
        /// <summary>
        /// 根据返回的金额进行拆分基金分类
        /// </summary>
        /// <param name="joSettle">结算返回值</param>
        /// <returns></returns>
        public JArray Split(JObject joSettle)
        {
            JArray joRtn = new JArray();
            try
            {
                Dictionary<string, string> medTypeDict = new Dictionary<string, string>()
            {
                { "A","310100"},  //职工
                { "B","390100"}     //居民
            };
                Dictionary<string, Dictionary<string, string>> fundDict = new Dictionary<string, Dictionary<string, string>>()
                {
                    ["hifp_pay"] = new Dictionary<string, string>() {
                     { "310100", "职工基本医疗保险统筹基金" },
                     { "390100","城乡居民基本医疗保险基金"} },
                    ["acct_pay"] = CreateKeyPair("310200", "职工基本医疗保险个人账户基金"),
                    ["hifob_pay"] = CreateKeyPair("330100", "职工大额医疗费用补助基金"),
                    ["hifes_pay"] = CreateKeyPair("370100", "企业补充医疗保险基金"),
                    ["hifmi_pay"] = CreateKeyPair("391100", "城乡居民大病医疗保险资"),
                    ["maf_pay"] = CreateKeyPair("610100", "医疗救助基金"),
                    ["cvlserv_pay"] = CreateKeyPair("320100", "公务员医疗补助基金"),
                };

                string inscopeAmt = joSettle["inscp_scp_amt"].Text();
                string medType = Global.pat.medType ?? "A"; //默认职工
                string medFundCode = medTypeDict.ToList().FirstOrDefault(s => s.Key == medType).Value;
                foreach (var k in fundDict.Keys)
                {
                    if (!string.IsNullOrEmpty(joSettle[k].Text()))
                    {
                        dynamic fund = new JObject();
                        fund.fund_payamt = decimal.Parse(joSettle[k].Text());
                        if (fund.fund_payamt <= 0.0D) continue;
                        fund.inscp_scp_amt = inscopeAmt;
                        if (fundDict[k].Count > 1)
                        {

                            fund.fund_pay_type = medFundCode;
                            fund.fund_pay_type_name = fundDict[k].ToList().FirstOrDefault(s => s.Key == medFundCode).Value;
                        }
                        else
                        {
                            fund.fund_pay_type = fundDict[k].First().Key;
                            fund.fund_pay_type_name = fundDict[k].First().Value;
                        }

                        joRtn.Add(fund);
                    }
                }
            }
            catch (Exception e)
            {
                Global.writeLog("基金分类转换错误:"+e.Message);
            }
            return joRtn;

        }

        private Dictionary<string, string> CreateKeyPair(string key, string value)
        {
            return new Dictionary<string, string>() { { key, value } };
        }

    }
       
}
