tycg_carviolation_BE/Traffic.Api/Controllers/EventController.cs

442 lines
15 KiB
C#

using ClosedXML.Excel;
using LiangLiSystem.Services.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Traffic.Data.ViewModels;
using Traffic.Service.Interfaces;
namespace Traffic.Api.Controllers
{
/// <summary>
/// 事件
/// </summary>
[Route("api/event")]
[Authorize]
[ApiController]
public class EventController : ControllerBase
{
private readonly IEventService _service;
private readonly IFileService _fileService;
private readonly IEventTypeService _eventTypeService;
public EventController(IFileService fileService, IEventService service, IEventTypeService eventTypeService)
{
_fileService = fileService;
_service = service;
_eventTypeService = eventTypeService;
}
/// <summary>
/// 取得事件列表 Term = 車牌號碼(Number)
/// </summary>
/// <param name="searchModel"></param>
/// <returns></returns>
/// <remarks>Term = 車牌號碼(Number)</remarks>
[HttpGet("")]
public IActionResult GetEvents([FromQuery] SearchModelViewModel searchModel)
{
try
{
var result = new Response();
var model = Util.GetSearchModel(searchModel);
result.Result = _service.GetEventsByCar(model);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///// <summary>
///// 取得某站點的事件列表
///// </summary>
///// <param name="siteId"></param>
///// <param name="searchModel"></param>
///// <returns></returns>
///// <remarks>Term = 車牌號碼(Number)</remarks>
//[HttpGet("siteId/{siteId}")]
//public IActionResult GetEventBySites(string siteId, [FromQuery] SearchModelViewModel searchModel)
//{
// var result = new Response();
// var model = Util.GetSearchModel(searchModel);
// result.Result = _service.GetEventBySites(siteId, model);
// return Ok(result);
//}
/// <summary>
/// 取得開單畫面的事件
/// </summary>
/// <param name="eventType">違規項目</param>
/// <param name="startTime">起始時間</param>
/// <param name="endTime">結束時間</param>
/// <param name="site">站點</param>
/// <param name="status">狀態</param>
/// <param name="time">違停時間</param>
/// <param name="speed">速度</param>
/// <param name="method">0 : 查詢, 1:匯出</param>
/// <returns></returns>
[HttpGet("eventType/{eventType}")]
public IActionResult GetEventByEventType(string eventType, string startTime, string endTime, string site = "ALL", string status = "ALL", string time = "ALL", string speed = "ALL", string method = "0")
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var id = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = new Response();
startTime = startTime.Replace("undefined", "00:00:00");
endTime = endTime.Replace("undefined", "23:59:59");
//if (startTime == endTime || (startTime.Length == 10 && endTime.Length == 10))
//{
// startTime = startTime.Substring(0, 10) + " 00:00:00";
// endTime = endTime.Substring(0, 10) + " 23:59:59";
//}
var events = _service.GetEventsByEventType(id, eventType, startTime, endTime, site, status, time, speed);
if (events == null)
{
var res = new TradeResultModel();
res.Success = false;
res.Message = "查詢筆數過多,請重新查詢。";
return BadRequest(res);
}
var eventTypeViewModel = _eventTypeService.GetEventTypeByEventType(eventType);
var eventName = string.Empty;
if (eventTypeViewModel != null)
{
eventName = eventTypeViewModel.EventName;
};
result.Result = events;
if (method == "1")
{
var url = _fileService.ExportEvent(
Convert.ToDateTime(startTime).ToString("yyMMdd"),
Convert.ToDateTime(endTime).ToString("yyMMdd"),
eventName, events);
return Ok(url);
}
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得今年累計處理案件數 (punished = 1)
/// </summary>
/// <returns></returns>
[HttpGet("count/thisYear")]
public IActionResult GetThisYearEventsCount()
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var account = new ParserForUserFromClaim(userIdentity).GetAccount();
var result = new Response
{
Result = _service.GetThisYearEventsCount(account)
};
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得本月新增案件數量
/// </summary>
/// <returns></returns>
[HttpGet("count/thisMonth")]
public IActionResult GetThisMonthEventsCount()
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var account = new ParserForUserFromClaim(userIdentity).GetAccount();
var result = new Response
{
Result = _service.GetThisMonthEventsCount(account)
};
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得處理進度 by 時間區間
/// </summary>
/// <returns></returns>
/// <remarks>時間格式 yyyy-MM-dd</remarks>
[HttpGet("count/interval")]
public IActionResult GetEventsby(string startDatetime, string endDatetime)
{
try
{
var result = new Response();
result.Result = _service.GetEventsCount(startDatetime, endDatetime);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得事件 by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public IActionResult GetEvent(int id)
{
try
{
var result = new Response();
result.Result = _service.GetEventById(id);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///// <summary>
///// 新增事件
///// </summary>
///// <param name="vm"></param>
///// <returns></returns>
//[HttpPost("")]
//public IActionResult InsertEvent(EventViewModel vm)
//{
// var result = _service.InsertEvent(vm);
// return result.Success ? Ok(result) : BadRequest(result);
//}
///// <summary>
///// 修改事件
///// </summary>
///// <param name="id">事件的流水號</param>
///// <param name="vm"></param>
///// <returns></returns>
//[HttpPut("{id}")]
//public IActionResult UpdateEvent(int id, EventViewModel vm)
//{
// var result = _service.UpdatEvent(id, vm);
// return result.Success ? Ok(result) : BadRequest(result);
//}
/// <summary>
/// 修改事件 成 CheckPunish (確認開單)
/// </summary>
/// <param name="eventId">事件的流水號</param>
/// <param name="law">違規法條</param>
/// <returns></returns>
[HttpPut("{eventId}/checkPunish")]
public IActionResult UpdatEventCheckPunish(int eventId, string law)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var account = new ParserForUserFromClaim(userIdentity).GetAccount();
var result = _service.UpdatEventCheckPunish(account, eventId, "1", law);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 批次確認開單
/// </summary>
/// <param name="eventId"></param>
/// <param name="law">違規法條</param>
/// <returns></returns>
[HttpPut("muti/checkPunish")]
public IActionResult UpdatEventCheckPunish(List<int> eventId, string law)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var account = new ParserForUserFromClaim(userIdentity).GetAccount();
var result = _service.UpdatEventCheckPunishList(account, eventId, "1", law);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///// <summary>
///// 修改事件 取消開單
///// </summary>
///// <param name="id">事件的流水號</param>
///// <returns></returns>
//[HttpPut("{id}/checkPunishRollback")]
//public IActionResult UpdatEventCheckPunishRollback(int id)
//{
// var userIdentity = (ClaimsIdentity)User.Identity;
// var account = new ParserForUserFromClaim(userIdentity).GetAccount();
// var result = _service.UpdatEventCheckPunish(account, id, "0");
// return result.Success ? Ok(result) : BadRequest(result);
//}
/// <summary>
/// 修改事件 成 不開單
/// </summary>
/// <param name="eventId">事件的流水號</param>
/// <param name="reason"></param>
/// <returns></returns>
[HttpPut("{eventId}/nopunish")]
public IActionResult UpdatEventNoPunish(int eventId, string reason)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var account = new ParserForUserFromClaim(userIdentity).GetAccount();
var result = _service.UpdatEventNoPunish(account, eventId, reason);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取消開單
/// </summary>
/// <param name="eventId">事件的流水號</param>
/// <returns></returns>
[HttpPut("{eventId}/cancel")]
public IActionResult UpdatEventCancel(int eventId)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var account = new ParserForUserFromClaim(userIdentity).GetAccount();
var result = _service.UpdatEventCancel(account, eventId);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///// <summary>
///// 刪除事件
///// </summary>
///// <param name="id">事件的流水號</param>
///// <returns></returns>
//[HttpDelete("{id}")]
//public IActionResult DeleteEvent(int id)
//{
// var result = _service.DeleteEvent(id);
// return result.Success ? Ok(result) : BadRequest(result);
//}
/// <summary>
/// 修改事件的車子類型
/// </summary>
/// <param name="eventId">事件的流水號</param>
/// <param name="vehicleType"></param>
/// <returns></returns>
[HttpPut("{eventId}/vehicleType")]
public IActionResult UpdateEventVehicleType(int eventId, string vehicleType)
{
try
{
var result = _service.UpdatEventVehicleType(eventId, vehicleType);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 修改事件的車牌號碼
/// </summary>
/// <param name="eventId">事件的流水號</param>
/// <param name="vehicleNumber"></param>
/// <returns></returns>
[HttpPut("{eventId}/vehicleNumber")]
public IActionResult UpdateEventVehicleNumber(int eventId, string vehicleNumber)
{
try
{
var result = _service.UpdatEventVehicleNumber(eventId, vehicleNumber);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 修改事件的車牌號碼和車子類型
/// </summary>
/// <param name="eventId">事件的流水號</param>
/// <param name="vehicleType"></param>
/// <param name="vehicleNumber"></param>
/// <returns></returns>
[HttpPut("{eventId}/vehicle")]
public IActionResult UpdateEventVehicle(int eventId, string vehicleType, string vehicleNumber)
{
try
{
var result = _service.UpdatEventVehicle(eventId, vehicleType, vehicleNumber);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpGet("vehicle/all")]
public IActionResult GetVehicleTypes()
{
try
{
var result = new Response();
result.Result = _service.GetVehicleTypes();
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
}
}