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

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

            if (request.roadList == null || request.roadList.Count == 0)
            {
                rslt.ErrMsg = "未提交路段信息";
                return;
            }
            foreach (var roadInfo in request.roadList)
            {
                if (roadInfo.roadid == null)
                {
                    rslt.ErrMsg = "路段ID异常";
                    return;
                }
                if (string.IsNullOrEmpty(roadInfo.roadname))
                {
                    rslt.ErrMsg = "路段名不能为空";
                    return;
                }
//                if (string.IsNullOrEmpty(roadInfo.groupname))
//                {
//                    rslt.ErrMsg = "分组名不能为空";
//                    return;
//                }
                if (string.IsNullOrEmpty(roadInfo.manager))
                {
                    rslt.ErrMsg = "管理员名不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(roadInfo.man_tel))
                {
                    rslt.ErrMsg = "管理员电话不能为空";
                    return;
                }
                if (roadInfo.cnt == null)
                {
                    rslt.ErrMsg = "车位数设置异常";
                    return;
                }
                if (roadInfo.dscrp == null)
                {
                    rslt.ErrMsg = "备注信息异常";
                    return;
                }
            }

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

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var dtNow = DateTime.Now;
                        foreach (var roadInfo in request.roadList)
                        {
                            cmd.CommandText = "SELECT roadid FROM st_road WHERE roadname = @roadname";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@roadname", roadInfo.roadname);
                            using (var reader = cmd.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    var roadid = Convert.ToInt32(reader["roadid"]);
                                    if (roadid != roadInfo.roadid)
                                    {
                                        throw new Exception(string.Format("已存在路段名为[{0}]的记录，不能重复添加",roadInfo.roadname));
                                    }
                                }
                            }
                            var fields = "roadname,groupname,manager,man_tel,cnt,dscrp";
                            if (roadInfo.roadid == 0)
                            {
                                //新建
                                
                                roadInfo.roadid = BllHelper.GetID(cmd, "st_road");
                                roadInfo.opdate = dtNow;
                                roadInfo.opemp = tokendata.username;
                                fields += ",roadid,opemp,opdate";
                                DbSqlHelper.Insert(cmd, "st_road", null, roadInfo, fields);
                            }
                            else
                            {
                                //修改
                                roadInfo.moddate = dtNow;
                                roadInfo.modemp = tokendata.username;
                                fields += ",modemp,moddate";
                                DbSqlHelper.Update(cmd, "st_road", null, roadInfo, "roadid", fields);
                            }
                        }
                        
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction.Rollback();
                        rslt.ErrMsg = e.ToString();
                        return;
                    }
                }
            }
        }
    }
}
