using System;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.Helper;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveSoftBedQuoteExcutor : ExcutorBase<SaveSoftBedQuoteRequest, SaveSoftBedQuoteResponse>
    {
        protected override void ExcuteInternal(SaveSoftBedQuoteRequest request, object state, SaveSoftBedQuoteResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var softBedHelper = HelperBase.GetHelper<SoftBedHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if(!string.IsNullOrWhiteSpace(request.type) && request.type == "copy")
                        {
                            var result = softBedHelper.CopySoftBed(request.softbed);
                            rslt.softbed = new u_softbed()
                            {
                                softbed_id = result.softbed_id,
                                softbed_code = result.softbed_code,
                                softbed_name = result.softbed_name,
                                softbed_relcode = result.softbed_relcode
                            };
                        } else { 
                            softBedHelper.SaveSoftBed(request.softbed);
                            rslt.softbed = new u_softbed()
                            {
                                softbed_id = request.softbed.softbed_id,
                                softbed_code = request.softbed.softbed_code,
                                softbed_name = request.softbed.softbed_name,
                                softbed_relcode = request.softbed.softbed_relcode
                            };
                        }

                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
