You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
303 lines
9.3 KiB
303 lines
9.3 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 WorkingStationsController : ControllerBase
|
|
{
|
|
private readonly AMESContext _context;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public WorkingStationsController(AMESContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全部報工生產單位別
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<ResultModel<WorkingStation>> GetWorkingStations(int page = 0, int limit = 10)
|
|
{
|
|
IQueryable<WorkingStation> q = _context.WorkingStations;
|
|
ResultModel<WorkingStation> result = new ResultModel<WorkingStation>();
|
|
|
|
// 紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
// Table 頁數
|
|
if (page > 0)
|
|
{
|
|
q = q.Skip((page - 1) * limit).Take(limit);
|
|
}
|
|
result.Data = await q.ToListAsync();
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 获取全部報工生產單位別
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("byALL")]
|
|
public async Task<ActionResult<IEnumerable<WorkingStation>>> GetWorkingStationALLs()
|
|
{
|
|
IQueryable<WorkingStation> q = _context.WorkingStations;
|
|
q = q.OrderBy(p => p.WorkingStationID);
|
|
var WorkingStations = await q.ToListAsync();
|
|
return WorkingStations;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 用ID获取该報工生產單位別
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingStation>>> GetWorkingStation(int id)
|
|
{
|
|
|
|
IQueryable<WorkingStation> q = _context.WorkingStations;
|
|
q = q.Where(p => p.WorkingStationID.Equals(id));
|
|
var WorkingStation = await q.ToListAsync();
|
|
|
|
if (WorkingStation == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingStation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用Name获取该報工生產單位別
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("Name/{id}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingStation>>> GetWorkingStationbyName(string id)
|
|
{
|
|
|
|
IQueryable<WorkingStation> q = _context.WorkingStations;
|
|
q = q.Where(p => p.WorkingStationName.Equals(id));
|
|
var WorkingStation = await q.ToListAsync();
|
|
|
|
if (WorkingStation == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingStation;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 用生產單位获取该報工線別基本資料
|
|
/// </summary>
|
|
/// <param name="id">報工生產單位</param>
|
|
/// <returns></returns>
|
|
[HttpGet("Unit/{id}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingStation>>> GetWorkingStations(string id)
|
|
{
|
|
try
|
|
{
|
|
IQueryable<WorkingStation> q = _context.WorkingStations;
|
|
if (id != "all")
|
|
{
|
|
q = q.Where(p => p.WorkingUnitNo.Equals(id));
|
|
}
|
|
var WorkingStation = await q.ToListAsync();
|
|
|
|
if (WorkingStation == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return WorkingStation;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var aa= ex.InnerException.Message;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 用用生產單位获取该報工線別基本資料
|
|
/// </summary>
|
|
/// <param name="id">報工生產單位</param>
|
|
/// <returns></returns>
|
|
[HttpGet("MultiUnit/{id}")]
|
|
public async Task<ActionResult<IEnumerable<dynamic>>> GetWorkingStationsMulti(string id)
|
|
{
|
|
// IQueryable<WorkingLine> q = _context.WorkingLines;
|
|
|
|
var q = from q1 in _context.WorkingStations
|
|
join q2 in _context.WorkingUnits on q1.WorkingUnitNo equals q2.WorkingUnitNo
|
|
select new
|
|
{
|
|
WorkingStationID = q1.WorkingStationID,
|
|
WorkingStationName = q1.WorkingStationName,
|
|
WorkingStationDesc = q1.WorkingStationDesc,
|
|
WorkingUnitNo = q1.WorkingUnitNo,
|
|
TypeNo = q1.TypeNo,
|
|
StatusNo = q1.StatusNo,
|
|
CreateUserID = q1.CreateUserID,
|
|
CreateDate = q1.CreateDate,
|
|
UpdateDate = q1.UpdateDate,
|
|
WorkingUnitName = q2.WorkingUnitName
|
|
};
|
|
|
|
if (id != "all")
|
|
{
|
|
q = q.Where(p => p.WorkingUnitNo.Equals(id));
|
|
}
|
|
var WorkingLine = await q.ToListAsync();
|
|
|
|
if (WorkingLine == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingLine;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 更新條報工生產單位別
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="WorkingStation"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}")]
|
|
public async Task<ResultModel<WorkingStation>> PutWorkingStations(int id, [FromBody] WorkingStation WorkingStation)
|
|
{
|
|
ResultModel<WorkingStation> result = new ResultModel<WorkingStation>();
|
|
if (id != WorkingStation.WorkingStationID)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "序號錯誤";
|
|
return result;
|
|
}
|
|
WorkingStation.UpdateDate = DateTime.Now;
|
|
_context.Entry(WorkingStation).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;
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增報工生產單位別
|
|
/// </summary>
|
|
/// <param name="WorkingStation"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ResultModel<WorkingStation>> PostWorkingStations(WorkingStation WorkingStation)
|
|
{
|
|
ResultModel<WorkingStation> result = new ResultModel<WorkingStation>();
|
|
Helper helper = new Helper(_context);
|
|
WorkingStation.WorkingStationID = helper.GetIDKey("WorkingStation_ID").Result;
|
|
WorkingStation.CreateDate = DateTime.Now;
|
|
WorkingStation.UpdateDate = DateTime.Now;
|
|
_context.WorkingStations.Add(WorkingStation);
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 停用/啟用報工生產單位別
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ResultModel<WorkingStation>> DeleteWorkingStations(int id)
|
|
{
|
|
ResultModel<WorkingStation> result = new ResultModel<WorkingStation>();
|
|
var WorkingStation = await _context.WorkingStations.Where(m => m.WorkingStationID == id).FirstOrDefaultAsync();
|
|
if (WorkingStation == null)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "序號錯誤";
|
|
return result;
|
|
}
|
|
//////
|
|
var WorkingStationNew = new WorkingStation();
|
|
WorkingStationNew = WorkingStation;
|
|
|
|
_context.Entry(WorkingStationNew).State = EntityState.Modified;
|
|
|
|
if (WorkingStation.StatusNo == "A")
|
|
WorkingStationNew.StatusNo = "S";
|
|
else
|
|
WorkingStationNew.StatusNo = "A";
|
|
WorkingStationNew.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;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|