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

            if (request.carList == null || request.carList.Count == 0)
            {
                rslt.ErrMsg = "未提交车辆信息";
                return;
            }
            foreach (var carInfo in request.carList)
            {
                if (carInfo == null)
                {
                    rslt.ErrMsg = "路段ID异常";
                    return;
                }
                if (string.IsNullOrEmpty(carInfo.carnum))
                {
                    rslt.ErrMsg = "车牌不能为空";
                    return;
                }
                carInfo.carnum = carInfo.carnum.ToUpper();
                if (string.IsNullOrEmpty(carInfo.owner))
                {
                    rslt.ErrMsg = "车主姓名不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(carInfo.owner_tel))
                {
                    rslt.ErrMsg = "车主电话不能为空";
                    return;
                }
                if (carInfo.dscrp == null)
                {
                    rslt.ErrMsg = "备注信息异常";
                    return;
                }
                if (carInfo.ifinput == null)
                {
                    carInfo.ifinput = 0;
                }

                if (!string.IsNullOrEmpty(carInfo.carnum_ori))
                {
                    carInfo.carnum_ori = carInfo.carnum_ori.ToUpper();
                }
            }

            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 carInfo in request.carList)
                        {
                            if (!string.IsNullOrEmpty(carInfo.carnum) && !carInfo.carnum_ori.Equals(carInfo.carnum))
                            {
                                cmd.CommandText = @"SELECT COUNT(carnum) AS cnt FROM st_permit WHERE carnum = @carnum_ori ";
                                cmd.Parameters.Clear();
                                cmd.Parameters.AddWithValue("@carnum_ori", carInfo.carnum_ori);
                                var cnt = Convert.ToInt32(cmd.ExecuteScalar());
                                if (cnt > 0)
                                {
                                    rslt.ErrMsg = string.Format("有{0}个与修改前车牌相关的车位信息，无法修改。", cnt);
                                    return;
                                }
                            }
                            cmd.CommandText = "SELECT carnum FROM st_car WHERE carnum = @carnum";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@carnum", carInfo.carnum);
                            using (var reader = cmd.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    if (!carInfo.carnum_ori.Equals(carInfo.carnum))
                                    {
                                        throw new Exception(string.Format("已存在车牌为[{0}]的记录，操作失败",carInfo.carnum));
                                    }
                                }
                            }
                            
                            if (string.IsNullOrEmpty(carInfo.carnum_ori))
                            {
                                //新建
                                carInfo.opdate = dtNow;
                                carInfo.opemp = tokendata.username;
                                var fields = "owner,owner_tel,dscrp,ifinput,carnum,opemp,opdate";
                                DbSqlHelper.Insert(cmd, "st_car", null, carInfo, fields);
                            }
                            else
                            {
                                //修改
                                carInfo.moddate = dtNow;
                                carInfo.modemp = tokendata.username;
                                cmd.CommandText = @"
                                    UPDATE st_car
                                    SET carnum = @carnum,
                                        owner = @owner,
                                        owner_tel = @owner_tel,
                                        dscrp = @dscrp,
                                        ifinput = @ifinput,
                                        modemp = @modemp,
                                        moddate = @moddate
                                    WHERE carnum = @carnum_ori";
                                cmd.Parameters.Clear();
                                cmd.Parameters.AddWithValue("carnum", carInfo.carnum);
                                cmd.Parameters.AddWithValue("owner", carInfo.owner);
                                cmd.Parameters.AddWithValue("owner_tel", carInfo.owner_tel);
                                cmd.Parameters.AddWithValue("dscrp", carInfo.dscrp);
                                cmd.Parameters.AddWithValue("ifinput", carInfo.ifinput);
                                cmd.Parameters.AddWithValue("modemp", carInfo.modemp);
                                cmd.Parameters.AddWithValue("moddate", carInfo.moddate);
                                cmd.Parameters.AddWithValue("carnum_ori", carInfo.carnum_ori);
                                cmd.ExecuteNonQuery();
                            }
                        }

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