using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using AMESCoreStudio.WebApi;
using AMESCoreStudio.WebApi.Models.AMES;
using AMESCoreStudio.CommonTools.Result;
namespace AMESCoreStudio.WebApi.Controllers.AMES
{
///
/// 工單條碼區間設定檔
///
[Route("api/[controller]")]
[ApiController]
public class WipBarcodeOtherController : Controller
{
private readonly AMESContext _context;
///
///
///
///
public WipBarcodeOtherController(AMESContext context)
{
_context = context;
}
///
/// 獲取產品別資料
///
///
// GET: api/SystemInfoes
[HttpGet]
public async Task>> GetWipBarcodeOther()
{
IQueryable q = _context.WipBarcodeOthers;
q = q.OrderBy(p => p.OtherID);
//q = q.OrderByDescending(p => p.SystemID);
var wipBarcodeOther = await q.ToListAsync();
//return await _context.SystemInfoes.ToListAsync();
return wipBarcodeOther;
}
[HttpGet("WipNo/{id}")]
public async Task> GetWipBarcodeOtherByWipNo(string id)
{
IQueryable q = _context.WipBarcodeOthers;
var wipBarcodeOther = await q.Where(p => p.WipNO == id).FirstOrDefaultAsync();
//if (wipBarcodeOther == null)
//{
// return NotFound();
//}
return wipBarcodeOther;
}
///
/// 抓工單出貨序號區間(多筆)
///
///
///
[HttpGet("WipNos/{id}")]
public async Task>> GetWipBarcodeOtherByWipNos(string id)
{
IQueryable q = _context.WipBarcodeOthers;
var wipBarcodeOther = await q.Where(p => p.WipNO == id).ToListAsync();
//if (wipBarcodeOther == null)
//{
// return NotFound();
//}
return wipBarcodeOther;
}
///
/// 查詢客戶區間
///
/// 工單號碼
/// 序號
///
[HttpGet("ByNo")]
public async Task>> CheckWipBarcodeOtherByNo(string wipNO, string No)
{
var wipBarcodeOther = _context.WipBarcodeOthers
.FromSqlInterpolated($" SELECT * FROM JHAMES.WIP_BARCODE_OTHER WHERE {No} BETWEEN START_NO AND END_NO AND length(START_NO) = length({No}) ")
.AsNoTracking().ToList();
wipBarcodeOther = wipBarcodeOther.Where(W => W.WipNO == wipNO).ToList();
//if (wipBarcodeOther == null)
//{
// return NotFound();
//}
return wipBarcodeOther;
}
///
///
///
///
///
///
[HttpGet("WipBarcodeOtherByItemNo")]
public async Task> GetWipBarcodeOtherByItemNo(string WipNo, string ItemNo)
{
var SerialRule = _context.SerialRuleDetails.Where(W => W.ItemNo == ItemNo);//.Select(s =>s.SerialRuleDetailID.ToString()).ToList();
var wipBarcodeOther = _context.WipBarcodeOthers.Where(W => W.WipNO == WipNo);
//查工單預計開工日
var wipinfo = _context.WipInfos.Where(w => w.WipNO == WipNo).OrderBy(o => o.WipScheduleDate).Select(s => s.WipScheduleDate).FirstOrDefault();
var q = from O in _context.WipBarcodeOthers
join W in _context.WipInfos.Where(w => w.WipScheduleDate >= wipinfo) on O.WipNO equals W.WipNO
join A in _context.WipAtts on O.WipNO equals A.WipNO
join D in _context.SerialRuleDetails on O.SerialRuleDetailID equals D.SerialRuleDetailID
where D.Rule == SerialRule.Select(s => s.Rule).FirstOrDefault()
select new WipBarcodeOtherDto
{
OtherID = O.OtherID,
WipNo = O.WipNO,
ItemNo = A.ItemNO,
PlanQTY = W.PlanQTY,
CompleteQTY = W.CompleteQTY,
StartNo = O.StartNO,
EndNo = O.EndNO,
CreateDate = O.CreateDate,
Rule = D.Rule,
ItemRule = D.ItemNo,
WipScheduleDate = W.WipScheduleDate,
SerialRuleDetailID = O.SerialRuleDetailID,
Wip_Status_NO = W.StatusNO
};
q = q.Distinct().OrderBy(o => o.CreateDate);
ResultModel result = new ResultModel();
if (ItemNo.Contains("OTHER"))
{
q = q.Where(w => w.ItemRule == ItemNo);
}
result.Data = await q.ToListAsync();
// result.Data = result.Data.Select(s => { s.Status = s.Status == "Y" ? "已投產" : s.Status == "N" ? "未投產" : "末知"; return s; })
// .ToList();
result.DataTotal = result.Data.Count();
// 紀錄筆數
return result;
}
///
/// 新增工單出貨條碼區間設定檔
///
///
///
[HttpPost]
public async Task> PostWipBarcodeOther([FromBody] WipBarcodeOther wipBarcodeOther)
{
ResultModel result = new ResultModel();
Helper helper = new Helper(_context);
wipBarcodeOther.OtherID = helper.GetIDKey("OTHER_ID").Result;
try
{
_context.WipBarcodeOthers.Add(wipBarcodeOther);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
///
/// 更新工單出貨條碼區間設定檔
///
///
///
[HttpPut]
public async Task> PutWipBarcodeOther(WipBarcodeOther wipBarcodeOther)
{
ResultModel result = new ResultModel();
var wipNo = wipBarcodeOther.WipNO;
try
{
var result_old = _context.WipBarcodeOthers.Where(w => w.WipNO == wipNo).Select(s =>s.OtherID).ToList();
if (result_old.Count != 0)
{
_context.Entry(wipBarcodeOther).State = EntityState.Modified;
_context.Entry(wipBarcodeOther).Property("CreateDate").IsModified = false;
_context.Entry(wipBarcodeOther).Property("CreateUserID").IsModified = false;
}
else
{
_context.WipBarcodeOthers.Add(wipBarcodeOther);
}
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.Message;
}
return result;
}
///
/// 刪除工單出貨條碼區間
///
/// 工單號碼
///
[HttpDelete("{id}")]
public async Task> DeleteWipBarcodeOther(string id)
{
ResultModel result = new ResultModel();
var wipBarcodeOther = await _context.WipBarcodeOthers.Where(w => w.WipNO.Trim().ToUpper() == id.ToUpper().Trim()).FirstOrDefaultAsync();
try
{
if (wipBarcodeOther != null)
{
_context.WipBarcodeOthers.Remove(wipBarcodeOther);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
else
{
result.Success = true;
result.Msg = "找不到資料刪除";
}
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
///
/// 刪除工單出貨條碼區間
///
/// 工單號碼
///
[HttpDelete("byOhterID/{id}")]
public async Task> DeleteWipBarcodeOtherByOhterID(int id)
{
ResultModel result = new ResultModel();
var wipBarcodeOther = await _context.WipBarcodeOthers.Where(w => w.OtherID == id).FirstOrDefaultAsync();
try
{
if (wipBarcodeOther != null)
{
_context.WipBarcodeOthers.Remove(wipBarcodeOther);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
else
{
result.Success = true;
result.Msg = "找不到資料刪除";
}
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.InnerException.Message;
}
return result;
}
}
}