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

namespace JLHHJSvr.Excutor
{
    internal sealed class GetCarListExcutor : ExcutorBase<GetCarListRequest, GetCarListResponse>
    {
        protected override void ExcuteInternal(GetCarListRequest request, object state, GetCarListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.violateBegindate == null)
            {
                request.violateBegindate = DateTime.MinValue;
            }
            else
            {
                request.violateBegindate = new DateTime(request.violateBegindate.Value.Year, request.violateBegindate.Value.Month,
                        request.violateBegindate.Value.Day);
            }
            if (request.violateEnddate == null)
            {
                request.violateEnddate = DateTime.MaxValue;
            }
            else
            {
                request.violateEnddate = new DateTime(request.violateEnddate.Value.Year, request.violateEnddate.Value.Month,
                        request.violateEnddate.Value.Day, 23, 59, 59);
            }

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

                var selectStr = @"
                    SELECT
                        st_car.carnum,
                        st_car.owner,
                        st_car.owner_tel,
                        st_car.dscrp,
                        st_car.opemp,
                        st_car.opdate,
                        st_car.modemp,
                        st_car.moddate,
                        st_car.ifinput,
                        ISNULL(vvv_bill_count.violateCnt,0) AS violateCnt
                    FROM st_car
                    LEFT OUTER JOIN (
                        SELECT carnum,COUNT(billid) AS violateCnt
                        FROM st_bill
                        WHERE opdate >= @violateBegindate
                            AND opdate <= @violateEnddate
                        GROUP BY carnum
                        ) AS vvv_bill_count ON st_car.carnum = vvv_bill_count.carnum";
                var outputFields =
                    "pid,roadname,roadid,carnum,owner,owner_tel,begindate,enddate,inuse,dscrp,paytype,payamt,opemp,opdate,modemp,moddate";
                var whereList = new List<string>();
                var parms = new Dictionary<string, object>()
                {
                    {"@violateBegindate", request.violateBegindate},
                    {"@violateEnddate", request.violateEnddate}
                };
                if (request.begindate != null)
                {
                    request.begindate = new DateTime(request.begindate.Value.Year, request.begindate.Value.Month,
                        request.begindate.Value.Day);
                    whereList.Add("st_car.opdate >= @begindate");
                    parms.Add("@begindate", request.begindate);
                }
                if (request.enddate != null)
                {
                    request.enddate = new DateTime(request.enddate.Value.Year, request.enddate.Value.Month,
                        request.enddate.Value.Day, 23, 59, 59);
                    whereList.Add("st_car.opdate <= @enddate");
                    parms.Add("@enddate", request.enddate);
                }
                if (!string.IsNullOrEmpty(request.carnum))
                {
                    whereList.Add("st_car.carnum like @carnum");
                    parms.Add("@carnum", "%" + request.carnum + "%");
                }
                if (!string.IsNullOrEmpty(request.owner_tel))
                {
                    whereList.Add("st_car.owner_tel like @owner_tel");
                    parms.Add("@owner_tel", "%" + request.owner_tel + "%");
                }
                if (!string.IsNullOrEmpty(request.owner))
                {
                    whereList.Add("st_car.owner like @owner");
                    parms.Add("@owner", "%" + request.owner + "%");
                }
                var whereStr = string.Empty;
                for (int i = 0; i < whereList.Count; i++)
                {
                    if (i == 0)
                    {
                        whereStr += "(" + whereList[i] + ")";
                    }
                    else
                    {
                        whereStr += " AND (" + whereList[i] + ")";
                    }
                }
                rslt.carList = new List<Car>();
                DbSqlHelper.SelectJoin(cmd, selectStr, whereStr, parms, null, outputFields, 0, 0, rslt.carList);
            }
        }
    }
}
