using LiangLiSystem.Services.Helpers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Omu.ValueInjecter; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Traffic.Data.Models; using Traffic.Data.ViewModels; using Traffic.Repository.Infrastructures; using Traffic.Service.Interfaces; namespace Traffic.Service.Implements { public class EventService : IEventService { private readonly IConfiguration _configuration; private readonly IUnitOfWork _unitOfWork; private readonly ILogger _logger; public EventService(IConfiguration configuration, IUnitOfWork unitOfWork, ILogger logger) { _configuration = configuration; _unitOfWork = unitOfWork; _logger = logger; } public PageViewModel> GetEventsByCar(SearchModel searchModel) { try { var number = $"%{searchModel.Term}%"; var datas = _unitOfWork.EventRepository.GetEventsByCar(number); var sites = _unitOfWork.SiteInformationRepository.GetSiteInformations(); var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes(); var count = datas.Count(); int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize; switch (searchModel.Order.ToLower()) { case "id": default: datas = searchModel.IsAsc ? datas.OrderBy(x => x.Id) : datas.OrderByDescending(x => x.Id); break; case "number": datas = searchModel.IsAsc ? datas.OrderBy(x => x.Number).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.Number).ThenByDescending(x => x.Id); break; case "siteid": datas = searchModel.IsAsc ? datas.OrderBy(x => x.SiteID).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.SiteID).ThenByDescending(x => x.Id); break; case "eventtype": datas = searchModel.IsAsc ? datas.OrderBy(x => x.EventType).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.EventType).ThenByDescending(x => x.Id); break; case "intime": datas = searchModel.IsAsc ? datas.OrderBy(x => Convert.ToDateTime(x.InTime)).ThenBy(x => x.Id) : datas.OrderByDescending(x => Convert.ToDateTime(x.InTime)).ThenByDescending(x => x.Id); break; } datas = datas.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize); var result = new List(); foreach (var data in datas) { var vm = new EventSearchByNumberViewModel(); vm.Id = data.Id; vm.Number = data.Number; vm.InTime = Convert.ToDateTime(data.InTime).ToString("yyyy-MM-dd HH:mm:ss"); vm.SiteName = sites.FirstOrDefault(i => i.SiteID == data.SiteID).SiteName; vm.EventName = eventTypes.FirstOrDefault(i => i.EventType == sites.FirstOrDefault(i => i.SiteID == data.SiteID).EventType).EventName; if (data.Punished == "1") { vm.Status = "已開單"; } else if (data.Rejected == "1") { vm.Status = "不開單"; } else if (data.CheckPunish == "1") { vm.Status = "開單處理中"; } else { vm.Status = "未處理"; } result.Add(vm); } if (searchModel.Order.ToLower() == "status") { result = searchModel.IsAsc ? result.OrderBy(x => x.Status).ThenBy(x => x.Id).ToList() : result.OrderByDescending(x => x.Status).ThenByDescending(x => x.Id).ToList(); } return new PageViewModel> { RowCount = count, PageCount = pageCount, CurrentPage = searchModel.Page, Data = result, }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new PageViewModel>(); } } public IEnumerable GetEventsByEventType(int userId, string eventType, string startTime, string endTime, string site, string status, string time, string speed) { try { _logger.LogInformation("GetEventsByEventType Init"); _logger.LogInformation($"GetEventsByEventType userId ={userId}"); _logger.LogInformation($"GetEventsByEventType eventType ={eventType}"); _logger.LogInformation($"GetEventsByEventType startTime ={startTime}"); _logger.LogInformation($"GetEventsByEventType endTime ={endTime}"); _logger.LogInformation($"GetEventsByEventType site ={site}"); var punishCount = Convert.ToInt16(_configuration["EventStatus:Count"]); var dataCount = _unitOfWork.EventRepository.GetEventByTimeCount(startTime, endTime); if (dataCount >= punishCount) { return null; } //異常車牌 var data = _unitOfWork.EventRepository.GetEventByTime(startTime, endTime); var userSite = _unitOfWork.AccountRepository.GetAccountUserSite(userId); var sites = new List(); foreach (var item in userSite) { var sitedata = _unitOfWork.SiteInformationRepository.GetSiteInformationById(item.SiteId); sites.Add(sitedata); } sites = sites.Where(i => i.EventType == eventType).ToList(); data = data.Where(i => sites.Any(j => j.SiteID == i.SiteID)); _logger.LogInformation($"GetEventJoinSiteAndUsers Data.count = {data.Count()}"); if (eventType == "1") { data = data.Where(i => Convert.ToDateTime(i.InTime).Hour >= 6); } _logger.LogInformation($"GetEventJoinSiteAndUsers Convert.ToDateTime(i.InTime).Hour >= 6 Data.count = {data.Count()}"); var exceptionNumbers = _unitOfWork.EventRepository.GetExceptionNumbers(); data = data.Where(i => ShowThisEvent(exceptionNumbers, i) == true); if (site != "ALL") { data = data.Where(i => i.SiteID == site); } if (time != "ALL") { if (time == "1") { data = data.Where(i => (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).TotalSeconds >= 70 && (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).TotalSeconds < 190); } else if (time == "3") { data = data.Where(i => (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).TotalSeconds >= 190); } } if (speed != "ALL") { data = data.Where(i => Convert.ToDouble(i.Speed) >= Convert.ToDouble(speed)); } if (status == "未開單") { data = data.Where(i => i.Punished == "0" && i.Rejected == "0" && i.CheckPunish == "0" && string.IsNullOrWhiteSpace(i.Ps)); } else if (status == "已退回") { data = data.Where(i => i.Rejected == "1"); } else if (status == "裁決中") { data = data.Where(i => i.CheckPunish == "1"); } else if (status == "已開罰") { data = data.Where(i => i.Punished == "1"); } else if (status == "不開單") { data = data.Where(i => i.Punished == "0" && i.Rejected == "0" && i.CheckPunish == "0" && !string.IsNullOrWhiteSpace(i.Ps)); } _logger.LogInformation($"GetEventJoinSiteAndUsers All condition Data.count = {data.Count()}"); var cars = _unitOfWork.EventRepository.GetVehicleType(); var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes(); var result = new List(); foreach (var x in data.ToList()) { var sitedata = sites.FirstOrDefault(i => i.SiteID == x.SiteID); var vm = new JoinEventSiteAndUserViewModel { Id = x.Id, UserID = userId.ToString(), SiteId = x.SiteID, SiteName = sitedata.SiteName, Account = x.Account, Area = sitedata.Area, County = sitedata.County, EventName = eventTypes.Any(i => i.EventType == x.EventType) ? eventTypes.FirstOrDefault(i => i.EventType == x.EventType).EventName : "", CheckPunish = x.CheckPunish, HandlingTime = x.HandlingTime, Laws = GetLawsCodes(sitedata.Laws), UploadTime = x.UploadTime, VehicleName = cars.Any(i => i.VehicleType == x.VehicleType) ? cars.FirstOrDefault(i => i.VehicleType == x.VehicleType).VehicleName : "", Ps = x.Ps, InTime = x.InTime, OutTime = x.OutTime, Punished = x.Punished, Rejected = x.Rejected, IMGPath1 = x.IMGPath1.Replace("carviolation.tycg.gov.tw:8443", "172.16.1.49:8443"), //IMGPath2 = x.IMGPath2, //IMGPath3 = x.IMGPath3, //IMGPath4 = x.IMGPath4, //IMGPath5 = x.IMGPath5, //IMGPath6 = x.IMGPath6, VideoPath1 = x.VideoPath1.Replace("carviolation.tycg.gov.tw:8443", "172.16.1.49:8443"), //VideoPath2 = x.VideoPath2, IMGList = GetFiles(x.IMGPath1.Replace("carviolation.tycg.gov.tw:8443", "172.16.1.49:8443"), _configuration["EventStatus:ImagePath"], x.SiteID, x.EventID), VideoList = GetFiles(x.VideoPath1.Replace("carviolation.tycg.gov.tw:8443", "172.16.1.49:8443"), _configuration["EventStatus:VideoPath"], x.SiteID, x.EventID), Number = x.Number, AlarmTime = x.AlarmTime, Channel = x.Channel, CompanyCode = x.CompanyCode, Distance = x.Distance, EventID = x.EventID, EventType = x.EventType, IsStay = x.IsStay, LimitTon = x.LimitTon, LPRTime = x.LPRTime, //PlateNumber = x.PlateNumber, Slot = x.Slot, Speed = x.Speed, Ton = x.Ton, TotalPassTime = x.TotalPassTime, Unsure = x.Unsure, VehicleType = x.VehicleType, Status = GetStatus(x) }; result.Add(vm); } return result; } catch (Exception ex) { _logger.LogError(ex.Message); return new List(); } } private List GetLawsCodes(string law) { try { if (string.IsNullOrWhiteSpace(law)) { return null; } var codes = new List(); foreach (var item in law.Split(',')) { var key = new LawsCodes(); key.LawsCode = item; codes.Add(key); } return codes; } catch (Exception ex) { _logger.LogError(ex.Message); return null; } } private static bool ShowThisEvent(IEnumerable exceptionNumber, Event joinEventSiteAndUser) { var date = Convert.ToDateTime(joinEventSiteAndUser.InTime).ToString("yyyy-MM-dd"); var result = exceptionNumber.Where(i => i.Number == joinEventSiteAndUser.Number && date == i.Date); if (result.Any()) { return false; } else { return true; } } public List GetFiles(string url, string path, string siteID, string eventID) { try { if (_configuration["Mode:IsTest"] == "True") { var result = new List { url + "/event1.jpg", url + "/event2.jpg", url + "/event3.jpg", url + "/event4.jpg", url + "/event5.jpg", url + "/event6.jpg" }; return result; } else { //取得圖片的路徑 var folder = new DirectoryInfo($"{path}/{siteID}/{eventID}"); //取得圖片路徑底下有多少 Image var files = folder.GetFiles(); var result = new List(); foreach (var item in files) { result.Add(url + "/" + item.Name); } return result; } } catch (Exception ex) { _logger.LogError(ex.Message); return null; } } public PageViewModel> GetEventByEventType(int userId, string eventType, SearchModel searchModel) { throw new NotImplementedException(); } private static string GetStatus(Event data) { if (data.Punished == "1") { return "已開罰"; } else if (data.Rejected == "1") { return "已退回"; } else if (data.CheckPunish == "1") { return "裁決中"; } else if (data.Punished == "0" && data.Rejected == "0" && data.CheckPunish == "0" && !string.IsNullOrWhiteSpace(data.Ps)) { return "不開單"; } else { return "未開單"; } } public PageViewModel> GetEventBySites(string siteId, SearchModel searchModel) { try { var count = _unitOfWork.EventRepository.GetEventBySitesCount(siteId, searchModel.Term); var datas = _unitOfWork.EventRepository.GetEventBySites(siteId, searchModel.Page, searchModel.PageSize); var cars = _unitOfWork.EventRepository.GetVehicleType(); int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize; switch (searchModel.Order.ToLower()) { case "id": default: datas = searchModel.IsAsc ? datas.OrderBy(x => x.Id) : datas.OrderByDescending(x => x.Id); break; case "siteid": datas = searchModel.IsAsc ? datas.OrderBy(x => x.SiteID).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.SiteID).ThenByDescending(x => x.Id); break; } datas = datas.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize); var result = new List(); foreach (var data in datas) { var vm = new EventViewModel(); vm.InjectFrom(data); vm.Id = data.Id; vm.VehicleName = cars.FirstOrDefault(x => x.VehicleType == vm.VehicleType).VehicleName; result.Add(vm); } return new PageViewModel> { RowCount = count, PageCount = pageCount, CurrentPage = searchModel.Page, Data = result, }; } catch (Exception ex) { _logger.LogError(ex.Message); return new PageViewModel>(); } } public EventViewModel GetEventById(int id) { try { var data = _unitOfWork.EventRepository.GetEventById(id); var cars = _unitOfWork.EventRepository.GetVehicleTypebyType(data.VehicleType); if (data != null) { var result = new EventViewModel(); result.InjectFrom(data); result.Id = data.Id; result.VehicleName = cars.VehicleName; return result; } else { return null; } } catch (Exception ex) { _logger.LogError(ex.Message); return null; } } public int GetThisYearEventsCount(string account) { try { var thisYearStart = $"{DateTime.Now.Year}-01-01 00:00:00"; var thisYearEnd = $"{DateTime.Now.Year}-12-31 23:59:59"; var count = _unitOfWork.EventRepository.GetThisYearEventsCount(thisYearStart, thisYearEnd, account); return count; } catch (Exception ex) { _logger.LogError(ex.Message); return 0; } } public int GetThisMonthEventsCount(string account) { try { var thisMonthStart = $"{DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToString("yyyy-MM-dd")} 00:00:00"; var thisMonthEnd = $"{DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.AddMonths(1).Day).ToString("yyyy-MM-dd")} 23:59:59"; var count = _unitOfWork.EventRepository.GetThisMonthEventsCount(thisMonthStart, thisMonthEnd, account); return count; } catch (Exception ex) { _logger.LogError(ex.Message); return 0; } } public EventCountViewModel GetEventsCount(string startDatetime, string endDatetime) { try { var datas = _unitOfWork.EventRepository.GetEvents(startDatetime, endDatetime); var TotalCount = datas.Count(); var PunishedCount = datas.Where(i => i.Punished == "1").Count(); var CheckPunishedCount = datas.Where(i => i.CheckPunish == "1").Count(); var IsStayCount = datas.Where(i => i.IsStay == "1").Count(); var UnsureCount = datas.Where(i => i.Unsure == "1").Count(); var RejectedCount = datas.Where(i => i.Rejected == "1").Count(); var NoStatusCount = datas. Where(i => i.Rejected == "0" && i.Unsure == "0" && i.IsStay == "0" && i.CheckPunish == "0" && i.Punished == "0").Count(); var NoPunishedCount = datas.Where(i => i.Rejected == "0" && i.Unsure == "0" && i.IsStay == "0" && i.CheckPunish == "0" && i.Punished == "0" && !string.IsNullOrWhiteSpace(i.Ps)).Count(); var result = new EventCountViewModel() { TotalCount = TotalCount, CheckPunishedCount = CheckPunishedCount, IsStayCount = IsStayCount, PunishedCount = PunishedCount, UnsureCount = UnsureCount, RejectedCount = RejectedCount, NoStatusCount = NoStatusCount, NoPunishedCount = NoPunishedCount }; return result; } catch (Exception ex) { _logger.LogError(ex.Message); return new EventCountViewModel(); } } public TradeResultModel InsertEvent(EventViewModel viewModel) { try { var data = new Event(); data.InjectFrom(viewModel); var result = _unitOfWork.EventRepository.InsertEvent(data); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "新增成功" : "新增失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEvent(int id, EventViewModel viewModel) { try { var data = new Event(); data.InjectFrom(viewModel); data.Id = id; var result = _unitOfWork.EventRepository.UpdateEvent(data); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEventCheckPunish(string account, int eventId, string checkPunish, string law) { try { var data = _unitOfWork.EventRepository.GetEventById(eventId); if (data == null) { return new TradeResultModel { Success = false, Message = "查無此案件" }; } if (data.Rejected == "1") { return new TradeResultModel { Success = false, Message = "此案件的狀態為不開罰" }; } if (data.Punished == "1") { return new TradeResultModel { Success = false, Message = "此案件的狀態為已開罰" }; } if (data.CheckPunish == "1") { if (checkPunish == "1") { return new TradeResultModel { Success = false, Message = "此案件的狀態為等待開罰中" }; } } if (checkPunish == "0") { account = string.Empty; } var site = _unitOfWork.SiteInformationRepository.GetSiteInformationBySiteId(data.SiteID); var eventName = _unitOfWork.EventTypeRepository.GetEventTypeByEventType(site.EventType).EventName; var vehicleName = _unitOfWork.EventRepository.GetVehicleTypebyType(data.VehicleType).VehicleName; var user = _unitOfWork.AccountRepository.GetAccountUserByAccount(account); var organization = _unitOfWork.PoliceStationRepository.GetPoliceStationById(user.PoliceStationId).StationName; //取得圖片的路徑 var imageFolder = new DirectoryInfo($"{_configuration["EventStatus:ImagePath"]}/{site.SiteID}/{data.EventID}"); //取得圖片路徑底下有多少 Image var imageFiles = imageFolder.GetFiles(); //建立一個此事件的資料夾在Upload的資料夾裡 var folder = $"{site.SiteID}_{data.EventID}_{data.Number}"; DirectoryInfo uploadFolder = Directory.CreateDirectory(_configuration["EventStatus:Upload"] + folder); //將Image路徑的資料複製到Upload的資料夾 var images = new List(); foreach (var file in imageFiles) { images.Add(file.Name); File.Copy(imageFolder.FullName + "\\" + file.Name, uploadFolder.FullName + "\\" + file.Name, true); } //各個EventType分別產生不同資料夾 switch (site.EventType) { case "1": var json = new JsonCheckPunishEventType1 { area = site.Area, county = site.County, eventID = data.EventID, siteName = site.SiteName, alarmTime = Util.GetDateTimeNow(data.AlarmTime), eventName = eventName, inTime = Util.GetDateTimeNow(data.InTime), handlingTime = Util.GetDateTimeNow(data.HandlingTime), sn = data.Id.ToString(), plate = data.Number, outTime = Util.GetDateTimeNow(data.OutTime), vehicleName = vehicleName, organization = organization, policeID = user.UserName, punishID = law, images = images, }; CreateCheckPunishEventTypeJson(_configuration["EventStatus:Upload"], folder, data.SiteID, data.EventID, json); break; case "2": var json2 = new JsonCheckPunishEventType2 { area = site.Area, county = site.County, eventID = data.EventID, siteName = site.SiteName, eventName = eventName, inTime = Util.GetDateTimeNow(data.InTime), handlingTime = Util.GetDateTimeNow(data.HandlingTime), sn = data.Id.ToString(), plate = data.Number, outTime = Util.GetDateTimeNow(data.OutTime), vehicleName = vehicleName, organization = organization, policeID = user.UserName, punishID = law, images = images, distance = data.Distance, speed = data.Speed, totalPassTime = data.TotalPassTime }; CreateCheckPunishEventTypeJson(_configuration["EventStatus:Upload"], folder, data.SiteID, data.EventID, json2); break; case "3": var json3 = new JsonCheckPunishEventType3 { area = site.Area, county = site.County, eventID = data.EventID, siteName = site.SiteName, alarmTime = Util.GetDateTimeNow(data.AlarmTime), eventName = eventName, handlingTime = Util.GetDateTimeNow(data.HandlingTime), sn = data.Id.ToString(), plate = data.Number, vehicleName = vehicleName, organization = organization, policeID = user.UserName, punishID = law, images = images, }; CreateCheckPunishEventTypeJson(_configuration["EventStatus:Upload"], folder, data.SiteID, data.EventID, json3); break; case "4": var json4 = new JsonCheckPunishEventType4 { area = site.Area, county = site.County, eventID = data.EventID, siteName = site.SiteName, alarmTime = Util.GetDateTimeNow(data.AlarmTime), eventName = eventName, handlingTime = Util.GetDateTimeNow(data.HandlingTime), sn = data.Id.ToString(), plate = data.Number, vehicleName = vehicleName, organization = organization, policeID = user.UserName, punishID = law, images = images, }; CreateCheckPunishEventTypeJson(_configuration["EventStatus:Upload"], folder, data.SiteID, data.EventID, json4); break; case "6": var json6 = new JsonCheckPunishEventType6 { area = site.Area, county = site.County, eventID = data.EventID, siteName = site.SiteName, alarmTime = Util.GetDateTimeNow(data.AlarmTime), eventName = eventName, handlingTime = Util.GetDateTimeNow(data.HandlingTime), sn = data.Id.ToString(), plate = data.Number, vehicleName = vehicleName, organization = organization, policeID = user.UserName, punishID = law, images = images, ton = data.Ton, }; CreateCheckPunishEventTypeJson(_configuration["EventStatus:Upload"], folder, data.SiteID, data.EventID, json6); break; } var result = _unitOfWork.EventRepository.UpdatEventCheckPunish(account, eventId, checkPunish, law); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEventCheckPunishList(string account, List eventIds, string checkPunish, string law) { try { bool IsSuccess = true; foreach (var eventId in eventIds) { var result = UpdatEventCheckPunish(account, eventId, checkPunish, law).Success; if (result == false) { IsSuccess = false; } } return new TradeResultModel { Success = IsSuccess, Message = IsSuccess ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEventNoPunish(string account, int eventId, string reason) { try { var data = _unitOfWork.EventRepository.GetEventById(eventId); if (data == null) { return new TradeResultModel { Success = false, Message = "查無此案件" }; } if (data.CheckPunish != "0") { return new TradeResultModel { Success = false, Message = "此案件的狀態為等待開罰中" }; } if (data.Rejected != "0") { return new TradeResultModel { Success = false, Message = "此案件的狀態已經為不開罰" }; } if (data.Punished != "0") { return new TradeResultModel { Success = false, Message = "此案件的狀態為已開罰" }; } var result = _unitOfWork.EventRepository.UpdatEventNoPunish(account, eventId, reason); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEventCancel(string account, int id) { try { var data = _unitOfWork.EventRepository.GetEventById(id); var site = _unitOfWork.SiteInformationRepository.GetSiteInformationBySiteId(data.SiteID); var eventType = _unitOfWork.EventTypeRepository.GetEventTypeByEventType(site.EventType); var vichle = _unitOfWork.EventRepository.GetVehicleTypebyType(data.VehicleType); if (data == null) { return new TradeResultModel { Success = false, Message = "查無此案件" }; } string siteId = data.SiteID; JsonCancelEvent jsonCancelEvent = new JsonCancelEvent { plate = data.Number, county = site.County, action = "取消開單", area = site.Area, siteName = site.SiteName, eventID = data.EventID, policeID = "取消開單測試", eventName = eventType.EventName, handlingTime = Convert.ToDateTime(data.HandlingTime).ToString("yyyy-MM-dd HH:mm:ss"), organization = site.Area, vehicleName = vichle.VehicleName, sn = data.Id.ToString(), }; CreateCancelJson(_configuration["EventStatus:Cancel"], siteId, jsonCancelEvent); return new TradeResultModel { Success = true, Message = "成功取消開單" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } private static void CreateCheckPunishEventTypeJson(string path, string folder, string siteId, string eventID, object jsonCheckPunishEvent) { string fileName = $"{siteId}_{eventID}.json"; path = path + folder + "\\" + fileName; string Text = JsonConvert.SerializeObject(jsonCheckPunishEvent); using (FileStream fs = File.Create(path)) { byte[] info = new UTF8Encoding(true).GetBytes(Text); fs.Write(info, 0, info.Length); } } private static void CreateCancelJson(string path, string siteId, JsonCancelEvent jsonCancelEvent) { string fileName = $"{siteId}_{jsonCancelEvent.eventID}_{jsonCancelEvent.plate}.json"; path = path + fileName; string Text = JsonConvert.SerializeObject(jsonCancelEvent); using (FileStream fs = File.Create(path)) { byte[] info = new UTF8Encoding(true).GetBytes(Text); fs.Write(info, 0, info.Length); } } public TradeResultModel UpdatEventVehicleType(int id, string vehicleType) { try { var result = _unitOfWork.EventRepository.UpdateEventVehicleType(id, vehicleType); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEventVehicleNumber(int id, string vehicleNumber) { try { var result = _unitOfWork.EventRepository.UpdateEventVehicleNumber(id, vehicleNumber); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel UpdatEventVehicle(int id, string vehicleType, string vehicleNumber) { try { var result = _unitOfWork.EventRepository.UpdateEventVehicle(id, vehicleType, vehicleNumber); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public TradeResultModel DeleteEvent(int id) { try { var result = _unitOfWork.EventRepository.DeleteEvent(id); _unitOfWork.Complete(); return new TradeResultModel { Success = result, Message = result ? "刪除成功" : "刪除失敗" }; } catch (Exception ex) { _unitOfWork.Rollback(); _logger.LogError(ex.Message); return new TradeResultModel() { Success = false, Message = ex.ToString() }; } } public IEnumerable GetVehicleTypes() { try { var datas = _unitOfWork.EventRepository.GetVehicleType(); var vms = new List(); foreach (var data in datas) { var vm = new VehicleTypeViewModel(); vm.InjectFrom(data); vms.Add(vm); } return vms; } catch (Exception ex) { _logger.LogError(ex.Message); return null; } } } }