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 { /// /// 事件 /// [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; } /// /// 取得事件列表 Term = 車牌號碼(Number) /// /// /// /// Term = 車牌號碼(Number) [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(); } } ///// ///// 取得某站點的事件列表 ///// ///// ///// ///// ///// Term = 車牌號碼(Number) //[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); //} /// /// 取得開單畫面的事件 /// /// 違規項目 /// 起始時間 /// 結束時間 /// 站點 /// 狀態 /// 違停時間 /// 速度 /// 0 : 查詢, 1:匯出 /// [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(); } } /// /// 取得今年累計處理案件數 (punished = 1) /// /// [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(); } } /// /// 取得本月新增案件數量 /// /// [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(); } } /// /// 取得處理進度 by 時間區間 /// /// /// 時間格式 yyyy-MM-dd [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(); } } /// /// 取得事件 by id /// /// /// [HttpGet("{id}")] public IActionResult GetEvent(int id) { try { var result = new Response(); result.Result = _service.GetEventById(id); return Ok(result); } catch (Exception) { return BadRequest(); } } ///// ///// 新增事件 ///// ///// ///// //[HttpPost("")] //public IActionResult InsertEvent(EventViewModel vm) //{ // var result = _service.InsertEvent(vm); // return result.Success ? Ok(result) : BadRequest(result); //} ///// ///// 修改事件 ///// ///// 事件的流水號 ///// ///// //[HttpPut("{id}")] //public IActionResult UpdateEvent(int id, EventViewModel vm) //{ // var result = _service.UpdatEvent(id, vm); // return result.Success ? Ok(result) : BadRequest(result); //} /// /// 修改事件 成 CheckPunish (確認開單) /// /// 事件的流水號 /// 違規法條 /// [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(); } } /// /// 批次確認開單 /// /// /// 違規法條 /// [HttpPut("muti/checkPunish")] public IActionResult UpdatEventCheckPunish(List 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(); } } ///// ///// 修改事件 取消開單 ///// ///// 事件的流水號 ///// //[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); //} /// /// 修改事件 成 不開單 /// /// 事件的流水號 /// /// [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(); } } /// /// 取消開單 /// /// 事件的流水號 /// [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(); } } ///// ///// 刪除事件 ///// ///// 事件的流水號 ///// //[HttpDelete("{id}")] //public IActionResult DeleteEvent(int id) //{ // var result = _service.DeleteEvent(id); // return result.Success ? Ok(result) : BadRequest(result); //} /// /// 修改事件的車子類型 /// /// 事件的流水號 /// /// [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(); } } /// /// 修改事件的車牌號碼 /// /// 事件的流水號 /// /// [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(); } } /// /// 修改事件的車牌號碼和車子類型 /// /// 事件的流水號 /// /// /// [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(); } } } }