using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.DBA.DBModle;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class SavePermitListExcutor : ExcutorBase<SavePermitListRequest, SavePermitListResponse>
    {
        protected override void ExcuteInternal(SavePermitListRequest request, object state, SavePermitListResponse rslt)
        {
            // TODO: 保存车位信息
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

            foreach (var permit in request.PermitList)
            {
                if (string.IsNullOrEmpty(permit.carnum))
                {
                    rslt.ErrMsg = "车牌号码不能为空";
                    return;
                }
                permit.carnum = permit.carnum.ToUpper();
                if (permit.roadid == null)
                {
                    permit.roadid = 0;
                }
                if (permit.roadname == null)
                {
                    permit.roadname = string.Empty;
                }
                if (permit.roadid == 0 && string.IsNullOrEmpty(permit.roadname))
                {
                    rslt.ErrMsg = "路段不能为空";
                    return;
                }
                if (permit.pid == null)
                {
                    permit.pid = 0;
                }
                if (permit.iftmp == null)
                {
                    permit.iftmp = 0;
                }
            }


            rslt.pidList = new List<int>();

            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 permit in request.PermitList)
                        {
                            var carinfo = new st_car() {carnum = permit.carnum};
                            if (DbSqlHelper.SelectOne(cmd, carinfo, "carnum,owner,owner_tel") != 1)
                            {
                                // 无车辆信息，新建一个
                                carinfo.owner = permit.owner;
                                carinfo.owner_tel = permit.owner_tel;
                                carinfo.opemp = tokendata.username;
                                carinfo.opdate = dtNow;
                                DbSqlHelper.Insert(cmd, carinfo, "carnum,owner,owner_tel,opemp,opdate");
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(permit.owner_tel))
                                {
                                    // 有车辆信息但当前无留电话，使用车辆信息中的电话
                                    permit.owner_tel = carinfo.owner_tel;
                                }
                                if (string.IsNullOrEmpty(permit.owner))
                                {
                                    // 有车辆信息但当前无留姓名，使用车辆信息中的姓名
                                    permit.owner = carinfo.owner;
                                }
                                if (permit.inuse != 1 && (carinfo.owner != permit.owner || carinfo.owner_tel != permit.owner_tel))
                                {
                                    // 车位信息与车辆信息不一至，用车位信息更新
                                    carinfo.owner = permit.owner;
                                    carinfo.owner_tel = permit.owner_tel;
                                    carinfo.modemp = tokendata.username;
                                    carinfo.moddate = dtNow;
                                    DbSqlHelper.Update(cmd, carinfo, "owner,owner_tel,modemp,moddate");
                                }
                            }

                            if (permit.roadid > 0)
                            {
                                var roadinfo = new st_road() {roadid = permit.roadid};
                                if (DbSqlHelper.SelectOne(cmd, roadinfo, "roadid,roadname") != 1)
                                {
                                    cmd.Transaction.Rollback();
                                    rslt.ErrMsg = string.Format("路段已经不存在,roadid:{0}", permit.roadid);
                                    return;
                                }
                            }

                            if (permit.roadid == 0)
                            {
                                var roadinfo = new st_road() {roadname = permit.roadname};
                                if (DbSqlHelper.SelectOne(cmd, roadinfo, "roadid,roadname") != 1)
                                {
                                    roadinfo.roadid = BllHelper.GetID(cmd, "st_road");
                                    roadinfo.opemp = tokendata.username;
                                    roadinfo.opdate = dtNow;
                                    DbSqlHelper.Insert(cmd, roadinfo, "roadid,roadname,opemp,opdate");
                                }
                                permit.roadid = roadinfo.roadid;
                            }

                            if (permit.pid == 0)
                            {
                                permit.pid = BllHelper.GetID(cmd, "st_permit");
                                permit.opemp = tokendata.username;
                                permit.opdate = dtNow;
                                var fields =
                                    "pid,carnum,owner,owner_tel,begindate,enddate,inuse,roadid,dscrp,opemp,opdate,paytype,payamt,iftmp";
                                if (permit.iftmp == 1 && permit.inuse == 1)
                                {
                                    // 自动审核
                                    permit.modemp = tokendata.username;
                                    permit.moddate = dtNow;
                                    fields += ",modemp,moddate";
                                }
                                DbSqlHelper.Insert(cmd, "st_permit", null, permit, fields);
                                rslt.pidList.Add(permit.pid.Value);
                            }
                            else
                            {
                                permit.modemp = tokendata.username;
                                permit.moddate = dtNow;
                                DbSqlHelper.Update(cmd, "st_permit", null, permit, "pid", "carnum,owner,owner_tel,begindate,enddate,inuse,roadid,dscrp,modemp,moddate,paytype,payamt,iftmp");
                            }
                        }

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