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

namespace PTMedicalInsurance.Business
{
    abstract class AbstractProcess
    {
        protected CenterBusiness cBus = new CenterBusiness();
        protected HisMainBusiness hBus = new HisMainBusiness();
        protected HisIrisServices hIS = new HisIrisServices();
        protected MIIrisServices mIS = new MIIrisServices();
        protected InvokeHelper invoker = new InvokeHelper();

        protected string outParam = "";
        protected string errMsg = "";

        public JObject OrginalInput { set; get; }

        /// <summary>
        /// 使用原始参数
        /// </summary>
        public bool UseOrginal
        {
            get; set;
        }

        /// <summary>
        /// 合并原始参数
        /// </summary>
        public bool MergeOrginal
        {
            get; set;
        }

        /// <summary>
        /// 返回成功信息
        /// </summary>
        /// <returns></returns>
        protected CallResult Success()
        {
            return Success("成功");
        }
        protected CallResult Success(string msg)
        {
            Global.writeLog(this.GetType().Name, msg, outParam);
            return new CallResult(0, msg, outParam);
        }

        /// <summary>
        /// 返回失败信息
        /// </summary>
        /// <param name="code"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        protected CallResult Error(int code, string errMsg)
        {
            return Exception(code, errMsg, outParam);
        }

        protected CallResult Exception(int code, string errMsg, string param)
        {
            outParam = JsonHelper.setExceptionJson(code, errMsg, param).ToString();
            Global.writeLog(this.GetType().Name, errMsg, outParam);
            return new CallResult(code, errMsg, outParam, param);
        }
        protected CallResult Exception(string errMsg, string param)
        {
            return Exception(-1, errMsg, param);
        }

        protected CallResult Exception(string errMsg)
        {
            return Exception(-1, errMsg, outParam);
        }

        protected CallResult Exception()
        {
            return Exception(-1, "", outParam);
        }
        protected CallResult IrisReturn(string msg, JObject obj)
        {
            outParam = JsonHelper.setIrisReturnValue(0, msg, obj).ToString();
            return new CallResult(0, msg, outParam, obj);
        }

        /// <summary>
        /// 返回失败信息
        /// </summary>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        protected CallResult Error(string errMsg)
        {
            return new CallResult(-1, errMsg, outParam);
        }

        /// <summary>
        /// 业务过程
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public abstract CallResult Process(JObject input);
    }
}
