using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.DBA.DBModle;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class SetOptionExcutor : ExcutorBase<SetOptionRequest, SetOptionResponse>
    {
        protected override void ExcuteInternal(SetOptionRequest request, object state, SetOptionResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.optionInfo.optionid == null || request.optionInfo.optionid == 0)
            {
                rslt.ErrMsg = "信息缺失：ID";
                return;
            }
            if (request.optionInfo.optionvalue == null)
            {
                rslt.ErrMsg = "信息缺失：值";
                return;
            }

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

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var stOption = new st_option();
                        stOption.SetValue(cmd, request.optionInfo.optionid.Value, request.optionInfo.optionvalue,
                            tokendata.username);
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
