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

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

            if (request.bednet == null)
            {
                rslt.ErrMsg = "未提交床网类别定义信息";
                return;
            }

            if (string.IsNullOrEmpty(request.bednet.typename))
            {
                rslt.ErrMsg = "床网类别名称不能为空,请检查!";
                return;
            }

            if (string.IsNullOrEmpty(request.bednet.typecode))
            {
                rslt.ErrMsg = "床网类别编号不能为空,请检查!";
                return;
            }

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

                var dtNow = DateTime.Now;

                AutoInit.AutoInitS(cmd, request.bednet);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if (request.bednet.bednettypeid <= 0)
                        {
                            request.bednet.bednettypeid = BllHelper.GetID(cmd, "u_bednet_type");
                            request.bednet.createtime = dtNow;
                            request.bednet.createby = tokendata.username;

                            var fields = @"bednettypeid,typecode,typename,if_double_net,if_double_spring,if_15cm_strip,if_part,spring_mtrl_formula,spring_hr_formula,
                            fabrics1_mtrl_formula,fabrics2_mtrl_formula,if_snake_wire,snake_wire_diameter,snake_wire_mtrl_formula,if_hard_around,hard_around_mtrl_formula,
                            hard_around_hr_formula,if_pocket_around,pocket_around_spring_formula,pocket_around_fabrics_formula,if_side_iron,side_iron_mtrl_formula,
                            side_iron_hr_formula,glue_mtrl_formula,if_rsorwa,rsorwa_mtrl_formula,rsorwa_hr_formula,if_sponge_drilling,sponge_drilling_hr_formula,
                            fork_mtrl_formula,fork_hr_formula,cnail_mtrl_formula,cnail_hr_formula,sponge_mtrl_formula,sponge_hr_formula,edge_mtrl_formula,
                            edge_hr_formula,bz_mtrl_formula,felt_mtrl_formula,felt_hr_formula,packet_mtrl_formula,packet_hr_formula,sponge_mtrl_tc_formula,
                            createtime,createby,spring_weight_formula,side_iron_weight_formula,hard_around_weight_formula,fabrics1_weight_formula,fabrics2_weight_formula,
                            felt_weight_formula,cnail_weight_formula,sponge_weight_formula,sponge_weight_tc_formula,edge_weight_formula,fork_weight_formula";
                            DbSqlHelper.Insert(cmd, "u_bednet_type", null, request.bednet, fields);
                        }
                        else
                        {
                            //修改
                            var fields = @"typecode,typename,if_double_net,if_double_spring,if_15cm_strip,if_part,spring_mtrl_formula,spring_hr_formula,
                            fabrics1_mtrl_formula,fabrics2_mtrl_formula,if_snake_wire,snake_wire_diameter,snake_wire_mtrl_formula,if_hard_around,hard_around_mtrl_formula,
                            hard_around_hr_formula,if_pocket_around,pocket_around_spring_formula,pocket_around_fabrics_formula,if_side_iron,side_iron_mtrl_formula,
                            side_iron_hr_formula,glue_mtrl_formula,if_rsORwa,rsORwa_mtrl_formula,rsORwa_hr_formula,if_sponge_drilling,sponge_drilling_hr_formula,
                            fork_mtrl_formula,fork_hr_formula,cnail_mtrl_formula,cnail_hr_formula,sponge_mtrl_formula,sponge_hr_formula,edge_mtrl_formula,
                            edge_hr_formula,bz_mtrl_formula,felt_mtrl_formula,felt_hr_formula,packet_mtrl_formula,packet_hr_formula,sponge_mtrl_tc_formula,
                            spring_weight_formula,side_iron_weight_formula,hard_around_weight_formula,fabrics1_weight_formula,fabrics2_weight_formula,
                            felt_weight_formula,cnail_weight_formula,sponge_weight_formula,sponge_weight_tc_formula,edge_weight_formula,fork_weight_formula";
                            DbSqlHelper.Update(cmd, "u_bednet_type", null, request.bednet, "bednettypeid", fields);
                        }

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