4 changed files with 31 additions and 184 deletions
@ -1,183 +0,0 @@ |
|||||
using Microsoft.AspNetCore.Http; |
|
||||
using Microsoft.AspNetCore.Mvc; |
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Linq; |
|
||||
using System.Threading.Tasks; |
|
||||
using Microsoft.EntityFrameworkCore; |
|
||||
using AMESCoreStudio.WebApi.Models.BAS; |
|
||||
using AMESCoreStudio.CommonTools.Result; |
|
||||
|
|
||||
namespace AMESCoreStudio.WebApi.Controllers.AMES |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// 料號基本資料檔Controller
|
|
||||
/// </summary>
|
|
||||
[Route("api/[controller]")]
|
|
||||
[ApiController] |
|
||||
public class MaterialFlowController : ControllerBase |
|
||||
{ |
|
||||
private readonly AMESContext _context; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 建構式
|
|
||||
/// </summary>
|
|
||||
/// <param name="context"></param>
|
|
||||
public MaterialFlowController(AMESContext context) |
|
||||
{ |
|
||||
_context = context; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 查詢料號流程資料檔
|
|
||||
/// </summary>
|
|
||||
/// <returns></returns>
|
|
||||
// GET: api/<MaterialFlowsController>
|
|
||||
[HttpGet] |
|
||||
public async Task<ActionResult<IEnumerable<MaterialFlow>>> GetMaterialFlows() |
|
||||
{ |
|
||||
IQueryable<MaterialFlow> q = _context.MaterialFlows; |
|
||||
q = q.OrderBy(p => p.MFID); |
|
||||
var MaterialFlows = await q.ToListAsync(); |
|
||||
return MaterialFlows; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 料號流程資料檔
|
|
||||
/// </summary>
|
|
||||
/// <param name="id">ItemID</param>
|
|
||||
/// <returns></returns>
|
|
||||
[HttpGet("{id}")] |
|
||||
public async Task<IEnumerable<MaterialFlow>> GetMaterialFlow(int id) |
|
||||
{ |
|
||||
IQueryable<MaterialFlow> q = _context.MaterialFlows; |
|
||||
var result = await q.Where(p => p.MFID == id).ToListAsync(); |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 料號流程查詢
|
|
||||
/// </summary>
|
|
||||
/// <param name="itemno">料號</param>
|
|
||||
/// <param name="unitNo">站別</param>
|
|
||||
/// <returns></returns>
|
|
||||
[HttpGet("Query/{itemno}/{unitNo}")] |
|
||||
public async Task<ActionResult<IEnumerable<MaterialFlow>>> GetMaterialFlowsByQuery(string itemno, string unitNo) |
|
||||
{ |
|
||||
IQueryable<MaterialFlow> q = _context.MaterialFlows; |
|
||||
|
|
||||
MaterialItemController materialItemController = new MaterialItemController(_context); |
|
||||
var MaterialItem = materialItemController.GetMaterialItemByItemNO(itemno); |
|
||||
|
|
||||
if (MaterialItem != null) |
|
||||
q = q.Where(w => w.ItemID == MaterialItem.Result.ItemID && w.UnitNo == unitNo); |
|
||||
else |
|
||||
q = q.Where(w => w.ItemID == -1); |
|
||||
|
|
||||
var materialFlows = await q.ToListAsync(); |
|
||||
|
|
||||
return materialFlows; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 新增料號流程資料檔
|
|
||||
/// </summary>
|
|
||||
/// <param name="MaterialFlow"></param>
|
|
||||
/// <returns></returns>
|
|
||||
[HttpPost] |
|
||||
public async Task<ResultModel<MaterialFlow>> PostMaterialFlows([FromBody] MaterialFlow MaterialFlow) |
|
||||
{ |
|
||||
ResultModel<MaterialFlow> result = new ResultModel<MaterialFlow>(); |
|
||||
Helper helper = new Helper(_context); |
|
||||
MaterialFlow.MFID = helper.GetIDKey("MF_ID").Result; |
|
||||
|
|
||||
_context.MaterialFlows.Add(MaterialFlow); |
|
||||
try |
|
||||
{ |
|
||||
await _context.SaveChangesAsync(); |
|
||||
result.Success = true; |
|
||||
result.Msg = "OK"; |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
result.Success = false; |
|
||||
result.Msg = ex.InnerException.Message; |
|
||||
} |
|
||||
return result; |
|
||||
|
|
||||
} |
|
||||
|
|
||||
|
|
||||
/// <summary>
|
|
||||
/// 修改料號流程資料檔
|
|
||||
/// </summary>
|
|
||||
/// <returns></returns>
|
|
||||
[HttpPut("{id}")] |
|
||||
public async Task<ResultModel<MaterialFlow>> PutMaterialFlows(int id, [FromBody] MaterialFlow materialFlow) |
|
||||
{ |
|
||||
ResultModel<MaterialFlow> result = new ResultModel<MaterialFlow>(); |
|
||||
if (id != materialFlow.MFID) |
|
||||
{ |
|
||||
result.Success = false; |
|
||||
result.Msg = "料號錯誤"; |
|
||||
return result; |
|
||||
} |
|
||||
//_
|
|
||||
//_context.Attach(materialFlow);
|
|
||||
//// 指定更新某個欄位
|
|
||||
//_context.Entry(materialFlow).Property(p => p.FlowSEQ).IsModified = true;
|
|
||||
//_context.Entry(materialFlow).Property(p => p.ItemID).IsModified = true;
|
|
||||
//_context.Entry(materialFlow).Property(p => p.FlowRuleID).IsModified = true;
|
|
||||
//_context.Entry(materialFlow).Property(p => p.UnitNo).IsModified = true;
|
|
||||
_context.Entry(materialFlow).State = EntityState.Modified; |
|
||||
try |
|
||||
{ |
|
||||
await _context.SaveChangesAsync(); |
|
||||
result.Success = true; |
|
||||
result.Msg = "OK"; |
|
||||
return result; |
|
||||
} |
|
||||
catch (DbUpdateConcurrencyException ex) |
|
||||
{ |
|
||||
result.Success = false; |
|
||||
result.Msg = ex.InnerException.Message; |
|
||||
return result; |
|
||||
//throw;
|
|
||||
|
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Delete料號流程
|
|
||||
/// </summary>
|
|
||||
/// <param name="id"></param>
|
|
||||
/// <returns></returns>
|
|
||||
// DELETE: api/StandardWorkTimes/5
|
|
||||
[HttpDelete("{id}")] |
|
||||
public async Task<ResultModel<MaterialFlow>> DeleteMaterialFlow(int id) |
|
||||
{ |
|
||||
ResultModel<MaterialFlow> result = new ResultModel<MaterialFlow>(); |
|
||||
|
|
||||
var materialFlow = await _context.MaterialFlows.Where(m => m.MFID == id).FirstOrDefaultAsync(); |
|
||||
if (materialFlow == null) |
|
||||
{ |
|
||||
result.Success = false; |
|
||||
result.Msg = "序號不存在"; |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
_context.MaterialFlows.Remove(materialFlow); |
|
||||
await _context.SaveChangesAsync(); |
|
||||
|
|
||||
result.Success = true; |
|
||||
result.Msg = "OK"; |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
private bool MaterialFlowExists(int id) |
|
||||
{ |
|
||||
return _context.MaterialFlows.Any(e => e.MFID == id); |
|
||||
} |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue