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

namespace JLHHJSvr.Excutor.APP
{
    internal sealed class TmpGetRoadListExcutor : ExcutorBase<TmpGetRoadListRequest, TmpGetRoadListResponse>
    {
        private List<Road> tmpList;
        private DateTime lastUpdateTime;
        protected override void ExcuteInternal(TmpGetRoadListRequest request, object state, TmpGetRoadListResponse rslt)
        {
            if (tmpList != null)
            {
                //防止频繁查询数据库
                if (lastUpdateTime.AddMinutes(5) > DateTime.Now)
                {
                    rslt.roadList = tmpList;
                    return;
                }
            }
            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                cmd.CommandText = @"
                    SELECT st_road.roadid,
                           st_road.roadname
                    FROM st_road";
                tmpList = new List<Road>();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        tmpList.Add(new Road()
                        {
                            roadid = Convert.ToInt32(reader["roadid"]),
                            roadname = Convert.ToString(reader["roadname"])
                        });
                    }
                }
                rslt.roadList = tmpList;
                lastUpdateTime = DateTime.Now;
            }
        }
    }
}
