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.WebApi.DTO.AMES; using AMESCoreStudio.CommonTools.Result; using AMESCoreStudio.WebApi.Enum; using Dapper; using System.Data; using AMESCoreStudio.WebApi.Extensions; namespace AMESCoreStudio.WebApi.Controllers.AMES { /// /// 入庫單資料檔 /// [Route("api/[controller]")] [ApiController] public class FqcInhouseMasterController : ControllerBase { private readonly AMESContext _context; public FqcInhouseMasterController(AMESContext context) { _context = context; } // GET: api/FqcInhouseMaster [HttpGet] public async Task>> GetFqcInhouseMasters() { return await _context.FqcInhouseMasters.ToListAsync(); } // GET: api/FqcInhouseMaster/5 [HttpGet("ByWipNo/{wipNo}")] public async Task>> GetFqcInhouseMasterByWipNo(string wipNo) { var fqcInhouseMaster = await _context.FqcInhouseMasters.Where(w => w.WipNo == wipNo.ToUpper()) .ToListAsync(); return fqcInhouseMaster; } /// /// 查詢入庫單by入庫單號 /// /// /// [HttpGet("ByinhouseNo/{inhouseNo}")] public async Task>> GetFqcInhouseMasterByinhouseNo(string inhouseNo) { var fqcInhouseMaster = await _context.FqcInhouseMasters.Where(w => w.InhouseNo == inhouseNo.ToUpper()) .ToListAsync(); return fqcInhouseMaster; } // GET: api/FqcInhouseMaster/5 [HttpGet("{inhouseNo}/{seqID}")] public async Task> GetFqcInhouseMaster(string inhouseNo, int seqID) { var fqcInhouseMaster = await _context.FqcInhouseMasters.FindAsync(inhouseNo, seqID); if (fqcInhouseMaster == null) { return NotFound(); } return fqcInhouseMaster; } /// /// 取最新入庫單號 /// /// /// [HttpGet("NewInhouseNo/{inhouseNo}")] public async Task> GetFqcInhouseMasterNewInhouseNo(string inhouseNo) { var fqcInhouseMaster = await _context.FqcInhouseMasters.Where(w => w.InhouseNo.StartsWith(inhouseNo)).ToListAsync(); string NowInhouseNo = string.Empty; if (fqcInhouseMaster.Count() == 0) return inhouseNo + "001"; NowInhouseNo = fqcInhouseMaster.OrderByDescending(o => o.InhouseNo).FirstOrDefault().InhouseNo; inhouseNo += (int.Parse(NowInhouseNo.Substring(NowInhouseNo.Length - 3, 3)) + 1).ToString().PadLeft(3, '0'); return inhouseNo; } /// /// FQC查詢 by Table /// /// 內部序號 /// 工單號碼 /// 外箱號碼 /// 入庫單號碼 /// 入庫時間起 /// 入庫時間迄 /// 抽驗結果 /// 頁數 /// 筆數 /// 委外廠商ID /// 委外廠商No /// 生產單位 /// [Obsolete] [HttpGet("FqcInhouseMasterQueryOld")] public async Task> GetFqcInhouseMasterQueryOld(string barcodeNo, string wipNo, string boxNo , string inhouseNo, string date_str, string date_end, string status, string factoryID, string factoryNo, int page = 0, int limit = 10, string unit = null) { var q = from q1 in _context.FqcInhouseMasters join q2 in _context.FqcInhouseDetails on new { q1.InhouseNo, q1.SeqID } equals new { q2.InhouseNo, q2.SeqID } into j0 from q2 in j0.DefaultIfEmpty() join q3 in _context.FqcResultMasters on new { q1.InhouseNo, q1.SeqID } equals new { q3.InhouseNo, q3.SeqID } into j1 from q3 in j1.DefaultIfEmpty() join q4 in _context.WipInfos on q1.WipNo equals q4.WipNO into s from q4 in s.DefaultIfEmpty() join q5 in _context.FactoryInfos on q4.Werks equals q5.FactoryID.ToString() into j2 from q5 in j2.DefaultIfEmpty() select new FqcInhouseMasterDto { Werks = q4.Werks, SerialNo = q2.SerialNo, FactoryNo = q5.FactoryNo, InhouseNo = q1.InhouseNo, SeqID = q1.SeqID, WipNo = q1.WipNo, ItemNo = q1.ItemNo, InhouseQty = q1.InhouseQty, ModelNo = q1.ModelNo, StatusName = q1.Status, ProTypeName = q1.ProType, CreateDate = q1.CreateDate, EndTime = q3.EndTime, SpecialNo = q3.SpecialPo, QaMemo = q3.QaMeno, }; //q1.Status == "P" ? "允收" : q1.Status == "R" ? "批退" : "未驗收完成", //IQueryable q1 = _context.FqcInhouseDetails; if (!string.IsNullOrWhiteSpace(inhouseNo)) q = q.Where(w => w.InhouseNo == inhouseNo); if (!string.IsNullOrWhiteSpace(wipNo)) q = q.Where(w => w.WipNo == wipNo); if (!string.IsNullOrWhiteSpace(status)) { // P1 特採允收 if (status != "P1") q = q.Where(w => w.StatusName == status); else { q = q.Where(w => w.StatusName == "P" && !string.IsNullOrWhiteSpace(w.SpecialNo)); } } if (!string.IsNullOrWhiteSpace(factoryID)) q = q.Where(w => w.Werks.ToString() == factoryID); if (!string.IsNullOrWhiteSpace(factoryNo)) q = q.Where(w => w.FactoryNo.ToString() == factoryNo); // 優先用內部序號取出包裝序號 if (!string.IsNullOrWhiteSpace(barcodeNo)) { BarcodeInfoesController barcodeInfoesController = new BarcodeInfoesController(_context); var q1 = await barcodeInfoesController.GetBarcodeInfoesByNo(barcodeNo); if (q1.Value.Count() != 0) { boxNo = q1.Value.FirstOrDefault().BoxNo; } } if (!string.IsNullOrWhiteSpace(boxNo)) { q = q.Where(w => w.SerialNo == boxNo); } if (DateTime.TryParse(date_str, out _)) { q = q.Where(w => w.CreateDate >= DateTime.Parse(date_str)); } if (DateTime.TryParse(date_end, out _)) { q = q.Where(w => w.CreateDate <= DateTime.Parse(date_end)); } ResultModel result = new ResultModel(); var resultQuery = await q.Select(s => new FqcInhouseMasterDto { Werks = s.Werks, FactoryNo = s.FactoryNo, InhouseNo = s.InhouseNo, SeqID = s.SeqID, WipNo = s.WipNo, ItemNo = s.ItemNo, InhouseQty = s.InhouseQty, ModelNo = s.ModelNo, StatusName = s.StatusName, ProTypeName = s.ProTypeName, CreateDate = s.CreateDate, EndTime = s.EndTime, SpecialNo = s.SpecialNo, QaMemo = s.QaMemo }).Distinct().ToListAsync(); // 紀錄筆數 result.DataTotal = resultQuery.Count(); // Table 頁數 if (page > 0) { resultQuery = resultQuery.Skip((page - 1) * limit).Take(limit).ToList(); } result.Data = resultQuery; // 判斷結束時間 result.Data = result.Data.Select(s => { s.EndTime = s.StatusName == "A" ? null : s.EndTime; return s; }) .ToList(); // 修改狀態名稱 result.Data = result.Data.Select(s => { s.StatusName = s.StatusName == "P" ? string.IsNullOrWhiteSpace(s.SpecialNo) ? "允收" : "特採允收" : s.StatusName == "R" ? "批退" : "未驗收完成"; return s; }) .ToList(); return result; } /// /// FQC查詢 by Table /// /// 內部序號 /// 工單號碼 /// 外箱號碼 /// 入庫單號碼 /// 入庫時間起 /// 入庫時間迄 /// 抽驗結果 /// 頁數 /// 筆數 /// 委外廠商ID /// 委外廠商No /// 生產單位 /// [HttpGet("FqcInhouseMasterQuery")] public ResultModel GetFqcInhouseMasterQuery(string barcodeNo, string wipNo, string boxNo , string inhouseNo, string date_str, string date_end, string status, string factoryID, string factoryNo, int page = 0, int limit = 10, string unit = null) { ResultModel result = new ResultModel(); var sql = @"SELECT DISTINCT FM.INHOUSE_NO AS InhouseNo , FM.SEQ_ID AS SeqID , FM.WIP_NO AS WipNo , FM.ITEM_NO AS ItemNo , FM.INHOUSE_QTY AS InhouseQty , FM.MODEL_NO AS ModelNo , FM.STATUS AS StatusName , FM.CREATE_DATE AS CreateDate , FM.PRO_TYPE AS ProType , --FD.SERIAL_NO AS SerialNo , RM.END_TIME AS EndTime , RM.SPECIAL_PO AS SpecialPo , RM.QA_MENO AS QaMeno , W.WERKS AS Werks , FA.FACTORY_NO AS FactoryNo , FU.UNIT_NAME AS UnitName FROM JHAMES.FQC_INHOUSE_MASTER FM LEFT JOIN JHAMES.FQC_INHOUSE_DETAIL FD ON FM.INHOUSE_NO = FD.INHOUSE_NO AND FM.SEQ_ID = FD.SEQ_ID -- 入庫Deail LEFT JOIN JHAMES.FQC_RESULT_MASTER RM ON FM.INHOUSE_NO = RM.INHOUSE_NO AND FM.SEQ_ID = RM.SEQ_ID -- FQC檢驗表 LEFT JOIN JHAMES.WIP_INFO W ON FM.WIP_NO = W.WIP_NO AND FM.UNIT_NO = W.UNIT_NO -- 工單資料 LEFT JOIN JHAMES.FACTORY_INFO FA ON W.WERKS = FA.FACTORY_ID -- 廠別 LEFT JOIN JHAMES.FACTORY_UNIT FU ON FM.UNIT_NO = FU.UNIT_NO -- 製程 LEFT JOIN JHAMES.BARCODE_INFO B ON B.BOX_NO = FD.SERIAL_NO -- 條碼資料 WHERE 1 = 1"; DynamicParameters p = new DynamicParameters(); if (!string.IsNullOrWhiteSpace(inhouseNo)) { sql += " AND FM.INHOUSE_NO = :inhouseNo "; p.Add("inhouseNo", inhouseNo, DbType.AnsiString); } if (!string.IsNullOrWhiteSpace(wipNo)) { sql += " AND FM.WIP_NO = :wipNo "; p.Add("wipNo", wipNo, DbType.AnsiString); } if (!string.IsNullOrWhiteSpace(status)) { // P1 特採允收 if (status != "P1") { sql += " AND FM.STATUS = :status "; p.Add("status", status, DbType.AnsiString); } else { sql += " AND FM.STATUS = :status AND RM.SPECIAL_PO IS NOT NULL"; p.Add("status", status, DbType.AnsiString); } } if (!string.IsNullOrWhiteSpace(factoryID)) { sql += " AND W.WERKS = :factoryID "; p.Add("factoryID", factoryID); } if (!string.IsNullOrWhiteSpace(factoryNo)) { sql += " AND FA.FACTORY_NO = :factoryNo "; p.Add("factoryNo", factoryNo, DbType.AnsiString); } if (!string.IsNullOrWhiteSpace(barcodeNo)) { sql += " AND B.BARCODE_NO = :barcodeNo "; p.Add("barcodeNo", barcodeNo, DbType.AnsiString); } if (!string.IsNullOrWhiteSpace(boxNo)) { sql += " AND FD.SERIAL_NO = :boxNo "; p.Add("boxNo", boxNo, DbType.AnsiString); } if (!string.IsNullOrWhiteSpace(unit)) { sql += " AND FM.UNIT_NO = :unit "; p.Add("unit", unit, DbType.AnsiString); } if (DateTime.TryParse(date_str, out _)) { sql += " AND FM.CREATE_DATE <= TO_DATE(:date_str, 'YYYY-MM-DD HH24:MI:SS')"; p.Add("date_str", $"{date_str} 00:00:00"); } if (DateTime.TryParse(date_end, out _)) { sql += " AND FM.CREATE_DATE >= TO_DATE(:date_end, 'YYYY-MM-DD HH24:MI:SS') "; p.Add("date_end", $"{date_end} 23:59:59"); } try { var q = _context.Database.DapperQuery(sql, p); // 紀錄筆數 result.DataTotal = q.Count(); // Table 頁數 if (page > 0) { q = q.Skip((page - 1) * limit).Take(limit).ToList(); } result.Data = q; // 判斷結束時間 result.Data = result.Data.Select(s => { s.EndTime = s.StatusName == "A" ? null : s.EndTime; return s; }) .ToList(); // 修改狀態名稱 result.Data = result.Data.Select(s => { s.StatusName = s.StatusName == "P" ? string.IsNullOrWhiteSpace(s.SpecialNo) ? "允收" : "特採允收" : s.StatusName == "R" ? "批退" : "未驗收完成"; return s; }) .ToList(); } catch (Exception ex) { result.Success = false; result.Msg = ex.Message; } return result; } /// /// FQC查詢 抽驗明細Dto /// /// 工單號碼 /// [HttpGet("FqcInhouseMasterQuery/ExamineDetail/{id}")] public ResultModel GetFqcInhouseMasterQueryExamineDetail(string id) { ResultModel result = new ResultModel(); var sql = @" SELECT M.WIP_NO AS WipNo , M.INHOUSE_NO AS InhouseNo , M.SEQ_ID AS SeqID , FU.UNIT_NAME AS UnitName , D.BARCODE_NO AS BarcodeNo , CASE WHEN D.STATUS_NO = 'P' THEN 'PASS' WHEN D.STATUS_NO = 'F' THEN 'NG' END AS StatusNo , D.BOX_NO AS BoxNo , D.EXTRA_BARCODE_NO AS ExtraBarcodeNo , D.CREATE_DATE AS CreateDate FROM JHAMES.FQC_RESULT_MASTER M INNER JOIN JHAMES.FQC_RESULT_DETAIL D ON M.FQC_ID = D.FQC_ID INNER JOIN JHAMES.FQC_INHOUSE_MASTER FM ON FM.INHOUSE_NO = M.INHOUSE_NO AND FM.SEQ_ID = M.SEQ_ID INNER JOIN JHAMES.FACTORY_UNIT FU ON FM.UNIT_NO = FU.UNIT_NO WHERE M.WIP_NO = :id"; DynamicParameters p = new DynamicParameters(); p.Add("id", id, DbType.AnsiString); try { var q = _context.Database.DapperQuery(sql, p); // 紀錄筆數 result.DataTotal = q.Count(); result.Data = q; } catch (Exception ex) { result.Success = false; result.Msg = ex.Message; } return result; } /// /// FQC抽驗資料 /// /// 入庫單號碼 /// 序號 /// [HttpGet("FqcQuery/{inhouseNo}")] public async Task> GetFqcQuery(string inhouseNo, int seqid = 1) { IQueryable q = from q1 in _context.FqcInhouseMasters.Where(w => w.InhouseNo == inhouseNo && w.SeqID == seqid) join q2 in _context.FqcResultMasters on new { q1.InhouseNo, q1.SeqID } equals new { q2.InhouseNo, q2.SeqID } into cp from q2 in cp.DefaultIfEmpty() join q3 in _context.QcCriteria on q1.CritID equals q3.CritID join q4 in _context.WipInfos on q1.WipNo equals q4.WipNO join q5 in _context.FactoryInfos on q4.Werks equals q5.FactoryID.ToString() join q6 in _context.UserInfoes on q2.Supervisor equals q6.UserNo into cp6 from q6 in cp6.DefaultIfEmpty() join q7 in _context.UserInfoes on q2.Proved equals q7.UserNo into cp7 from q7 in cp7.DefaultIfEmpty() join q8 in _context.UserInfoes on q2.UpdateUserID equals q8.UserID into cp8 from q8 in cp8.DefaultIfEmpty() select new FqcDto { InhouseNo = q1.InhouseNo, SeqID = q1.SeqID, WipNo = q1.WipNo, ItemNo = q1.ItemNo, ModelNo = q1.ModelNo, InhouseQty = q1.InhouseQty, SpecialPo = q2.SpecialPo, StatusName = q1.Status == "P" ? string.IsNullOrWhiteSpace(q2.SpecialPo) ? "允收Pass" : "特採允收" : q1.Status == "R" ? "批退Reject" : "", FqcID = q2.WipNo == null ? 0 : q2.FqcID, InhouseMemo = q1.InhouseMemo, ManualQaMeno = q2.ManualQaMeno, QaMeno = q2.QaMeno, CustomerNo = q4.CustomerNO, CustomerMedical = q4.CustomerMedical, CustomerVIP = q4.CustomerVIP, Werk = q5.FactoryNo + "-" + q5.FactoryNameCh, BIOS = q2.BIOS, CPU = q2.CPU, OS = q2.OS, RAM = q2.RAM, OutfitNo = q2.OutfitNo, Supervisor = q2.Supervisor, Supervisor_Name = q6.UserName, Proved = q2.Proved, Proved_Name = q7.UserName, UpdateUser = q8.UserNo, UpdateUser_Name = q8.UserName, InspectionType = q2.InspectionType, MajorQty = q2.MajorQty, MinorQty = q2.MinorQty, WipECN = q4.GetWipAtt.ECN, EndTime = q2.EndTime.ToString("yyyy/MM/dd"), QuotDescription = q3.GetQcQuot.QuotDescription, CritID = q3.CritID, QcQty = q3.QcQty, AcQty = q3.AcQty, ReQty = q3.ReQty, AQL = q3.GetQcQuot.Aql, AQLType = q3.GetQcQuot.AqlType, QuotName = q3.GetQcQuot.QuotName, PLMECN = q2.PLM_ECN, ECN_Memo = q2.ECN_Memo, Note = q2.Note, InspectionStatus = q2.InspectionStatus, Description = q4.Description, LocationNo = q1.LocationNo, UnitNo = q1.UnitNo }; var query = await q.Distinct().ToListAsync(); query.Select(s => s.InspectionTypeName = System.Enum.IsDefined(typeof(EnumFQC.EnumInspection_Type), s.InspectionType ?? "") ? EnumFQC.GetDisplayName((EnumFQC.EnumInspection_Type)System.Enum.Parse(typeof(EnumFQC.EnumInspection_Type), s.InspectionType)) : "") .ToList(); var FqcDetail = new List(); if (query.Count() != 0) { var Detail = _context.FqcInhouseDetails.Where(w => w.InhouseNo == inhouseNo && w.SeqID == seqid); foreach (var item in Detail) { // 取抽驗結果明細檔資料 var FqcResultDetail = _context.FqcResultDetails.Where(w => w.FqcID == query.FirstOrDefault().FqcID && w.BoxNo == item.SerialNo).ToList(); FqcDetail.Add(new FqcDto.FqcDetailDto { SerialNo = item.SerialNo, Qty = _context.BarcodeInfoes.Where(w => w.BoxNo == item.SerialNo).Count(), HasQty = FqcResultDetail.Count(), PassQty = FqcResultDetail.Where(w => w.StatusNo != "F").Count(), FailQty = FqcResultDetail.Where(w => w.StatusNo == "F").Count() }); // 總數 query.FirstOrDefault().PassQty += FqcResultDetail.Where(w => w.StatusNo != "F").Count(); query.FirstOrDefault().FailQty += FqcResultDetail.Where(w => w.StatusNo == "F").Count(); } // 特殊判斷 // 檢查儀器編號 空白時預設帶N/A query = query.Select(s => { s.OutfitNo = string.IsNullOrEmpty(s.OutfitNo) ? "N/A" : s.OutfitNo; return s; }).ToList(); // Proved 空白時 抓開立入庫單人員 foreach (var item in query.Where(w => string.IsNullOrWhiteSpace(w.Proved))) { var ByQuery = await _context.FqcInhouseMasters.Where(w => w.InhouseNo == item.InhouseNo && w.SeqID == item.SeqID).FirstOrDefaultAsync(); var UserInfo = await _context.UserInfoes.Where(w => w.UserID == ByQuery.CreateUserID).FirstOrDefaultAsync(); if (UserInfo != null) { item.Proved = UserInfo.UserNo; item.Proved_Name = UserInfo.UserName; } } } ResultModel result = new ResultModel(); result.Data = query; foreach (var item in result.Data) { item.fqcDetails = FqcDetail.OrderBy(o => o.SerialNo).ToList(); } return result; } /// /// 更新入庫單 /// /// /// [HttpPut] public async Task> PutFqcInhouseMaster(FqcInhouseMaster fqcInhouseMaster) { ResultModel result = new ResultModel(); _context.Entry(fqcInhouseMaster).State = EntityState.Modified; _context.Entry(fqcInhouseMaster).Property("CreateDate").IsModified = false; _context.Entry(fqcInhouseMaster).Property("CreateUserID").IsModified = false; fqcInhouseMaster.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; } /// /// 更新入庫單 抽驗係數標準 /// /// /// [HttpPut("PutForCritID")] public async Task> PutFqcInhouseMasterForCritID(FqcInhouseMaster fqcInhouseMaster) { ResultModel result = new ResultModel(); // 如果是預設0不更新 if (fqcInhouseMaster.CritID == 0) { result.Msg = "無異動不更新"; result.Success = true; return result; } var data = await _context.FqcInhouseMasters.Where(w => w.InhouseNo == fqcInhouseMaster.InhouseNo && w.SeqID == fqcInhouseMaster.SeqID).FirstOrDefaultAsync(); data.CritID = fqcInhouseMaster.CritID; _context.Entry(data).State = EntityState.Modified; _context.Entry(data).Property("CritID").IsModified = true; try { await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } /// /// 更新入庫別 /// /// /// [HttpPut("PutForLocationNo")] public async Task> PutFqcInhouseMasterForLocationNo(FqcInhouseMaster fqcInhouseMaster) { ResultModel result = new ResultModel(); var data = await _context.FqcInhouseMasters.Where(w => w.InhouseNo == fqcInhouseMaster.InhouseNo && w.SeqID == fqcInhouseMaster.SeqID).FirstOrDefaultAsync(); data.LocationNo = fqcInhouseMaster.LocationNo; _context.Entry(data).State = EntityState.Modified; _context.Entry(data).Property("LocationNo").IsModified = true; try { await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; await _context.Database.ExecuteSqlInterpolatedAsync ($" UPDATE JHAMES.WAREHOUSEING SET LOCATION_NO={fqcInhouseMaster.LocationNo} WHERE INHOUSE_NO={fqcInhouseMaster.InhouseNo} AND SEQ_ID={fqcInhouseMaster.SeqID}"); } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } /// /// 新增入庫單 /// /// /// [HttpPost] public async Task> PostFqcInhouseMaster(FqcInhouseMaster fqcInhouseMaster) { ResultModel result = new ResultModel(); try { _context.FqcInhouseMasters.Add(fqcInhouseMaster); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } /// /// 刪除入庫單 /// /// 入庫單號 /// 順序 /// [HttpDelete("{no}/{seq}")] public async Task> DeleteFqcInhouseMaster(string no, int seq) { ResultModel result = new ResultModel(); var fqcInhouseMaster = await _context.FqcInhouseMasters.FindAsync(no, seq); try { if (fqcInhouseMaster == null) { result.Success = false; result.Msg = "找不到要刪除資料"; } else { _context.FqcInhouseMasters.Remove(fqcInhouseMaster); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } //YIRU ADD ----------------------------------------------------------------------------------------------------------------------------------------------- /// /// FQC查詢 by Table /// /// 內部序號 /// 工單號碼 /// 外箱號碼 /// 入庫單號碼 /// 入庫時間起 /// 入庫時間迄 /// 是否已入庫Y/N/ALL /// 頁數 /// 筆數 /// [HttpGet("FqcInhouseMasterMultiQuery")] public async Task> GetFqcInhouseMasteMultiQuery(string barcodeNo, string wipNo, string boxNo , string inhouseNo, string date_str, string date_end, string status, int page = 0, int limit = 10) { IQueryable q = from q1 in _context.FqcInhouseMasters join q2 in _context.FqcInhouseDetails on new { q1.InhouseNo, q1.SeqID } equals new { q2.InhouseNo, q2.SeqID } join q3 in _context.FqcResultMasters on q1.InhouseNo equals q3.InhouseNo join q4 in _context.WareHouseings on new { q2.InhouseNo, q2.SeqID, q2.SerialNo } equals new { q4.InhouseNo, q4.SeqID, q4.SerialNo } into q2q4 from q402 in q2q4.DefaultIfEmpty() select new FqcInhouseMaster_WareDto { InhouseNo = q1.InhouseNo, SeqID = q1.SeqID, WipNo = q1.WipNo, ItemNo = q1.ItemNo, ModelNo = q1.ModelNo, SerialNo = q2.SerialNo, StatusName = q1.Status, ProTypeName = q1.ProType, CreateDate = q1.CreateDate, EndTime = q3.EndTime, RecordNumber = q402.RecordNumber, RecordDate = q402.Create_Date, LocationNo = q1.LocationNo, }; //q1.Status == "P" ? "允收" : q1.Status == "R" ? "批退" : "未驗收完成", //IQueryable q1 = _context.FqcInhouseDetails; if (!string.IsNullOrWhiteSpace(inhouseNo)) q = q.Where(w => w.RecordNumber == inhouseNo); if (!string.IsNullOrWhiteSpace(wipNo)) q = q.Where(w => w.WipNo == wipNo); if (!string.IsNullOrWhiteSpace(status) || status != "ALL") // q = q.Where(w => w.StatusName == status); { if (status == "N") q = q.Where(w => w.RecordNumber == null); else if (status == "Y") q = q.Where(w => w.RecordNumber != null); } // 優先用內部序號取出包裝序號 if (!string.IsNullOrWhiteSpace(barcodeNo)) { BarcodeInfoesController barcodeInfoesController = new BarcodeInfoesController(_context); var q1 = await barcodeInfoesController.GetBarcodeInfoesByNo(barcodeNo); if (q1.Value.Count() != 0) { boxNo = q1.Value.FirstOrDefault().BoxNo; } } if (!string.IsNullOrWhiteSpace(boxNo)) { q = q.Where(w => w.SerialNo == boxNo); } if (DateTime.TryParse(date_str, out _)) { q = q.Where(w => w.RecordDate >= DateTime.Parse(date_str)); } if (DateTime.TryParse(date_end, out _)) { q = q.Where(w => w.RecordDate <= DateTime.Parse(date_end)); } q = q.Where(w => w.StatusName == "P"); ResultModel result = new ResultModel(); // 紀錄筆數 result.DataTotal = q.Count(); // Table 頁數 if (page > 0) { q = q.Skip((page - 1) * limit).Take(limit); } result.Data = await q.ToListAsync(); // 判斷結束時間 result.Data = result.Data.Select(s => { s.EndTime = s.StatusName == "A" ? null : s.EndTime; return s; }) .ToList(); // 修改狀態名稱 result.Data = result.Data.Select(s => { s.StatusName = s.StatusName == "P" ? "允收" : s.StatusName == "R" ? "批退" : "未驗收完成"; return s; }) .ToList(); result.Data = result.Data.Select(s => { s.BoxQty = _context.BarcodeInfoes.Where(w => w.BoxNo == s.SerialNo).Count().ToString(); return s; }).ToList(); return result; } /// /// FQC查詢 by 安勤 /// /// 內部序號 /// 工單號碼 /// 外箱號碼 /// 入庫單號碼 /// 入庫時間起 /// 入庫時間迄 /// 是否已入庫Y/N/ALL /// 頁數 /// 筆數 /// [HttpGet("FqcInhouseMasterMultiQuery_Avalue")] public async Task> GetFqcInhouseMasteMultiQuery_Avalue(string barcodeNo, string wipNo, string boxNo , string inhouseNo, string date_str, string date_end, string status, int page = 0, int limit = 10) { IQueryable q = from q1 in _context.FqcInhouseMasters join q2 in _context.FqcInhouseDetails on new { q1.InhouseNo, q1.SeqID } equals new { q2.InhouseNo, q2.SeqID } join q3 in _context.FqcResultMasters on q1.InhouseNo equals q3.InhouseNo join q4 in _context.WareHouseings on new { q2.InhouseNo, q2.SeqID, q2.SerialNo } equals new { q4.InhouseNo, q4.SeqID, q4.SerialNo } into q2q4 from q402 in q2q4.DefaultIfEmpty() select new FqcInhouseMaster_WareDto { InhouseNo = q1.InhouseNo, SeqID = q1.SeqID, WipNo = q1.WipNo, ItemNo = q1.ItemNo, ModelNo = q1.ModelNo, SerialNo = q2.SerialNo, StatusName = q1.Status, ProTypeName = q1.ProType, CreateDate = q1.CreateDate, EndTime = q3.EndTime, RecordNumber = q402.RecordNumber, RecordDate = q402.Create_Date, LocationNo = q1.LocationNo, }; //q1.Status == "P" ? "允收" : q1.Status == "R" ? "批退" : "未驗收完成", //IQueryable q1 = _context.FqcInhouseDetails; if (!string.IsNullOrWhiteSpace(inhouseNo)) q = q.Where(w => w.RecordNumber == inhouseNo); if (!string.IsNullOrWhiteSpace(wipNo)) { var wip_NOresult = _context.WipInfos.Where(w => w.RelatedWONO == wipNo); //先抓安勤的工單 再查昶享的工單 q = q.Where(w => wip_NOresult.Any(a => a.WipNO == w.WipNo)); } if (!string.IsNullOrWhiteSpace(status) || status != "ALL") // q = q.Where(w => w.StatusName == status); { if (status == "N") q = q.Where(w => w.RecordNumber == null); else if (status == "Y") q = q.Where(w => w.RecordNumber != null); } // 優先用內部序號取出包裝序號 if (!string.IsNullOrWhiteSpace(barcodeNo)) { BarcodeInfoesController barcodeInfoesController = new BarcodeInfoesController(_context); var q1 = await barcodeInfoesController.GetBarcodeInfoesByNo(barcodeNo); if (q1.Value.Count() != 0) { boxNo = q1.Value.FirstOrDefault().BoxNo; } } if (!string.IsNullOrWhiteSpace(boxNo)) { q = q.Where(w => w.SerialNo == boxNo); } if (DateTime.TryParse(date_str, out _)) { q = q.Where(w => w.RecordDate >= DateTime.Parse(date_str)); } if (DateTime.TryParse(date_end, out _)) { q = q.Where(w => w.RecordDate <= DateTime.Parse(date_end)); } q = q.Where(w => w.StatusName == "P"); q = q.Where(w => w.ItemNo.StartsWith("EV")); ResultModel result = new ResultModel(); // 紀錄筆數 result.DataTotal = q.Count(); // Table 頁數 if (page > 0) { q = q.Skip((page - 1) * limit).Take(limit); } result.Data = await q.ToListAsync(); // 判斷結束時間 result.Data = result.Data.Select(s => { s.EndTime = s.StatusName == "A" ? null : s.EndTime; return s; }) .ToList(); // 修改狀態名稱 result.Data = result.Data.Select(s => { s.StatusName = s.StatusName == "P" ? "允收" : s.StatusName == "R" ? "批退" : "未驗收完成"; return s; }) .ToList(); result.Data = result.Data.Select(s => { s.BoxQty = _context.BarcodeInfoes.Where(w => w.BoxNo == s.SerialNo).Count().ToString(); return s; }).ToList(); return result; } //YIRU End ----------------------------------------------------------------------------------------------------------------------------------------------- //Fion ADD ----------------------------------------------------------------------------------------------------------------------------------------------- /// /// FQC統計報表QRS027 /// /// 工單號碼 /// 料號 /// 製程說明 /// 客戶別 /// 入庫時間起 /// 入庫時間迄 /// 頁數 /// 筆數 /// [HttpGet("FqcInhouseMasterQuery4QRS027")] public async Task> GetFqcInhouseMasterQuery4QRS027(string customerType, string wipNo, string itemNo , string processType, string date_str, string date_end, int page = 0, int limit = 10) { IQueryable q = from q1 in _context.FqcInhouseMasters join q2 in _context.FqcResultMasters on new { q1.WipNo, q1.InhouseNo, q1.SeqID } equals new { q2.WipNo, q2.InhouseNo, q2.SeqID } select new FqcInhouseMaster_QRS027 { EndTimeYMD = q2.EndTime.ToString("yyyy/MM/dd"), EndTime = q2.EndTime, WipNo = q1.WipNo, ItemNo = q1.ItemNo, ProcessType = q1.ItemNo.EndsWith("CPK") ? "精包CPK" : q1.ItemNo.EndsWith("I") ? "單板" : q1.ItemNo.EndsWith("S") ? "單板" : q1.ItemNo.EndsWith("D") ? "單板" : q1.ItemNo.EndsWith("P") ? "單板" : "組裝", InhouseNo = q1.InhouseNo, InhouseId = q1.InhouseNo.Substring(13, 3), CustomerType = q1.ItemNo.Substring(0, 2), SeqID = q1.SeqID, InhouseQty = q1.InhouseQty, QaQty = q2.QaQty, FailQty = q2.FailQty, QaResult = q2.QaResult == "P" ? "允收 PASS" : q2.QaResult == "R" ? "拒收 REJECT" : "未檢驗完畢", }; q = q.Where(w => w.QaResult != "未檢驗完畢"); if (wipNo != null && wipNo != "") { q = q.Where(w => w.WipNo == wipNo); } if (itemNo != null && itemNo != "") { q = q.Where(w => w.ItemNo == itemNo); } if (customerType != null && customerType != "") { q = q.Where(w => w.CustomerType == customerType); } if (processType != null && processType != "") { if (processType == "CPK") { q = q.Where(w => w.ItemNo.EndsWith("CPK")); } if (processType == "ISDP") { q = q.Where(w => w.ItemNo.EndsWith("I") || w.ItemNo.EndsWith("S") || w.ItemNo.EndsWith("D") || w.ItemNo.EndsWith("P")); } if (processType == "OBT") { q = q.Where(w => w.ItemNo.EndsWith("B") || w.ItemNo.EndsWith("O") || w.ItemNo.EndsWith("T")); } } if (date_str != null && date_str != "" && date_end != null && date_end != "") { q = q.Where(w => w.EndTime >= DateTime.Parse(date_str + " 00:00:00") && w.EndTime <= DateTime.Parse(date_end + " 23:59:59")); } q = q.OrderBy(c => c.EndTime).ThenBy(n => n.ItemNo); ResultModel result = new ResultModel(); // 紀錄筆數 result.DataTotal = q.Count(); // Table 頁數 if (page > 0) { q = q.Skip((page - 1) * limit).Take(limit); } result.Data = await q.ToListAsync(); return result; } /// /// FQC統計報表QRS027-NGList /// /// 工單號碼 /// 料號 /// 製程說明 /// 客戶別 /// 入庫時間起 /// 入庫時間迄 /// 頁數 /// 筆數 /// [HttpGet("FqcInhouseMasterQuery4QRS027NGList")] public async Task> GetFqcInhouseMasterQuery4QRS027NGList(string customerType, string wipNo, string itemNo , string processType, string date_str, string date_end, int page = 0, int limit = 10) { IQueryable q = from q1 in _context.FqcInhouseMasters join q2 in _context.FqcResultMasters on new { q1.WipNo, q1.InhouseNo, q1.SeqID } equals new { q2.WipNo, q2.InhouseNo, q2.SeqID } join q3 in _context.FqcResultDetails on q2.FqcID equals q3.FqcID join q4 in _context.NGReasons on q3.NgReasonNo equals q4.NGReasonNo select new FqcInhouseMaster_QRS027 { CreatDateYMD = q3.CreateDate.ToString("yyyy/MM/dd"), CreateDate = q3.CreateDate, WipNo = q1.WipNo, CustomerType = q1.ItemNo.Substring(0, 2), ItemNo = q1.ItemNo, BarcodeNo = q3.BarcodeNo, QaResult = q3.StatusNo, ProcessType = q1.ItemNo.EndsWith("CPK") ? "精包CPK" : q1.ItemNo.EndsWith("I") ? "單板" : q1.ItemNo.EndsWith("S") ? "單板" : q1.ItemNo.EndsWith("D") ? "單板" : q1.ItemNo.EndsWith("P") ? "單板" : "組裝", NGReasonDesc = q4.NGReasonDesc, PartPosition = q3.PartPosition, NGMemo = q3.NgMemo, FailQty = q2.FailQty, }; if (wipNo != null && wipNo != "") { q = q.Where(w => w.WipNo == wipNo); } if (itemNo != null && itemNo != "") { q = q.Where(w => w.ItemNo == itemNo); } if (customerType != null && customerType != "") { q = q.Where(w => w.CustomerType == customerType); } if (processType != null && processType != "") { if (processType == "CPK") { q = q.Where(w => w.ItemNo.EndsWith("CPK")); } if (processType == "ISDP") { q = q.Where(w => w.ItemNo.EndsWith("I") || w.ItemNo.EndsWith("S") || w.ItemNo.EndsWith("D") || w.ItemNo.EndsWith("P") || w.ItemNo.EndsWith("R")); } if (processType == "OBT") { q = q.Where(w => w.ItemNo.EndsWith("B") || w.ItemNo.EndsWith("O") || w.ItemNo.EndsWith("T")); } } if (date_str != null && date_str != "" && date_end != null && date_end != "") { DateTime startDate = DateTime.Parse(date_str + " 00:00:00"); DateTime endDate = DateTime.Parse(date_end + " 23:59:59"); q = q.Where(w => w.CreateDate >= startDate && w.CreateDate <= endDate); } q = q.Where(w => w.QaResult == "F"); // 使用 ToListAsync 獲取資料 var dataList = await q.ToListAsync(); // 在記憶體中進行分組操作 var groupedData = dataList .GroupBy(w => new { w.WipNo, w.CustomerType, w.QaResult, w.ProcessType, w.NGReasonDesc, w.PartPosition, w.NGMemo, w.FailQty }) .Select(g => g.First()) .ToList(); ResultModel result = new ResultModel(); // 紀錄筆數 result.DataTotal = groupedData.Count(); result.Data = groupedData; if (result == null) { result.Msg = "查無資料"; result.Success = false; return result; } result.Success = true; result.Msg = "OK"; return result; } /// /// FQC統計報表QRS027-NGDetail /// /// 工單號碼 /// 料號 /// [HttpGet("FqcInhouseMasterQuery4QRS027NGDetail")] //public async Task> GetFqcInhouseMasterQuery4QRS027NGDetail(string wipNo, string itemNo) public async Task> GetFqcInhouseMasterQuery4QRS027NGDetail(string wipNo, string itemNo) { IQueryable q = from q1 in _context.FqcInhouseMasters join q2 in _context.FqcResultMasters on new { q1.WipNo, q1.InhouseNo, q1.SeqID } equals new { q2.WipNo, q2.InhouseNo, q2.SeqID } join q3 in _context.FqcResultDetails on q2.FqcID equals q3.FqcID join q4 in _context.NGReasons on q3.NgReasonNo equals q4.NGReasonNo select new FqcInhouseMaster_QRS027 { WipNo = q1.WipNo, CustomerType = q1.ItemNo.Substring(0, 2), ItemNo = q1.ItemNo, BarcodeNo = q3.BarcodeNo, QaResult = q3.StatusNo, NGReasonDesc = q4.NGReasonDesc, PartPosition = q3.PartPosition, NGMemo = q3.NgMemo, CreatDateYMD = q3.CreateDate.ToString("yyyy/MM/dd"), CreateDate = q3.CreateDate, }; if (wipNo != null && wipNo != "") { q = q.Where(w => w.WipNo == wipNo); } if (itemNo != null && itemNo != "") { q = q.Where(w => w.ItemNo == itemNo); } q = q.Where(w => w.QaResult == "F"); // 使用 ToListAsync 獲取資料 var dataList = await q.ToListAsync(); // 在記憶體中進行分組操作 var groupedData = dataList .GroupBy(w => new { w.WipNo, w.CustomerType, w.ItemNo, w.BarcodeNo, w.QaResult, w.NGReasonDesc, w.PartPosition, w.NGMemo }) .Select(g => g.First()) .ToList(); ResultModel result = new ResultModel(); // 紀錄筆數 result.DataTotal = groupedData.Count(); result.Data = groupedData; if (result == null) { result.Msg = "查無資料"; result.Success = false; return result; } result.Success = true; result.Msg = "OK"; return result; } //Fion End ----------------------------------------------------------------------------------------------------------------------------------------------- private bool FqcInhouseMasterExists(string id) { return _context.FqcInhouseMasters.Any(e => e.InhouseNo == id); } } }