using System;
using DirectService.Tools;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Helper;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveSoftBedFormulaExcutor : ExcutorBase<SaveSoftBedFormulaRequest, SaveSoftBedFormulaResponse>
    {
        protected override void ExcuteInternal(SaveSoftBedFormulaRequest request, object state, SaveSoftBedFormulaResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (request.formula == null)
            {
                rslt.ErrMsg = "未提交软床公式定义信息";
                return;
            }

            if (string.IsNullOrEmpty(request.formula.formulaname))
            {
                rslt.ErrMsg = "公式名称不能为空,请检查!";
                return;
            }

            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var baseHelper = HelperBase.GetHelper<BasicInfoHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var _formula = ObjectHelper.DeepCopy(request.formula);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        baseHelper.SaveSoftBedFormula(_formula);
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        rslt.ErrMsg = e.Message;
                        cmd.Transaction?.Rollback();
                    }
                }
            }
        }
    }
}
