using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.Helper;
using JLHHJSvr.LJException;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class LockTableExcutor : ExcutorBase<LockTableRequest, LockTableResponse>
    {
        protected override void ExcuteInternal(LockTableRequest request, object state, LockTableResponse rslt)
        {
            if (string.IsNullOrEmpty(request.keyword)) throw new LJCommonException("加锁单据关键字错误,请检查!");

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

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        LockHelper.LockBill(cmd, request.keyword, request.billid, request.billcode, BllHelper.GetToken(request.token).username);
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
