using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using AMESCoreStudio.WebApi; using AMESCoreStudio.WebApi.Models.AMES; using AMESCoreStudio.CommonTools.Result; using AMESCoreStudio.WebApi.DTO.AMES; namespace AMESCoreStudio.WebApi.Controllers.AMES { /// <summary> /// 設備狀態紀錄維護 /// </summary> [Route("api/[controller]")] [ApiController] public class OutfitStatusLogsController : ControllerBase { private readonly AMESContext _context; /// <summary> /// 設備狀態紀錄維護 /// </summary> /// <param name="context"></param> public OutfitStatusLogsController(AMESContext context) { _context = context; } /// <summary> /// /// </summary> /// <returns></returns> // GET: api/OutfitStatusLogs [HttpGet] public async Task<ActionResult<IEnumerable<OutfitStatusLog>>> GetOutfitStatusLog() { IQueryable<OutfitStatusLog> q = _context.OutfitStatusLogs; q = q.OrderBy(p => p.OutfitLogID); var OutfitStatusLog = await q.ToListAsync(); return OutfitStatusLog; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/OutfitStatusLogs/5 [HttpGet("{id}")] public async Task<ActionResult<IEnumerable<OutfitStatusLog>>> GetOutfitStatusLog(int id) { IQueryable<OutfitStatusLog> q = _context.OutfitStatusLogs; q = q.Where(p => p.OutfitLogID.Equals(id)); var OutfitStatusLog = await q.ToListAsync(); if (OutfitStatusLog == null) { return NotFound(); } return OutfitStatusLog; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/OutfitStatusLogs/5 [HttpGet("Outfit/{id}")] public async Task<ActionResult<IEnumerable<OutfitStatusLog>>> GetOutfitStatusLogByOutfitID(int id) { IQueryable<OutfitStatusLog> q = _context.OutfitStatusLogs; q = q.Where(p => p.OutfitID.Equals(id)); var OutfitStatusLog = await q.ToListAsync(); if (OutfitStatusLog == null) { return NotFound(); } return OutfitStatusLog; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/OutfitStatusLogs/5 [HttpGet("OutfitDto/{id}")] public async Task<ActionResult<IEnumerable<OutfitStatusLogDto>>> GetOutfitStatusLogDtoByOutfitID(int id) { IQueryable<OutfitStatusLog> q = _context.OutfitStatusLogs; q = q.Where(p => p.OutfitID.Equals(id)); var OutfitStatusLog = await q.ToListAsync(); if (OutfitStatusLog == null) { return NotFound(); } var outfitStatusLogDtos = await q.Select(s => new OutfitStatusLogDto { OutfitID = _context.OutfitInfoes.Where(p1 => p1.OutfitID.Equals(s.OutfitID)).FirstOrDefault().QANo, OutfitLogID = s.OutfitLogID, CreateUserID = _context.UserInfoes.Where(p1 => p1.UserID.Equals(s.CreateUserID)).FirstOrDefault().UserName, ConfirmUserID = _context.UserInfoes.Where(p1 => p1.UserID.Equals(s.ConfirmUserID)).FirstOrDefault().UserName, StatusUserID = _context.UserInfoes.Where(p1 => p1.UserID.Equals(s.StatusUserID)).FirstOrDefault().UserName, StatusDate = s.StatusDate.ToString("yyyy-MM-dd"), Duty = s.Duty, WipNo = s.WipNo, StatusNo = s.StatusNo, Remark = s.Remark, CreateDate = s.CreateDate.ToString("yyyy-MM-dd") }).ToListAsync(); return outfitStatusLogDtos; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="OutfitStatusLog"></param> /// <returns></returns> // PUT: api/OutfitStatusLogs/5 // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPut("{id}")] public async Task<ResultModel<OutfitStatusLog>> PutOutfitStatusLog(int id, [FromBody] OutfitStatusLog OutfitStatusLog) { ResultModel<OutfitStatusLog> result = new ResultModel<OutfitStatusLog>(); if (id != OutfitStatusLog.OutfitLogID) { result.Success = false; result.Msg = "序號錯誤"; return result; } _context.Entry(OutfitStatusLog).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception ex) { result.Success = false; result.Msg = ex.Message; return result; } result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// 新增资料 /// </summary> /// <param name="OutfitStatusLog"></param> /// <returns></returns> // POST: api/OutfitStatusLogs // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPost] public async Task<ResultModel<OutfitStatusLog>> PostOutfitStatusLog(OutfitStatusLog OutfitStatusLog) { ResultModel<OutfitStatusLog> result = new ResultModel<OutfitStatusLog>(); Helper helper = new Helper(_context); OutfitStatusLog.OutfitLogID = helper.GetIDKey("OUTFITLOG_ID").Result; _context.OutfitStatusLogs.Add(OutfitStatusLog); try { await _context.SaveChangesAsync(); } catch (Exception ex) { result.Success = false; result.Msg = ex.Message; return result; } result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // DELETE: api/OutfitStatusLogs/5 [HttpDelete("{id}")] public async Task<ResultModel<OutfitStatusLog>> DeleteOutfitStatusLog(int id) { ResultModel<OutfitStatusLog> result = new ResultModel<OutfitStatusLog>(); var OutfitStatusLog = await _context.OutfitStatusLogs.Where(p => p.OutfitLogID == id).FirstOrDefaultAsync(); if (OutfitStatusLog == null) { result.Success = false; result.Msg = "序號錯誤"; return result; } //_context.OutfitStatusLogs.Remove(OutfitStatusLog); OutfitStatusLog newOutfitStatusLog = new OutfitStatusLog(); newOutfitStatusLog = OutfitStatusLog; if (OutfitStatusLog.StatusNo == "A") newOutfitStatusLog.StatusNo = "S"; else newOutfitStatusLog.StatusNo = "A"; _context.Entry(newOutfitStatusLog).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception ex) { result.Success = false; result.Msg = ex.Message; return result; } result.Success = true; result.Msg = "OK"; return result; } private bool OutfitStatusLogExists(int id) { return _context.OutfitStatusLogs.Any(e => e.OutfitLogID == id); } } }