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.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.DBA.DBModle;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetSysUserFileStringExcutor : ExcutorBase<GetSysUserFileStringRequest, GetSysUserFileStringResponse>
    {
        protected override void ExcuteInternal(GetSysUserFileStringRequest request, object state, GetSysUserFileStringResponse 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;
            }

            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();

                //var ufs = HelperBase.GetHelper<SysUserFileString>(cmd);

                rslt.itemvalue = BllHelper.GetValue(cmd, request.empid.Value, request.dwname, request.itemname, string.Empty, request.ifcompress == 1 ? true : false);
                if (string.IsNullOrEmpty(rslt.itemvalue))//如果没有自己的布局方案，尝试获取系统的布局方案
                {
                    rslt.itemvalue = BllHelper.GetValue(cmd, -1, request.dwname, request.itemname, string.Empty, request.ifcompress == 1 ? true : false);
                }
                if (request.ifdel != null && request.ifdel == 1)
                {
                    BllHelper.delLayout(cmd, tokendata.userid, request.dwname, request.itemname);
                }

            }
        }

    }
}
