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 SaveMattressTypeExcutor : ExcutorBase<SaveMattressTypeRequest, SaveMattressTypeResponse>
    {
        protected override void ExcuteInternal(SaveMattressTypeRequest request, object state, SaveMattressTypeResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

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

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

            if (string.IsNullOrEmpty(request.mattress.typecode))
            {
                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.mattress);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if (request.mattress.mattresstypeid <= 0)
                        {
                            request.mattress.mattresstypeid = BllHelper.GetID(cmd, "u_mattress_type");

                            request.mattress.createtime = dtNow;
                            request.mattress.createby = tokendata.username;

                            var fields = @"mattresstypeid,typecode,typename,if_top_side,if_button_sdie,if_big_side,formula_big_side,if_small_side,
                                        formula_small_side,if_v_side,formula_v_side,if_in_cloth_cover,if_out_cloth_cover,biandaiqty,hrcost_formula,dianceng_sort,
                                        dianceng_area,if_small_side2,if_small_side3,if_v_side2,if_v_side3,formula_big_side,formula_small_side,formula_v_side,
                                        zhizao_amt,guanli_rate,createtime,createby,typerate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate";
                            DbSqlHelper.Insert(cmd, "u_mattress_type", null, request.mattress, fields);
                        }
                        else
                        {
                            //修改
                            var fields = @"typecode,typename,if_top_side,if_button_sdie,if_big_side,formula_big_side,if_small_side,
                                        formula_small_side,if_v_side,formula_v_side,if_in_cloth_cover,if_out_cloth_cover,biandaiqty,hrcost_formula,dianceng_sort,
                                        dianceng_area,if_small_side2,if_small_side3,if_v_side2,if_v_side3,formula_big_side,formula_small_side,formula_v_side,
                                        zhizao_amt,guanli_rate,typerate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate";
                            DbSqlHelper.Update(cmd, "u_mattress_type", null, request.mattress, "mattresstypeid", fields);
                        }

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