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.
300 lines
9.2 KiB
300 lines
9.2 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 WorkingLinesController : ControllerBase
|
|
{
|
|
private readonly AMESContext _context;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public WorkingLinesController(AMESContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全部報工線別基本資料
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<ResultModel<WorkingLine>> GetWorkingLines(int page = 0, int limit = 10)
|
|
{
|
|
IQueryable<WorkingLine> q = _context.WorkingLines;
|
|
ResultModel<WorkingLine> result = new ResultModel<WorkingLine>();
|
|
|
|
// 紀錄筆數
|
|
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<WorkingLine>>> GetWorkingLine(int id)
|
|
{
|
|
|
|
IQueryable<WorkingLine> q = _context.WorkingLines;
|
|
q = q.Where(p => p.WorkingLineID.Equals(id));
|
|
var WorkingLine = await q.ToListAsync();
|
|
|
|
if (WorkingLine == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingLine;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 用Name获取该報工線別基本資料
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("Name/{name}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingLine>>> GetWorkingLinebyName(string name)
|
|
{
|
|
|
|
IQueryable<WorkingLine> q = _context.WorkingLines;
|
|
q = q.Where(p => p.WorkingLineName.Equals(name));
|
|
var WorkingLine = await q.ToListAsync();
|
|
|
|
if (WorkingLine == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingLine;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 用生產單位获取该報工線別基本資料
|
|
/// </summary>
|
|
/// <param name="id">報工生產單位</param>
|
|
/// <returns></returns>
|
|
[HttpGet("Unit/{id}")]
|
|
public async Task<ActionResult<IEnumerable<WorkingLine>>> GetWorkingLinesbyUnit(int id)
|
|
{
|
|
IQueryable<WorkingLine> q = _context.WorkingLines;
|
|
|
|
//var q = from q1 in _context.WorkingLines
|
|
// join q2 in _context.WorkingUnits on q1.WorkingUnitNo equals q2.WorkingUnitNo
|
|
// select new WorkingLine
|
|
// {
|
|
// WorkingLineID = q1.WorkingLineID,
|
|
// WorkingLineName = q1.WorkingLineName,
|
|
// WorkingUnitNo = q1.WorkingUnitNo,
|
|
// StatusNo = q1.StatusNo,
|
|
// CreateUserID = q1.CreateUserID,
|
|
// CreateDate = q1.CreateDate,
|
|
// UpdateDate = q1.UpdateDate,
|
|
// GetWorkingUnit = q2
|
|
// };
|
|
|
|
if (id != -99)
|
|
{
|
|
q = q.Where(p => p.WorkingUnitID.Equals(id));
|
|
}
|
|
var WorkingLine = await q.ToListAsync();
|
|
|
|
if (WorkingLine == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WorkingLine;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 用生產單位获取该報工線別基本資料
|
|
/// </summary>
|
|
/// <param name="id">報工生產單位</param>
|
|
/// <returns></returns>
|
|
[HttpGet("MultiUnit/{id}")]
|
|
public async Task<ResultModel<dynamic>> GetWorkingLinesMulti(int id,int page = 0, int limit = 10)
|
|
{
|
|
|
|
|
|
var q = from q1 in _context.WorkingLines
|
|
join q2 in _context.WorkingUnits on q1.WorkingUnitID equals q2.WorkingUnitID
|
|
select new
|
|
{
|
|
WorkingLineID = q1.WorkingLineID,
|
|
WorkingLineName = q1.WorkingLineName,
|
|
WorkingUnitID = q1.WorkingUnitID,
|
|
WorkingUnitNo = q2.WorkingUnitNo,
|
|
StatusNo = q1.StatusNo,
|
|
CreateUserID = q1.CreateUserID,
|
|
CreateDate = q1.CreateDate,
|
|
UpdateDate = q1.UpdateDate,
|
|
WorkingUnitName = q2.WorkingUnitName
|
|
};
|
|
|
|
if (id != -99)
|
|
{
|
|
q = q.Where(p => p.WorkingUnitID.Equals(id));
|
|
}
|
|
var WorkingLine = await q.ToListAsync();
|
|
|
|
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="WorkingLine"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}")]
|
|
public async Task<ResultModel<WorkingLine>> PutWorkingLines(int id, [FromBody] WorkingLine WorkingLine)
|
|
{
|
|
ResultModel<WorkingLine> result = new ResultModel<WorkingLine>();
|
|
if (id != WorkingLine.WorkingLineID)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "序號錯誤";
|
|
return result;
|
|
}
|
|
WorkingLine.UpdateDate = DateTime.Now;
|
|
_context.Entry(WorkingLine).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="WorkingLine"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ResultModel<WorkingLine>> PostWorkingLines(WorkingLine WorkingLine)
|
|
{
|
|
ResultModel<WorkingLine> result = new ResultModel<WorkingLine>();
|
|
Helper helper = new Helper(_context);
|
|
WorkingLine.WorkingLineID = helper.GetIDKey("WorkingLine_ID").Result;
|
|
WorkingLine.CreateDate = DateTime.Now;
|
|
WorkingLine.UpdateDate = DateTime.Now;
|
|
_context.WorkingLines.Add(WorkingLine);
|
|
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<WorkingLine>> DeleteWorkingLines(int id)
|
|
{
|
|
ResultModel<WorkingLine> result = new ResultModel<WorkingLine>();
|
|
var workingunit = await _context.WorkingLines.Where(m => m.WorkingLineID == id).FirstOrDefaultAsync();
|
|
if (workingunit == null)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "序號錯誤";
|
|
return result;
|
|
}
|
|
//////
|
|
var workingunitNew = new WorkingLine();
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|