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.
289 lines
9.4 KiB
289 lines
9.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AMESCoreStudio.WebApi.Models.AMES;
|
|
using AMESCoreStudio.CommonTools.Result;
|
|
|
|
|
|
namespace AMESCoreStudio.WebApi.Controllers.AMES
|
|
{
|
|
/// <summary>
|
|
/// 報工對應途程站點维护
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class WorkingFlowStationsController : ControllerBase
|
|
{
|
|
private readonly AMESContext _context;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public WorkingFlowStationsController(AMESContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全部報工對應途程站點
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<ResultModel<WorkingFlowStations>> GetWorkingFlowStations(int page = 0, int limit = 10)
|
|
{
|
|
IQueryable<WorkingFlowStations> q = _context.WorkingFlowStations;
|
|
ResultModel<WorkingFlowStations> result = new ResultModel<WorkingFlowStations>();
|
|
|
|
// 紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
// Table 頁數
|
|
if (page > 0)
|
|
{
|
|
q = q.Skip((page - 1) * limit).Take(limit);
|
|
}
|
|
result.Data = await q.ToListAsync();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用ID获取该報工對應途程站點
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingFlowStations>>> GetWorkingFlowStations(int id)
|
|
{
|
|
|
|
IQueryable<WorkingFlowStations> q = _context.WorkingFlowStations;
|
|
q = q.Where(p => p.WorkingFlowStationsID.Equals(id));
|
|
var WorkingFlowStations = await q.ToListAsync();
|
|
|
|
if (WorkingFlowStations == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingFlowStations;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 用生產單位获取该報工對應途程站點
|
|
/// </summary>
|
|
/// <param name="id">報工生產單位</param>
|
|
/// <returns></returns>
|
|
[HttpGet("Unit/{id}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingFlowStations>>> GetWorkingFlowStationsbyUnit(int id)
|
|
{
|
|
IQueryable<WorkingFlowStations> q = _context.WorkingFlowStations;
|
|
|
|
|
|
if (id != -99)
|
|
{
|
|
q = q.Where(p => p.UnitNo.Equals(id));
|
|
}
|
|
var WorkingFlowStations = await q.ToListAsync();
|
|
|
|
if (WorkingFlowStations == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingFlowStations;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 用生產單位获取该報工對應途程站點
|
|
/// </summary>
|
|
/// <param name="id">報工生產單位</param>
|
|
/// <returns></returns>
|
|
[HttpGet("MultiUnit/{id}")]
|
|
public async Task<ResultModel<dynamic>> GetWorkingFlowStationsMulti(int id,int page = 0, int limit = 10)
|
|
{
|
|
|
|
|
|
var q = from q1 in _context.WorkingFlowStations
|
|
join q3 in _context.Stationses on q1.StationID equals q3.StationID
|
|
join q2 in _context.FactoryUnits on q1.UnitNo equals q2.UnitNo
|
|
join q4 in _context.WorkingUnits on q1.WorkingUnitID equals q4.WorkingUnitID
|
|
join q5 in _context.WorkingStations on q1.WorkingStationID equals q5.WorkingStationID
|
|
select new
|
|
{
|
|
WorkingFlowStationsID = q1.WorkingFlowStationsID,
|
|
UnitNo = q1.UnitNo,
|
|
UnitName = q2.UnitName,
|
|
StationID = q1.StationID,
|
|
StationName = q3.StationName,
|
|
WorkingUnitID = q1.WorkingUnitID,
|
|
WorkingUnionNo = q4.WorkingUnitNo,
|
|
WorkingUnitName = q4.WorkingUnitName,
|
|
WorkingStationID = q5.WorkingStationID,
|
|
WorkingStationName = q5.WorkingStationName,
|
|
CreateUserID = q1.CreateUserID,
|
|
CreateDate = q1.CreateDate,
|
|
UpdateDate = q1.UpdateDate,
|
|
StatusNo = q1.StatusNo
|
|
|
|
};
|
|
|
|
if (id != -99)
|
|
{
|
|
q = q.Where(p => p.WorkingUnitID.Equals(id));
|
|
}
|
|
|
|
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
// 紀錄筆數
|
|
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>
|
|
/// <param name="id"></param>
|
|
/// <param name="WorkingFlowStations"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}")]
|
|
public async Task<ResultModel<WorkingFlowStations>> PutWorkingFlowStations(int id, [FromBody] WorkingFlowStations WorkingFlowStations)
|
|
{
|
|
ResultModel<WorkingFlowStations> result = new ResultModel<WorkingFlowStations>();
|
|
if (id != WorkingFlowStations.WorkingFlowStationsID)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "序號錯誤";
|
|
return result;
|
|
}
|
|
WorkingFlowStations.UpdateDate = DateTime.Now;
|
|
_context.Entry(WorkingFlowStations).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="WorkingFlowStations"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ResultModel<WorkingFlowStations>> PostWorkingFlowStations([FromBody] WorkingFlowStations WorkingFlowStations)
|
|
{
|
|
ResultModel<WorkingFlowStations> result = new ResultModel<WorkingFlowStations>();
|
|
|
|
if (WorkingFlowStationExists(WorkingFlowStations))
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "途程站別對應報工站別重覆!!";
|
|
return result;
|
|
}
|
|
|
|
Helper helper = new Helper(_context);
|
|
WorkingFlowStations.WorkingFlowStationsID = helper.GetIDKey("WorkingFlowStationID").Result;
|
|
WorkingFlowStations.CreateDate = DateTime.Now;
|
|
WorkingFlowStations.UpdateDate = DateTime.Now;
|
|
_context.WorkingFlowStations.Add(WorkingFlowStations);
|
|
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}/{uid}")]
|
|
public async Task<ResultModel<WorkingFlowStations>> DeleteWorkingFlowStations(int id,int uid)
|
|
{
|
|
ResultModel<WorkingFlowStations> result = new ResultModel<WorkingFlowStations>();
|
|
var workingunit = await _context.WorkingFlowStations.Where(m => m.WorkingFlowStationsID == id).FirstOrDefaultAsync();
|
|
if (workingunit == null)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "序號錯誤";
|
|
return result;
|
|
}
|
|
//////
|
|
var workingunitNew = new WorkingFlowStations();
|
|
workingunitNew = workingunit;
|
|
|
|
_context.Entry(workingunitNew).State = EntityState.Modified;
|
|
|
|
if (workingunit.StatusNo == "A")
|
|
workingunitNew.StatusNo = "S";
|
|
else
|
|
workingunitNew.StatusNo = "A";
|
|
workingunitNew.UpdateDate = DateTime.Now;
|
|
workingunitNew.UpdateUserID = uid;
|
|
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
private bool WorkingFlowStationExists(WorkingFlowStations model)
|
|
{
|
|
return _context.WorkingFlowStations.Any(e =>
|
|
e.WorkingStationID == model.WorkingStationID &&
|
|
e.StationID == model.StationID);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|