using System;
using System.Collections.Generic;
using System.Data;
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;
using LJLib.SQLEX;

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

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

                var selectStr = @"
                    SELECT 
                        st_road.roadname,
                        st_road.roadid,
                        st_bill.billid,
                        st_bill.carnum,
                        st_bill.owner,
                        st_bill.owner_tel,
                        st_bill.dscrp,
                        st_bill.paytype,
                        st_bill.payamt,
                        st_bill.flag,
                        st_bill.opemp,
                        st_bill.opdate,
                        st_bill.auditemp,
                        st_bill.auditdate,
                        st_bill.modemp,
                        st_bill.moddate,
                        ISNULL(vvv_bill_count.violateCnt,0) AS violateCnt,
                        ISNULL(vvv_record_count.recordCnt,0) AS recordCnt
                    FROM st_bill
                        INNER JOIN st_road ON st_bill.roadid = st_road.roadid
                    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_bill.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_bill.carnum = vvv_bill_count.carnum";
                var outputFields =
                    "roadname,roadid,billid,carnum,owner,owner_tel,dscrp,paytype,payamt,flag,opemp,opdate,auditemp,auditdate,modemp,moddate,violateCnt,recordCnt";
                var orderStr = "opdate DESC";
                var whereList = new List<string>();
                var parms = new Dictionary<string, object>();
                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_bill.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_bill.opdate <= @enddate");
                    parms.Add("@enddate", request.enddate);
                }
                if (!string.IsNullOrEmpty(request.carnum))
                {
                    whereList.Add("st_bill.carnum like @carnum");
                    parms.Add("@carnum", "%" + request.carnum + "%");
                }
                if (!string.IsNullOrEmpty(request.owner_tel))
                {
                    whereList.Add("st_bill.owner_tel like @owner_tel");
                    parms.Add("@owner_tel", "%" + request.owner_tel + "%");
                }
                if (!string.IsNullOrEmpty(request.owner))
                {
                    whereList.Add("st_bill.owner like @owner");
                    parms.Add("@owner", "%" + request.owner + "%");
                }
                if (request.flag != -1)
                {
                    whereList.Add("st_bill.flag = @flag");
                    parms.Add("@flag",request.flag);
                }
                var whereStr = string.Empty;
                for (int i = 0; i < whereList.Count; i++)
                {
                    if (i == 0)
                    {
                        whereStr += "(" + whereList[i] + ")";
                    }
                    else
                    {
                        whereStr += " AND (" + whereList[i] + ")";
                    }
                }
                rslt.billList = new List<Bill>();
                DbSqlHelper.SelectJoin(cmd, selectStr, whereStr, parms, orderStr, outputFields, 0, 0, rslt.billList);
                cmd.CommandText = "SELECT filemd5 FROM st_bill_mx WHERE billid = @billid";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@billid",SqlDbType.Int);
                foreach (var bill in rslt.billList)
                {
                    cmd.Parameters["@billid"].Value = bill.billid.Value;
                    bill.pictureList = new List<FileInfo>();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            bill.pictureList.Add(new FileInfo()
                            {
                                md5 = Convert.ToString(reader["filemd5"])
                            });
                        }
                    }
                }
                rslt.amtPerDay = Convert.ToDecimal(new st_option().GetValue(cmd, 2));
            }
        }
    }
}
