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

namespace JLHHJSvr.Excutor
{
    internal sealed class GetFileByMd5Excutor : ExcutorBase<GetFileByMd5Request, GetFileByMd5Response>
    {
        protected override void ExcuteInternal(GetFileByMd5Request request, object state, GetFileByMd5Response rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (string.IsNullOrEmpty(request.md5))
            {
                rslt.ErrMsg = "所请求的图片无效";
                return;
            }

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

                cmd.CommandText = "SELECT filedata, fileType    FROM st_file    WHERE filemd5 = @md5";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@md5", request.md5);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        rslt.fileData = (byte[])reader["filedata"];
                        rslt.fileType = Convert.ToString(reader["fileType"]);
                    }
                    else
                    {
                        rslt.ErrMsg = "文件不存在";
                        return;
                    }
                }
            }
        }
    }
}
