using System;
using DirectService.Tools;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Helper;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

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

            if (request.postMessage == null)
            {
                rslt.ErrMsg = "未提交公告栏信息";
                return;
            }

            if (string.IsNullOrEmpty(request.postMessage.dscrp))
            {
                rslt.ErrMsg = "公告内容不能为空,请检查!";
                return;
            }

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

                var baseHelper = HelperBase.GetHelper<BasicInfoHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var _message = ObjectHelper.DeepCopy(request.postMessage);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        baseHelper.SaveSysPostMessage(_message);
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        rslt.ErrMsg = e.Message;
                        cmd.Transaction?.Rollback();
                    }
                }
            }
        }
    }
}
