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.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.Tools.Encry;

namespace JLHHJSvr.Excutor
{
    internal sealed class PostFileExcutor : ExcutorBase<PostFileRequest, PostFileResponse>
    {
        protected override void ExcuteInternal(PostFileRequest request, object state, PostFileResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

            if (request.filedata == null || request.filedata.Length == 0)
            {
                rslt.ErrMsg = "获取文件数据失败";
                return;
            }

            if (string.IsNullOrEmpty(request.fileType))
            {
                rslt.ErrMsg = "分析文件类型失败";
                return;
            }

            var md5Helper = new MD5();
            var md5 = md5Helper.GetMD5(request.filedata);
            using (var con = new SqlConnection(GlobalVar.ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var filemap = new st_file {filemd5 = md5};
                if (DbSqlHelper.SelectOne(cmd, filemap, "md5") != 1)
                {
                    filemap.filedata = request.filedata;
                    filemap.fileType = request.fileType;
                    DbSqlHelper.Insert(cmd, filemap);
                }
            }
            rslt.md5 = md5;
        }
    }
}
