96 lines
3.1 KiB
C#
96 lines
3.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using System.Web.Http;
|
||
|
||
/// <summary>
|
||
/// guadanStatisticsTable 的摘要描述
|
||
/// </summary>
|
||
///
|
||
[ezAuthorize]
|
||
public class guadanStatisticsTableController: ApiController
|
||
{
|
||
private Model.ezEntities _db = new Model.ezEntities();
|
||
public guadanStatisticsTableController()
|
||
{
|
||
//
|
||
// TODO: 在這裡新增建構函式邏輯
|
||
//
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("api/guadan/guadanstatisticstable/list")]
|
||
public IHttpActionResult Get([FromUri] DateTime? start, [FromUri] DateTime? end)
|
||
{
|
||
// 如果兩個都為空,設定 start = 今天,end = 一個月後
|
||
if (!start.HasValue && !end.HasValue)
|
||
{
|
||
start = DateTime.Today;
|
||
end = DateTime.Today.AddMonths(1);
|
||
}
|
||
|
||
// 如果有 start 沒有 end,就給 end 預設一個月後
|
||
if (start.HasValue && !end.HasValue)
|
||
{
|
||
end = start.Value.Date.AddMonths(1);
|
||
}
|
||
|
||
// 如果有 end 沒有 start,就給 start 預設 end 的前一個月
|
||
if (!start.HasValue && end.HasValue)
|
||
{
|
||
start = end.Value.Date.AddMonths(-1);
|
||
}
|
||
|
||
// 最後確保只取 date 部分
|
||
var startDate = start.Value.Date;
|
||
var endDate = end.Value.Date;
|
||
|
||
// 查詢資料庫時就用 date 型別對應
|
||
var statistics = _db.RegionAndRoomAndBedSchedule
|
||
.Where(s => s.IsCancel == false)
|
||
.Where(s => s.ScheduleDate >= startDate && s.ScheduleDate <= endDate)
|
||
.GroupBy(s => s.ScheduleDate)
|
||
.Select(g => new
|
||
{
|
||
date = g.Key,
|
||
todaytotalbookers = g.Count(), // 该日期的总记录数
|
||
checkin = g.Key <= DateTime.Today
|
||
? g.Count(x => x.GuaDanOrderGuest.RegionRoomBedStatus.Code == "402"
|
||
|| x.GuaDanOrderGuest.RegionRoomBedStatus.Code == "403")
|
||
: 0,
|
||
})
|
||
.OrderBy(x => x.date)
|
||
.ToList();
|
||
var todayDate = DateTime.Today;
|
||
dynamic today = statistics.FirstOrDefault(x => x.date == todayDate);
|
||
|
||
if (today == null)
|
||
{
|
||
var todayCount = _db.RegionAndRoomAndBedSchedule
|
||
.Where(s => s.ScheduleDate == todayDate && !s.IsCancel)
|
||
.Count();
|
||
|
||
today = new { date = todayDate, todaytotalbookers = todayCount };
|
||
}
|
||
|
||
var bedcount = _db.RegionRoomBed
|
||
.Where(a => a.IsDeleted == false)
|
||
.Count();
|
||
var roomcount = _db.Room.Count();
|
||
var result = new
|
||
{
|
||
bedcount,
|
||
roomcount,
|
||
statistics,
|
||
today,
|
||
totalbookers = _db.RegionAndRoomAndBedSchedule
|
||
.Where(s => s.IsCancel == false)
|
||
.Where(s => s.ScheduleDate >= DateTime.Today)
|
||
.Count(),
|
||
};
|
||
|
||
return Ok(result);
|
||
}
|
||
|
||
} |