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;
using Dapper;
using AMESCoreStudio.WebApi.Extensions;
namespace AMESCoreStudio.WebApi.Controllers.AMES
{
///
/// 工單對應工項資料檔
///
[Route("api/[controller]")]
[ApiController]
public class WipFqcItemController : ControllerBase
{
private readonly AMESContext _context;
public WipFqcItemController(AMESContext context)
{
_context = context;
}
// GET: api/WipFqcItem
[HttpGet]
public async Task>> GetWipFqcItems()
{
return await _context.WipFqcItems.ToListAsync();
}
// GET: api/WipFqcItem/5
[HttpGet("{id}")]
public async Task> GetWipFqcItem(int id)
{
var wipFqcItem = await _context.WipFqcItems.FindAsync(id);
if (wipFqcItem == null)
{
return NotFound();
}
return wipFqcItem;
}
///
/// 工單號碼查詢綁定檢驗工項
///
/// 工單號碼
///
[HttpGet("ByWipNo/{id}")]
public async Task>> GetWipFqcItemByWipNo(string id)
{
return await _context.WipFqcItems.Where(w => w.WipNo == id).ToListAsync();
}
///
/// 更新工單對應工項資料檔
///
///
///
[HttpPut]
public async Task> PutWipFqcItem(WipFqcItem wipFqcItem)
{
ResultModel result = new ResultModel();
_context.Entry(wipFqcItem).State = EntityState.Modified;
wipFqcItem.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;
}
///
/// 新增工單對應工項資料檔
///
///
///
[HttpPost]
public async Task> PostWipFqcItem(WipFqcItem wipFqcItem)
{
ResultModel result = new ResultModel();
Helper helper = new Helper(_context);
wipFqcItem.WipFqcitemID = helper.GetIDKey("WIP_FQCITEM_ID").Result;
wipFqcItem.CreateDate = DateTime.Now;
wipFqcItem.UpdateDate = DateTime.Now;
_context.WipFqcItems.Add(wipFqcItem);
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
///
/// 判斷工單號碼是否有資料,沒有在取FQC群組建立資料
///
/// 工單號碼
///
[HttpPost("ByWipNo/{wipNo}")]
public async Task> PostWipFqcItemByWipNo(string wipNo)
{
var wipFqcItem = await _context.WipFqcItems.Where(w => w.WipNo == wipNo).AnyAsync();
ResultModel result = new ResultModel { Success = true };
// 沒有資料 再由FQC_ITEM新增
if (!wipFqcItem)
{
var querySql = @" SELECT DISTINCT A.WIP_NO AS WipNo ,
A.ITEM_NO AS ItemNo ,
F.QC_GROUP_ID AS QcGroupID ,
F.QC_ITEM_ID AS QcItemID
FROM JHAMES.WIP_ATT A
INNER JOIN JHAMES.MATERIAL_ITEM M ON A.ITEM_NO = M.ITEM_NO-- 料號對應檔
INNER JOIN JHAMES.FQC_ITEM_GROUP_MATERIAL FM ON FM.ITEM_ID = M.ITEM_ID--料號ID對應FQC群組
INNER JOIN JHAMES.FQC_ITEM F ON F.GROUP_ID = FM.GROUP_ID-- FQC群組對應綁定項目
WHERE A.WIP_NO = :WipNo ";
DynamicParameters p = new DynamicParameters();
p.Add("WipNo", wipNo);
var query = await _context.Database.DapperQueryAsync(querySql, p);
if (query.Any())
{
Helper helper = new Helper(_context);
foreach (var item in query)
{
item.WipFqcitemID = helper.GetIDKey("WIP_FQCITEM_ID").Result;
_context.WipFqcItems.Add(item);
}
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
}
return result;
}
// DELETE: api/WipFqcItem/5
[HttpDelete("{id}")]
public async Task> DeleteWipFqcItem(int id)
{
var wipFqcItem = await _context.WipFqcItems.FindAsync(id);
if (wipFqcItem == null)
{
return NotFound();
}
_context.WipFqcItems.Remove(wipFqcItem);
await _context.SaveChangesAsync();
return wipFqcItem;
}
private bool WipFqcItemExists(int id)
{
return _context.WipFqcItems.Any(e => e.WipFqcitemID == id);
}
}
}