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

namespace JLHHJSvr.Excutor
{
    internal sealed class GetPermitListExcutor : ExcutorBase<GetPermitListRequest, GetPermitListResponse>
    {
        protected override void ExcuteInternal(GetPermitListRequest request, object state, GetPermitListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

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

                var selectStr = @"
                    SELECT 
                        st_permit.pid,
                        st_road.roadname,
                        st_road.roadid,
                        st_permit.carnum,
                        st_permit.owner,
                        st_permit.owner_tel,
                        st_permit.begindate,
                        st_permit.enddate,
                        st_permit.inuse,
                        st_permit.dscrp,
                        st_permit.paytype,
                        st_permit.payamt,
                        st_permit.opemp,
                        st_permit.opdate,
                        st_permit.modemp,
                        st_permit.moddate,
                        CASE WHEN enddate > GETDATE() AND enddate < @expiringDate THEN 1 ELSE 0 END AS expiring";
                var fromStr = @"
                    FROM st_permit
                    INNER JOIN st_road ON st_permit.roadid = st_road.roadid ";
                var outputFields =
                    "pid,roadname,roadid,carnum,owner,owner_tel,begindate,enddate,inuse,dscrp,paytype,payamt,opemp,opdate,modemp,moddate,expiring";
                var whereList = new List<string>(){"st_permit.iftmp = @iftmp"};
                var parms = new Dictionary<string, object>() {{"@iftmp", request.iftmp}};
                //设置即将过期时间
                var stOption = new st_option { optionid = 4 };
                var expiringOption = Convert.ToInt32(stOption.GetValue(cmd));
                var expiringDate = DateTime.Now.AddDays(expiringOption);
                parms.Add("@expiringDate", expiringDate);
                if (request.iftmp == 1)
                {
                    //请求备案列表
                    selectStr += @",
                        ISNULL(vvv_bill_count.violateCnt,0) AS violateCnt,
                        ISNULL(vvv_record_count.recordCnt,0) AS recordCnt";
                    fromStr += @"
                    LEFT OUTER JOIN (
                            SELECT carnum, COUNT(pid) AS recordCnt
                            FROM st_permit
                            WHERE iftmp = 1 
                                AND opdate >= @dateA
                                AND opdate <= @dateZ
                            GROUP BY carnum
                            ) AS vvv_record_count ON st_permit.carnum = vvv_record_count.carnum
                        LEFT OUTER JOIN (
                            SELECT carnum,COUNT(billid) AS violateCnt
                            FROM st_bill
                            WHERE opdate >= @dateA
                                AND opdate <= @dateZ
                            GROUP BY carnum
                            ) AS vvv_bill_count ON st_permit.carnum = vvv_bill_count.carnum";
                    outputFields += ",violateCnt,recordCnt";
                    var dateZ = DateTime.Now;
                    var dateA = new DateTime(dateZ.Year, dateZ.Month, 1);
                    parms.Add("@dateA", dateA);
                    parms.Add("@dateZ", dateZ);
                }
                if (request.begindate != null)
                {
                    request.begindate = new DateTime(request.begindate.Value.Year, request.begindate.Value.Month,
                        request.begindate.Value.Day);
                    whereList.Add("st_permit.enddate >= @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_permit.enddate <= @enddate");
                    parms.Add("@enddate", request.enddate);
                }
                if (!string.IsNullOrEmpty(request.carnum))
                {
                    whereList.Add("st_permit.carnum like @carnum");
                    parms.Add("@carnum","%"+request.carnum+"%");
                }
                if (!string.IsNullOrEmpty(request.owner_tel))
                {
                    whereList.Add("st_permit.owner_tel like @owner_tel");
                    parms.Add("@owner_tel", "%" + request.owner_tel + "%");
                }
                if (!string.IsNullOrEmpty(request.owner))
                {
                    whereList.Add("st_permit.owner like @owner");
                    parms.Add("@owner", "%" + request.owner + "%");
                }
                if (request.ifExpiring == 1)
                {
                    whereList.Add("enddate > GETDATE() AND enddate < @expiringDate");
                    
                }
                var whereStr = string.Empty;
                for (int i = 0; i < whereList.Count; i++)
                {
                    if (i == 0)
                    {
                        whereStr += "(" + whereList[i] + ")";
                    }
                    else
                    {
                        whereStr += " AND (" + whereList[i] + ")";
                    }
                }
                rslt.PermitList = new List<Permit>();
                DbSqlHelper.SelectJoin(cmd,selectStr+fromStr,whereStr,parms,null,outputFields,0,0,rslt.PermitList);
            }
        }
    }
}
