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 WorkingUnitsController : ControllerBase
{
private readonly AMESContext _context;
///
///
///
///
public WorkingUnitsController(AMESContext context)
{
_context = context;
}
///
/// 获取全部報工生產單位別
///
///
[HttpGet]
public async Task> GetWorkingUnits(int page = 0, int limit = 10)
{
IQueryable q = _context.WorkingUnits;
ResultModel result = new ResultModel();
// 紀錄筆數
result.DataTotal = q.Count();
// Table 頁數
if (page > 0)
{
q = q.Skip((page - 1) * limit).Take(limit);
}
result.Data = await q.ToListAsync();
return result;
}
///
/// 获取全部報工生產單位別
///
///
[HttpGet("byALL")]
public async Task>> GetWorkingStationALLs()
{
IQueryable q = _context.WorkingUnits;
q = q.OrderBy(p => p.WorkingUnitID);
var WorkingUnits = await q.ToListAsync();
return WorkingUnits;
}
///
/// 用ID获取该報工生產單位別
///
///
///
[HttpGet("{id}")]
public async Task>> GetWorkingUnit(int id)
{
IQueryable q = _context.WorkingUnits;
q = q.Where(p => p.WorkingUnitID.Equals(id));
var WorkingUnit = await q.ToListAsync();
if (WorkingUnit == null)
{
return NotFound();
}
return WorkingUnit;
}
///
/// 更新條報工生產單位別
///
///
///
///
[HttpPut("{id}")]
public async Task> PutWorkingUnits(int id, [FromBody] WorkingUnit WorkingUnit)
{
ResultModel result = new ResultModel();
if (id != WorkingUnit.WorkingUnitID)
{
result.Success = false;
result.Msg = "序號錯誤";
return result;
}
WorkingUnit.UpdateDate = DateTime.Now;
_context.Entry(WorkingUnit).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
return result;
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
return result;
//throw;
}
}
///
/// 新增報工生產單位別
///
///
///
[HttpPost]
public async Task> PostWorkingUnits(WorkingUnit WorkingUnit)
{
ResultModel result = new ResultModel();
Helper helper = new Helper(_context);
WorkingUnit.WorkingUnitID = helper.GetIDKey("WorkingUnit_ID").Result;
WorkingUnit.CreateDate = DateTime.Now;
WorkingUnit.UpdateDate = DateTime.Now;
_context.WorkingUnits.Add(WorkingUnit);
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (DbUpdateException ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
///
/// 停用/啟用報工生產單位別
///
///
///
[HttpDelete("{id}")]
public async Task> DeleteWorkingUnits(int id)
{
ResultModel result = new ResultModel();
var workingunit = await _context.WorkingUnits.Where(m => m.WorkingUnitID == id).FirstOrDefaultAsync();
if (workingunit == null)
{
result.Success = false;
result.Msg = "序號錯誤";
return result;
}
//////
var workingunitNew = new WorkingUnit();
workingunitNew = workingunit;
_context.Entry(workingunitNew).State = EntityState.Modified;
if (workingunit.StatusNo == "A")
workingunitNew.StatusNo = "S";
else
workingunitNew.StatusNo = "A";
workingunitNew.UpdateDate = DateTime.Now;
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
}
}