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; namespace AMESCoreStudio.WebApi.Controllers.AMES { /// /// 工作群組維護 /// [Route("api/[controller]")] [ApiController] public class StandardWorkTimeReworksController : ControllerBase { private readonly AMESContext _context; /// /// 重工標準工時 /// /// public StandardWorkTimeReworksController(AMESContext context) { _context = context; } /// /// /// /// // GET: api/StandardWorkTimeReworks [HttpGet] public async Task>> GetStandardWorkTimeRework() { IQueryable q = _context.StandardWorkTimeReworks; q = q.OrderBy(p => p.ReworkStandardID); var standardWorkTimeRework = await q.ToListAsync(); return standardWorkTimeRework; } /// /// /// /// /// // GET: api/StandardWorkTimeReworks/5 [HttpGet("{id}")] public async Task>> GetStandardWorkTimeRework(int id) { IQueryable q = _context.StandardWorkTimeReworks; q = q.Where(p => p.ReworkStandardID.Equals(id)); var standardWorkTimeRework = await q.ToListAsync(); if (standardWorkTimeRework == null) { return NotFound(); } return standardWorkTimeRework; } /// /// /// /// /// // GET: api/StandardWorkTimeReworks/5 [HttpGet("Query/{wipno}/{sid}")] public async Task>> GetStandardWorkTimeReworkByQuery(string wipno,int sid) { IQueryable q = _context.StandardWorkTimeReworks; if(wipno!="*") q = q.Where(p => p.WipNo.Contains(wipno)&& p.StationID.Equals(sid)); else q = q.Where(p => p.StationID.Equals(sid)); var standardWorkTimeRework = await q.ToListAsync(); if (standardWorkTimeRework == null) { return NotFound(); } return standardWorkTimeRework; } /// /// 修改重工標準工時 /// /// /// /// // PUT: api/StandardWorkTimeReworks/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> PutStandardWorkTimeRework(int id, [FromBody] StandardWorkTimeRework standardWorkTimeRework) { ResultModel result = new ResultModel(); if (id != standardWorkTimeRework.ReworkStandardID) { result.Success = false; result.Msg = "序號錯誤"; return result; } _context.Entry(standardWorkTimeRework).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StandardWorkTimeExists(id)) { result.Success = false; result.Msg = "序號不存在"; return result; } else { throw; } } result.Success = true; result.Msg = "OK"; return result; //if (id != standardWorkTimeRework.ReworkStandardID) //{ // return BadRequest(); //} //_context.Entry(standardWorkTimeRework).State = EntityState.Modified; //try //{ // await _context.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!StandardWorkTimeExists(id)) // { // return NotFound(); // } // else // { // throw; // } //} //return standardWorkTimeRework; } /// /// 新增资料 /// /// /// // POST: api/StandardWorkTimeReworks // 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> PostStandardWorkTimeRework(StandardWorkTimeRework standardWorkTimeRework) { ResultModel result = new ResultModel(); Helper helper = new Helper(_context); standardWorkTimeRework.ReworkStandardID = helper.GetIDKey("RE_WORKTIME_ID").Result; _context.StandardWorkTimeReworks.Add(standardWorkTimeRework); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; return result; //return CreatedAtAction("GetStandardWorkTime", new { id = standardWorkTimeRework.ReworkStandardID }, standardWorkTimeRework); } /// /// /// /// /// // DELETE: api/StandardWorkTimeReworks/5 [HttpDelete("{id}")] public async Task> DeleteStandardWorkTimeRework(int id) { ResultModel result = new ResultModel(); var standardWorkTimeRework = await _context.StandardWorkTimeReworks.FindAsync(id); if (standardWorkTimeRework == null) { result.Success = false; result.Msg = "序號不存在"; return result; } _context.StandardWorkTimeReworks.Remove(standardWorkTimeRework); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; return result; //var standardWorkTimeRework = await _context.StandardWorkTimeReworks.Where(p => p.ReworkStandardID == id).FirstOrDefaultAsync(); //if (standardWorkTimeRework == null) //{ // return NotFound(); //} //_context.StandardWorkTimeReworks.Remove(standardWorkTimeRework); //await _context.SaveChangesAsync(); //return standardWorkTimeRework; } private bool StandardWorkTimeExists(int id) { return _context.StandardWorkTimeReworks.Any(e => e.ReworkStandardID == id); } } }