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 SaveMattressExtraExcutor : ExcutorBase<SaveMattressExtraRequest, SaveMattressExtraResponse>
    {
        protected override void ExcuteInternal(SaveMattressExtraRequest request, object state, SaveMattressExtraResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (request.data == null)
            {
                rslt.ErrMsg = "未提交数据定义";
                return;
            }

            if (string.IsNullOrEmpty(request.data.extramxname))
            {
                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 = "extraid,extramxname,price,qty,dscrp,inuse,ifinit,inputtype,createby,createtime";
                        request.data.createby = tokendata.username;
                        request.data.createtime = DateTime.Now;
                        if (request.data.extramxid <= 0)
                        {
                            request.data.extramxid = BllHelper.GetID(cmd, "u_mattress_extra");

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

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