using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
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 GetPermitListQueryExcutor : ExcutorBase<GetPermitListQueryRequest, GetPermitListQueryResponse>
    {
        protected override void ExcuteInternal(GetPermitListQueryRequest request, object state, GetPermitListQueryResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (string.IsNullOrEmpty(request.carnum))
            {
                rslt.ErrMsg = "未输入搜索关键字";
                return;
            }
            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var selectStr = @"
                    SELECT 
                        st_permit.carnum,
                        st_permit.owner,
                        st_permit.owner_tel,
                        st_permit.iftmp,
                        st_permit.inuse,
                        st_permit.dscrp,
                        st_permit.opdate,
                        st_permit.begindate,
                        st_permit.enddate,
                        st_road.roadname,
                        st_road.manager,
                        st_road.man_tel
                    FROM st_permit
                        LEFT OUTER JOIN st_road ON st_permit.roadid = st_road.roadid
                    WHERE UPPER(st_permit.carnum) like UPPER(@carnum)";
                var outputFields =
                    "carnum,owner,owner_tel,iftmp,inuse,dscrp,begindate,enddate,roadname,manager,man_tel";
//                var whereStr = "UPPER(st_car.carnum) like UPPER(@carnum)";
//                var orderStr = "st_permit.opdate DESC";
                var parms = new Dictionary<string, object> {{"@carnum", "%" + request.carnum + "%"}};
                rslt.permitList = new List<Permit>();
                DbSqlHelper.SelectJoin(cmd, selectStr, null, parms, "inuse DESC,enddate DESC", outputFields, 0, 0, rslt.permitList);
            }
        }
    }
}
