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;

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

            if (request.spring == null)
            {
                rslt.ErrMsg = "未提交弹簧资料";
                return;
            }

            if(request.spring.line_diameter == null || request.spring.line_diameter == 0)
            {
                rslt.ErrMsg = "请录入线径";
                return;
            }

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

                var dtNow = DateTime.Now;

                AutoInit.AutoInitS(cmd, request.spring);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if (request.spring.springid <= 0)
                        {
                            request.spring.springid = BllHelper.GetID(cmd, "u_spring");

                            request.spring.his_typeid = 1;
                            request.spring.createtime = dtNow;
                            request.spring.createby = tokendata.username;

                            var fields = @"springid,line_diameter,height,caliber,center_diameter,cyclenum,gram_weight,dscrp,createtime,createby,tempqty,bedtypeid,his_typeid,springtypeid,flag,free_height,actual_height,hardness,hardness_indentation_1,hardness_indentation_2,hardness_indentation_4,arrangement_width,arrangement_height,roll_width,roll_length,comperssion_rate";
                            DbSqlHelper.Insert(cmd, "u_spring", null, request.spring, fields);
                        }
                        else
                        {
                            //修改
                            request.spring.moddate = dtNow;
                            request.spring.modemp = tokendata.username;

                            var fields = @"line_diameter,height,caliber,center_diameter,cyclenum,gram_weight,dscrp,tempqty,bedtypeid,his_typeid,springtypeid,free_height,actual_height,hardness,hardness_indentation_1,hardness_indentation_2,hardness_indentation_4,arrangement_width,arrangement_height,roll_width,roll_length,comperssion_rate,modemp,moddate";
                            DbSqlHelper.Update(cmd, "u_spring", null, request.spring, "springid", fields);
                        }

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