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

            if (request.dept == null)
            {
                rslt.ErrMsg = "未提交部门信息";
                return;
            }

            if (string.IsNullOrEmpty(request.dept.deptname))
            {
                rslt.ErrMsg = "请录入部门名称!";
                return;
            }
            if (request.dept.pricelistid == null || request.dept.pricelistid <= 0)
            {
                rslt.ErrMsg = "请选择价格表!";
                return;
            }

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

                // 初始化属性
                AutoInit.AutoInitS(cmd, request.dept);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var dtNow = DateTime.Now;

                        if (request.dept.deptid == null || request.dept.deptid <= 0)
                        {

                            cmd.CommandText = @"SELECT COUNT(deptname) AS cnt FROM u_dept WHERE deptname = @deptname";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@deptname", request.dept.deptname);
                            var cnt = Convert.ToInt32(cmd.ExecuteScalar());
                            if (cnt > 0)
                            {
                                throw new LJCommonException("存在重复部门名称，请检查!");
                            }

                            //新建
                            request.dept.createtime = dtNow;
                            request.dept.deptid = BllHelper.GetID(cmd, "u_dept");
                            
                            var fields = "deptid,deptname,profitrate,pricelistid,springtypeid,createtime,moneyrate,discount,flag,if_rate_auto,manage_amt,mtrltype,managerate,com_profitrate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate,taxes_rate";
                            DbSqlHelper.Insert(cmd, "u_dept", null, request.dept, fields);
                        }
                        else
                        {
                            //修改
                            request.dept.moddate = dtNow;
                            request.dept.modemp = tokendata.username;

                            var fields = "deptname,profitrate,pricelistid,springtypeid,moneyrate,discount,flag,if_rate_auto,manage_amt,mtrltype,moddate,modemp,managerate,com_profitrate,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate,taxes_rate";
                            DbSqlHelper.Update(cmd, "u_dept", null, request.dept, "deptid", fields);
                        }

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