using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web.UI;
using JLHHJSvr.BLL;
using JLHHJSvr.Com.APP;
using JLHHJSvr.DBA.DBModle;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor.APP
{
    internal sealed class TmpSavePermitExcutor : ExcutorBase<TmpSavePermitRequest, TmpSavePermitResponse>
    {
        protected override void ExcuteInternal(TmpSavePermitRequest request, object state, TmpSavePermitResponse rslt)
        {
            if (request.roadid == 0)
            {
                rslt.ErrMsg = "未选择路段";
                return;
            }
            if (string.IsNullOrEmpty(request.carnum))
            {
                rslt.ErrMsg = "未填写车牌号码";
                return;
            }
            request.carnum = request.carnum.ToUpper();

            if (request.dscrp == null)
            {
                rslt.ErrMsg = "备注信息异常";
                return;
            }
            if (string.IsNullOrEmpty(request.owner))
            {
                rslt.ErrMsg = "未填写车主姓名";
                return;
            }
            if (string.IsNullOrEmpty(request.owner_tel))
            {
                rslt.ErrMsg = "未填写车主电话";
                return;
            }

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

                var opendate = DateTime.Parse(new st_option().GetValue(cmd, 7));
                var closedate = DateTime.Parse(new st_option().GetValue(cmd, 5));
                if (!opendate.Equals(closedate))
                {
                    if (closedate <= opendate)
                    {
                        if (DateTime.Now < opendate)
                        {
                            opendate = opendate.AddDays(-1);
                        }
                        else
                        {
                            closedate = closedate.AddDays(1);
                        }
                    }
                    if (DateTime.Now < opendate || DateTime.Now > closedate)
                    {
                        rslt.ErrMsg = "当前不在报备开放的时间段，报备失败！";
                        return;
                    }
                }

                var stOption = new st_option {optionid = 3};
                var maxRecordCnt = Convert.ToInt32(stOption.GetValue(cmd));
                var dtNow = DateTime.Now;
                var thismonth = new DateTime(dtNow.Year, dtNow.Month, 1);
                cmd.CommandText = @"
                    SELECT COUNT(pid)
                    FROM st_permit
                    WHERE carnum = @carnum
                        AND iftmp = 1
                        AND inuse = 1
                        AND opdate > @thismonth";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@carnum", request.carnum);
                cmd.Parameters.AddWithValue("thismonth", thismonth);
                var thisMonthCnt = Convert.ToInt32(cmd.ExecuteScalar());
                if (thisMonthCnt >= maxRecordCnt)
                {
                    rslt.ErrMsg = string.Format("本月已经备案{0}次，不能再申请", maxRecordCnt);
                    return;
                }

                cmd.CommandText = @"
                    SELECT pid
                    FROM st_permit
                    WHERE carnum = @carnum
                    AND begindate < GETDATE()
                    AND enddate > GETDATE()
                    AND iftmp = 1
                    AND inuse = 1";

                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        rslt.ErrMsg = "今天已经备案，无需重复备案";
                        return;
                    }
                }

                var begindate = DateTime.Now;
                DateTime tomorrow = DateTime.Parse(new st_option().GetValue(cmd, 5));
                if ((begindate.Hour + begindate.Minute/60) >= (tomorrow.Hour + tomorrow.Minute/60))
                {
                    tomorrow = tomorrow.AddDays(1);
                }
//                var enddate = new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 8, 0, 0);
                var enddate = tomorrow;
                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var stPermit = new st_permit
                        {
                            pid = BllHelper.GetID(cmd, "st_permit"),
                            carnum = request.carnum,
                            owner = request.owner,
                            owner_tel = request.owner_tel,
                            begindate = begindate,
                            enddate = enddate,
                            iftmp = 1,
                            inuse = 1,
                            roadid = request.roadid,
                            dscrp = request.dscrp,
                            opemp = "自助备案",
                            opdate = DateTime.Now
                        };
                        DbSqlHelper.Insert(cmd, stPermit,
                            "pid,carnum,owner,owner_tel,begindate,enddate,iftmp,inuse,roadid,dscrp,opemp,opdate");

                        var stCar = new st_car()
                        {
                            carnum = request.carnum
                        };
                        if (DbSqlHelper.SelectOne(cmd, stCar, "carnum") != 1)
                        {
                            stCar.owner = request.owner;
                            stCar.owner_tel = request.owner_tel;
                            stCar.opemp = "自助备案";
                            stCar.opdate = DateTime.Now;
                            DbSqlHelper.Insert(cmd, stCar, "carnum,owner,owner_tel,opemp,opdate");
                        }

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