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

namespace JLHHJSvr.Excutor
{
    internal sealed class SetDwLayoutExcutor : ExcutorBase<SetDwLayoutRequest, SetDwLayoutResponse>
    {
        protected override void ExcuteInternal(SetDwLayoutRequest request, object state, SetDwLayoutResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已中断";
                return;
            }

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

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var layout = new fx_user_dwlayout()
                        {
                            userid = tokendata.userid,
                            dwname = request.layout.dwname,
                            column_visible = request.layout.column_visible,
                            column_width = request.layout.column_width,
                            column_x = request.layout.column_x,
                            column_autosize = request.layout.column_autosize,
                            column_detail_height = request.layout.column_detail_height
                        };
                        if (DbSqlHelper.InsertOrUpdate(cmd, layout, "*") != 1)
                        {
                            cmd.Transaction.Rollback();
                            return;
                        }

                        cmd.Transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        cmd.Transaction.Rollback();
                        Trace.Write(ex.ToString());
                        return;
                    }
                }
            }
        }
    }
}
