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

namespace JLHHJSvr.Excutor
{
    internal sealed class UnLockTableExcutor : ExcutorBase<UnLockTableRequest, UnLockTableResponse>
    {
        protected override void ExcuteInternal(UnLockTableRequest request, object state, UnLockTableResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var user = new u_user_jlhprice() { empid = tokendata.empid };
                DbSqlHelper.SelectOne(cmd, user, "usermode");

                byte is_admin = 0;
                if (tokendata.empid == 0) is_admin = 1;
                else if (user.usermode == 0 || user.usermode == 2) is_admin = 1; // 管理员或者经理

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        LockHelper.UnLockBill(cmd, request.keyword, request.billid, tokendata.username, is_admin);
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
