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

namespace PTMedicalInsurance.Business
{
    class ProcessManager
    {
        private List<AbstractProcess> Processes { get; set; }

        public ProcessManager()
        {
            Processes = new List<AbstractProcess>();
        }

        public ProcessManager(List<AbstractProcess> processes)
        {
            Processes = processes;
        }

        /// <summary>
        /// 增加单个处理流程
        /// </summary>
        /// <param name="process"></param>
        public ProcessManager Add(AbstractProcess process)
        {
            Processes.Add(process);
            return this;
        }

        /// <summary>
        /// 运行
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string Run(JObject input)
        {
            string data = "";
            // 按顺序执行业务
            foreach (var process in Processes)
            {
                var ret = process.Process(input);
                data = ret.Data;
                if (ret.Code < 0)
                {
                    //异常
                    data = ret.Data;
                    break;
                }
                if (!string.IsNullOrEmpty(data))
                {
                    input = JObject.Parse(data);
                }
                
            }
            return data;
        }

    }
}
