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 JLHHJSvr.DBA.DBModle;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetRoadListExcutor : ExcutorBase<GetRoadListRequest, GetRoadListResponse>
    {
        protected override void ExcuteInternal(GetRoadListRequest request, object state, GetRoadListResponse 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_road.roadid,
                           st_road.roadname,
                           st_road.groupname,
                           st_road.manager,
                           st_road.man_tel,
                           st_road.cnt,
                           ISNULL(vvv_count.soldCnt,0) AS soldCnt,
                           st_road.cnt - ISNULL(vvv_count.soldCnt,0) AS leftCnt,
                           ISNULL(vvv_count.expiringCnt,0) AS expiringCnt,
                           ISNULL(vvv_count.expiredCnt,0) AS expiredCnt,
                           st_road.dscrp,
                           st_road.opemp,
                           st_road.opdate,
                           st_road.modemp,
                           st_road.moddate
                    FROM st_road
                    LEFT OUTER JOIN
                      (SELECT roadid,
                              SUM(CASE WHEN enddate >= GETDATE() THEN 1 ELSE 0 END) AS soldCnt,
                              SUM(CASE WHEN enddate < @expiringDate
                                  AND enddate > GETDATE() THEN 1 ELSE 0 END) AS expiringCnt,
                              SUM(CASE WHEN enddate < GETDATE() THEN 1 ELSE 0 END) AS expiredCnt
                       FROM st_permit
                       WHERE st_permit.inuse = 1 AND st_permit.iftmp = 0
                       GROUP BY roadid) vvv_count ON st_road.roadid = vvv_count.roadid";
                var outputFields =
                    "roadid,roadname,groupname,manager,man_tel,cnt,soldCnt,leftCnt,expiringCnt,expiredCnt,dscrp,opemp,opdate,modemp,moddate";
                var whereList = new List<string>();
                var parms = new Dictionary<string, object>();
                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 (!string.IsNullOrEmpty(request.groupname))
                {
                    whereList.Add("st_road.groupname like @groupname");
                    parms.Add("@groupname", "%" + request.groupname + "%");
                }
                if (!string.IsNullOrEmpty(request.manager))
                {
                    whereList.Add("st_road.manager like @manager");
                    parms.Add("@manager", "%" + request.manager + "%");
                }
                if (request.ifLeftCnt == 1)
                {
                    whereList.Add("st_road.cnt > ISNULL(vvv_count.soldCnt,0)");
                }
                var whereStr = string.Empty;
                for (int i = 0; i < whereList.Count; i++)
                {
                    if (i == 0)
                    {
                        whereStr += "(" + whereList[i] + ")";
                    }
                    else
                    {
                        whereStr += " AND (" + whereList[i] + ")";
                    }
                }
                rslt.RoadList = new List<Road>();
                DbSqlHelper.SelectJoin(cmd, selectStr, whereStr, parms, null, outputFields, 0, 0, rslt.RoadList);
            }
        }
    }
}
