234 lines
7.7 KiB

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
{
/// <summary>
/// 工作群組維護
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class StandardWorkTimeReworksController : ControllerBase
{
private readonly AMESContext _context;
/// <summary>
/// 重工標準工時
/// </summary>
/// <param name="context"></param>
public StandardWorkTimeReworksController(AMESContext context)
{
_context = context;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
// GET: api/StandardWorkTimeReworks
[HttpGet]
public async Task<ActionResult<IEnumerable<StandardWorkTimeRework>>> GetStandardWorkTimeRework()
{
IQueryable<StandardWorkTimeRework> q = _context.StandardWorkTimeReworks;
q = q.OrderBy(p => p.ReworkStandardID);
var standardWorkTimeRework = await q.ToListAsync();
return standardWorkTimeRework;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/StandardWorkTimeReworks/5
[HttpGet("{id}")]
public async Task<ActionResult<IEnumerable<StandardWorkTimeRework>>> GetStandardWorkTimeRework(int id)
{
IQueryable<StandardWorkTimeRework> q = _context.StandardWorkTimeReworks;
q = q.Where(p => p.ReworkStandardID.Equals(id));
var standardWorkTimeRework = await q.ToListAsync();
if (standardWorkTimeRework == null)
{
return NotFound();
}
return standardWorkTimeRework;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/StandardWorkTimeReworks/5
[HttpGet("Query/{wipno}/{sid}")]
public async Task<ActionResult<IEnumerable<StandardWorkTimeRework>>> GetStandardWorkTimeReworkByQuery(string wipno,int sid)
{
IQueryable<StandardWorkTimeRework> 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;
}
/// <summary>
/// 修改重工標準工時
/// </summary>
/// <param name="id"></param>
/// <param name="standardWorkTimeRework"></param>
/// <returns></returns>
// 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<ResultModel<StandardWorkTimeRework>> PutStandardWorkTimeRework(int id, [FromBody] StandardWorkTimeRework standardWorkTimeRework)
{
ResultModel<StandardWorkTimeRework> result = new ResultModel<StandardWorkTimeRework>();
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;
}
/// <summary>
/// 新增资料
/// </summary>
/// <param name="standardWorkTimeRework"></param>
/// <returns></returns>
// 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<ResultModel<StandardWorkTimeRework>> PostStandardWorkTimeRework(StandardWorkTimeRework standardWorkTimeRework)
{
ResultModel<StandardWorkTimeRework> result = new ResultModel<StandardWorkTimeRework>();
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);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/StandardWorkTimeReworks/5
[HttpDelete("{id}")]
public async Task<ResultModel<StandardWorkTimeRework>> DeleteStandardWorkTimeRework(int id)
{
ResultModel<StandardWorkTimeRework> result = new ResultModel<StandardWorkTimeRework>();
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);
}
}
}