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; using AMESCoreStudio.WebApi.Extensions; using Dapper; using Microsoft.AspNetCore.Components.Forms; using static Azure.Core.HttpHeader; 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(); return wipBarcodeOther; } /// /// 抓工單出貨序號區間(多筆) /// /// /// [HttpGet("WipNos/{id}")] public async Task>> GetWipBarcodeOtherByWipNos(string id) { IQueryable q = _context.WipBarcodeOthers; var wipBarcodeOther = await q.Where(p => p.WipNO == id).OrderBy(o => o.StartNO).ToListAsync(); return wipBarcodeOther; } /// /// 查詢客戶區間 /// /// 工單號碼 /// 序號 /// [HttpGet("ByNo")] public async Task>> CheckWipBarcodeOtherByNo(string wipNO, string No) { var wipBarcodeOther = await _context.WipBarcodeOthers .FromSqlInterpolated($" SELECT * FROM JHAMES.WIP_BARCODE_OTHER WHERE {No} BETWEEN START_NO AND END_NO AND length(START_NO) = length({No}) ") .AsNoTracking().ToListAsync(); wipBarcodeOther = wipBarcodeOther.Where(W => W.WipNO == wipNO).ToList(); //if (wipBarcodeOther == null) //{ // return NotFound(); //} return wipBarcodeOther; } /// /// 確認是否有重複出貨序號區間 /// /// 起始出貨條碼 /// 結束出貨條碼 /// [HttpGet("CheckRepeat")] public ActionResult> CheckWipBarcodeOtherRepeat(string startNo, string endNo) { var wipBarcodeOther = _context.WipBarcodeOthers .FromSqlInterpolated($@" SELECT * FROM JHAMES.WIP_BARCODE_OTHER WHERE {startNo} BETWEEN START_NO AND END_NO OR {endNo} BETWEEN START_NO AND END_NO ").AsNoTracking().ToList(); return wipBarcodeOther; } /// /// 判斷出貨序號是否重複 By 新版 /// /// 起始出貨條碼(流水碼要轉十進位) /// 結束出貨條碼(流水碼要轉十進位) /// 條碼前綴 /// 工單號碼 /// [HttpGet("CheckRepeatByNew")] public async Task CheckWipBarcodeOtherRepeatByNew(string startNo, string endNo, string prefix, string wipNo) { var query = @$" SELECT * FROM JHAMES.WIP_BARCODE_OTHER O INNER JOIN JHAMES.WIP_INFO W ON O.WIP_NO = W.WIP_NO WHERE O.CHECK_REPEAT = 'Y' -- 判斷重複 AND SUBSTR(O.WIP_NO ,1 ,1) <> '6' -- 6開頭工單號碼不判斷重複 AND O.WIP_NO <> :WipNo -- 排除自己工單號碼 AND SUBSTR(O.START_NO,0 ,LENGTH(O.START_NO) - O.SERIAL_LENGTH - O.TRUNCATE) = :Prefix --取相同條碼前綴 AND LENGTH(:StartNo) = LENGTH(O.START_NO) - O.TRUNCATE --判斷去尾碼後 條碼區間長度一致 AND ((O.RADIX = 'D' AND -- 十進位判斷 條碼間字串-去尾碼 (:StartNo BETWEEN SUBSTR(O.START_NO, 1, LENGTH(O.START_NO) - O.TRUNCATE) AND SUBSTR(O.END_NO, 1, LENGTH(O.END_NO) - O.TRUNCATE) OR :EndNo BETWEEN SUBSTR(O.START_NO, 1, LENGTH(O.START_NO) - O.TRUNCATE) AND SUBSTR(O.END_NO, 1, LENGTH(O.END_NO) - O.TRUNCATE))) OR (O.RADIX = 'H' AND -- 十六進位判斷 取條碼前綴 || 流水碼轉成十進位 (:StartNo BETWEEN SUBSTR(O.START_NO, 1, LENGTH(O.START_NO) - O.SERIAL_LENGTH - O.TRUNCATE) || LPAD(TO_NUMBER(SUBSTR(O.START_NO, -(O.SERIAL_LENGTH + O.TRUNCATE) , O.SERIAL_LENGTH), 'XXXXXXXX'),O.SERIAL_LENGTH,'0') AND SUBSTR(O.END_NO, 1, LENGTH(O.END_NO) - O.SERIAL_LENGTH - O.TRUNCATE) || LPAD(TO_NUMBER(SUBSTR(O.END_NO, -(O.SERIAL_LENGTH + O.TRUNCATE) , O.SERIAL_LENGTH), 'XXXXXXXX'),O.SERIAL_LENGTH,'0') OR :EndNo BETWEEN SUBSTR(O.START_NO, 1, LENGTH(O.START_NO) - O.SERIAL_LENGTH - O.TRUNCATE) || LPAD(TO_NUMBER(SUBSTR(O.START_NO, -(O.SERIAL_LENGTH + O.TRUNCATE) , O.SERIAL_LENGTH), 'XXXXXXXX'),O.SERIAL_LENGTH,'0') AND SUBSTR(O.END_NO, 1, LENGTH(O.END_NO) - O.SERIAL_LENGTH - O.TRUNCATE) || LPAD(TO_NUMBER(SUBSTR(O.END_NO, -(O.SERIAL_LENGTH + O.TRUNCATE) , O.SERIAL_LENGTH), 'XXXXXXXX'),O.SERIAL_LENGTH,'0'))))"; DynamicParameters p = new DynamicParameters(); p.Add("Prefix", prefix); p.Add("StartNo", startNo); p.Add("EndNo", endNo); p.Add("WipNo", wipNo); var q = await _context.Database.DapperQueryAsync(query, p); try { // 有重複序號在細分 板卡與系統組裝可重複,板卡與板卡工單、組裝與組裝工單不可重複 if (q.Any()) { // 板卡相關製程代號 var unitBoard = new List { "S", "D", "I", "P" }; // 系統組裝相關製程代號 var unitSystem = new List { "B", "T", "O" }; query = $@" SELECT * FROM JHAMES.WIP_INFO WHERE WIP_NO=:WipNo "; p = new DynamicParameters(); p.Add("WipNo", wipNo); // 取得目前工單相關資料 var q1 = await _context.Database.DapperQueryAsync(query, p); // 當目前工單所屬於板卡,在判斷出貨重複是否有涵蓋板卡製程 if (q1.Any(w => unitBoard.Contains(w.UnitNO))) { if (q.Any(w => unitBoard.Contains(w.UNITNO))) return true; } // 當目前工單所屬於系統,在判斷出貨重複是否有涵蓋系統製程 else if (q1.Any(w => unitSystem.Contains(w.UnitNO))) { if (q.Any(w => unitSystem.Contains(w.UNITNO))) return true; } } else return false; } catch { return true; } return true; } /// /// 新增工單出貨條碼區間設定檔 /// /// /// [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; } /// /// 刪除出貨序號區間 /// /// KeyId /// [HttpDelete("KeyId/{id}")] public async Task> DeleteWipBarcodeOtherByKeyId(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; } } }