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

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

            if (string.IsNullOrEmpty(request.bednetvar.varname))
            {
                rslt.ErrMsg = "请录入名称";
                return;
            }

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

                var dtNow = DateTime.Now;

                AutoInit.AutoInitS(cmd, request.bednetvar);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if (request.bednetvar.varid <= 0)
                        {
                            request.bednetvar.varid = BllHelper.GetID(cmd, "u_bednet_var");

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

                            var fields = @"varid,varkind,varcode,varname,varclass,vartype,varunit,varvalue,dscrp,createtime,createby,formula,ifmxformula";
                            DbSqlHelper.Insert(cmd, "u_bednet_var", null, request.bednetvar, fields);
                        }
                        else
                        {
                            //修改
                            var fields = @"varcode,varname,varclass,vartype,varunit,varvalue,dscrp,formula,ifmxformula";
                            DbSqlHelper.Update(cmd, "u_bednet_var", null, request.bednetvar, "varid", fields);
                        }

                        if(request.bednetvar.mxlist != null && request.bednetvar.mxlist.Any())
                        {
                            // 新增明细
                            foreach (var item in request.bednetvar.mxlist)
                            {
                                AutoInit.AutoInitS(cmd, item);

                                if (item.varmxid <= 0)
                                {
                                    item.varmxid = BllHelper.GetID(cmd, "u_bednet_varmx");
                                    item.varid = request.bednetvar.varid;

                                    var fields = @"varmxid,varid,line_diameter,bednettypeid,varvalue,pricelistid,formula";
                                    DbSqlHelper.Insert(cmd, "u_bednet_varmx", null, item, fields);
                                } else
                                {
                                    //修改
                                    var fields = @"line_diameter,bednettypeid,varvalue,formula";
                                    DbSqlHelper.Update(cmd, "u_bednet_varmx", null, item, "varmxid", fields);
                                }
                            }
                        }

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