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

namespace JLHHJSvr.Excutor
{
    internal sealed class SetSysUserFileStringExcutor : ExcutorBase<SetSysUserFileStringRequest, SetSysUserFileStringResponse>
    {
        protected override void ExcuteInternal(SetSysUserFileStringRequest request, object state, SetSysUserFileStringResponse rslt)
        {
            if (request.empid == null)
            {
                rslt.ErrMsg = "参数错误,empid不能为null";
                return;
            }

            if (request.dwname == null)
            {
                rslt.ErrMsg = "参数错误,dwname不能为null";
                return;
            }

            if (request.itemname == null)
            {
                rslt.ErrMsg = "参数错误,itemname不能为null";
                return;
            }

            if (request.itemvalue == null)
            {
                rslt.ErrMsg = "参数错误,itemvalue不能为null";
                return;
            }


            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断";
                return;
            }
            //if (string.IsNullOrEmpty(tokendata.ConStr))
            //{
            //    rslt.ErrMsg = "当前账套未设置数据库";
            //    return;
            //}

            using (var con = new SqlConnection(GlobalVar.ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                con.Open();
                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        //var ufs = HelperBase.GetHelper<SysUserFileString>(cmd);

                        var ifok = BllHelper.SetValue(cmd, request.empid.Value, request.dwname, request.itemname, request.itemvalue, request.ifcompress == 1 ? true : false);
                        if (!ifok)
                        {
                            rslt.ErrMsg = "自定义值保存失败,可能数据库字段长度不足";
                        }
                        //if (request.empid == -1)
                        //{
                        //    BllHelper.delOtherLayout(cmd, request.dwname, request.itemname);
                        //}
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction.Rollback();
                        rslt.ErrMsg = e.ToString();
                    }
                }

            }
        }
    }
}
