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

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveMattressExtraTypeExcutor : ExcutorBase<SaveMattressExtraTypeRequest, SaveMattressExtraTypeResponse>
    {
        protected override void ExcuteInternal(SaveMattressExtraTypeRequest request, object state, SaveMattressExtraTypeResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (request.data == null)
            {
                rslt.ErrMsg = "未提交数据定义";
                return;
            }

            if (string.IsNullOrEmpty(request.data.typename))
            {
                rslt.ErrMsg = "请填写类型名称";
                return;
            }

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

                var dtNow = DateTime.Now;

                AutoInit.AutoInitS(cmd, request.data);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var fields = "typename,typeid,inuse,createby,createtime,inputtype";
                        request.data.createby = tokendata.username;
                        request.data.createtime = DateTime.Now;
                        if (request.data.extraid <= 0)
                        {
                            request.data.extraid = BllHelper.GetID(cmd, "u_mattress_extra_type");

                            DbSqlHelper.Insert(cmd, "u_mattress_extra_type", null, request.data, "extraid," + fields);
                        }
                        else
                        {
                            //修改
                            DbSqlHelper.Update(cmd, "u_mattress_extra_type", null, request.data, "extraid", fields);
                        }

                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
