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

            if (!request.list.Any())
            {
                rslt.ErrMsg = "未提交工厂利润率信息";
                return;
            }

            foreach(var profitrate in request.list)
            {
                if (profitrate.profitrate < 0)
                {
                    rslt.ErrMsg = "利润率有误,请检查!";
                    return;
                }
            }

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

                var dtNow = DateTime.Now;

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        foreach (var profitrate in request.list)
                        {
                            cmd.CommandText = @"SELECT COUNT(*) AS cnt
                                    FROM u_factory_profitrate
                                    WHERE u_factory_profitrate.deptid = @deptid
	                                    AND u_factory_profitrate.bednettypeid_mattresstypeid = @typid
	                                    AND u_factory_profitrate.bednet_or_mattress = @kind";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@deptid", profitrate.deptid);
                            cmd.Parameters.AddWithValue("@typid", profitrate.bednettypeid_mattresstypeid);
                            cmd.Parameters.AddWithValue("@kind", profitrate.bednet_or_mattress);
                            var cnt = Convert.ToInt32(cmd.ExecuteScalar());

                            if (cnt <= 0)
                            {
                                AutoInit.AutoInitS(cmd, profitrate);

                                profitrate.createtime = dtNow;
                                profitrate.createby = tokendata.username;

                                var fields = "deptid,bednet_or_mattress,bednettypeid_mattresstypeid,createtime,profitrate,createby";
                                DbSqlHelper.Insert(cmd, "u_factory_profitrate", null, profitrate, fields);
                            }
                            else
                            {
                                //修改
                                var fields = "profitrate";
                                DbSqlHelper.Update(cmd, "u_factory_profitrate", null, profitrate, "deptid,bednet_or_mattress,bednettypeid_mattresstypeid", fields);
                            }
                        }

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