using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AMESCoreStudio.WebApi.Models.SYS; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Localization; using Microsoft.AspNetCore.Http; using AMESCoreStudio.Web.Models; using AMESCoreStudio.CommonTools.Result; using static AMESCoreStudio.Web.ViewModels.LAB.LAB003ViewModel; using AMESCoreStudio.WebApi.Models.AMES; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Microsoft.AspNetCore.Mvc.Rendering; using AMESCoreStudio.WebApi.DTO.AMES; using AMESCoreStudio.Web.ViewModels.LAB; using System.Dynamic; using System.IO; using Microsoft.AspNetCore.Hosting; namespace AMESCoreStudio.Web.Controllers { public class LABController : Controller { public readonly ISYS _sysApi; public readonly ILAB _labApi; public readonly IPCS _pcsApi; private readonly IWebHostEnvironment _env; public LABController(ILogger logger, ILAB labApi, ISYS sysApi, IPCS pcsApi, IWebHostEnvironment env, IStringLocalizer sharedLocalizer) { _sysApi = sysApi; _labApi = labApi; _pcsApi = pcsApi; _env = env; } private void GetFlagList() { var FlagList = new List(); FlagList.Add(new SelectListItem("Y", "Y")); FlagList.Add(new SelectListItem("N", "N")); ViewBag.FlagList = FlagList; } public void GetUserID() { var userID = ""; HttpContext.Request.Cookies.TryGetValue("UserID", out userID); int user_id = 0; if (userID != null) { if (int.Parse(userID.ToString()) >= 0) { user_id = int.Parse(userID.ToString()); } } ViewBag.UserID = user_id; } #region LAB001工作群組維護相關 public IActionResult LAB001() { return View(); } //新增頁面 public IActionResult LAB001C() { GetUserID(); GetFlagList(); return View(); } //修改页面 [HttpGet] public async Task LAB001U(int id) { GetUserID(); GetFlagList(); var result = await _labApi.GetLabelParam(id); if (result.LABEL_FIELD_ID == 0) { return View(); } return View(result); } //頁面提交,id=0 添加,id>0 修改 [HttpPost] public async Task LAB001CSaveAsync(LabelParam model) { if (ModelState.IsValid) { IResultModel result; result = await _labApi.PostLabelParam(JsonConvert.SerializeObject(model)); if (result.Success) //yiru modify 2022-09-27 { var _msg = "添加成功!"; return RedirectToAction("Refresh", "Home", new { msg = _msg }); } else { ModelState.AddModelError("error", result.Msg); } } return View("LAB001C", model); } public async Task LAB001USaveAsync(LabelParam model) { if (ModelState.IsValid) { IResultModel result; result = await _labApi.PutLabelParam(model.LABEL_FIELD_ID, JsonConvert.SerializeObject(model)); if (result.Success) { var _msg = "修改成功!"; return RedirectToAction("Refresh", "Home", new { msg = _msg }); } else { ModelState.AddModelError("error", result.Msg); } } return View("LAB001U", model); } [ResponseCache(Duration = 0)] [HttpGet] public async Task GetLabParamsAsync(string status = "*" ,int page = 0, int limit = 10) { var result = await _labApi.GetLabParamsByStatus(status,page, limit); if (result.Data.Count() > 0) { return Json(new Table() { code = 0, msg = "", data = result.Data, count = result.DataTotal }); } return Json(new Table() { count = 0, data = null }); } #endregion #region LAB002 標籤樣板維護 /// /// /// /// /// /// /// public async Task LAB002Async(string LabelMatnr, int page = 0, int limit = 10) { return View(); } [ResponseCache(Duration = 0)] [HttpGet] public async Task GetLabelTemplateDtoAsync(string LabelMatnr, int page = 0, int limit = 10) { var result = await _labApi.LabelTemplatebyMatnr(LabelMatnr, page, limit); if (result.DataTotal > 0) { return Json(new Table() { code = 0, msg = "", data = result.Data, count = result.DataTotal }); } return Json(new Table() { count = 0, data = null }); } //新增頁面 public IActionResult LAB002C() { GetUserID(); @ViewBag.MasterID = 0; return View(); } public async Task CheckLabelMatnrAsync(string LabelMatnr) { //判斷為E209開頭的料號才可輸入 if (LabelMatnr.StartsWith("E209")) { var PlmMeterial = await _pcsApi.GetPlmMeterialInfo(LabelMatnr); if (PlmMeterial.Count > 0) { var Master = await _labApi.LabelTemplatebyMatnr(LabelMatnr,0,10); if (Master.DataTotal > 0) return Json(new Table() { count = 0, msg = "料號已存在", data = null }); else return Json(new Table() { code = 0, msg = "", data = PlmMeterial, count = 1 }); } else return Json(new Table() { count = 0, msg = "料號不存在", data = null }); } else return Json(new Table() { count = 0, msg = "請輸入Label料號", data = null }); } public async Task GetLabelTemplatebyMatnrAsync(int id) { var result = await _labApi.GetLabelTemplatebyMatnr(id); // var result = await _labApi.GetLabelParams(); List models = new List(); if (result.Count > 0) { for (int i = 0; i < result.Count; i += 3) { Label002Model newItem = new Label002Model(); newItem.LABEL_FIELD_ID1 = result[i].labeL_FIELD_ID; newItem.LABEL_FIELD_NAME1 = result[i].labeL_FIELD_NAME; newItem.DATA_V1 = result[i].data; if (i + 1 < result.Count) { newItem.LABEL_FIELD_ID2 = result[i + 1].labeL_FIELD_ID; newItem.LABEL_FIELD_NAME2 = result[i + 1].labeL_FIELD_NAME; newItem.DATA_V2 = result[i + 1].data; } if (i + 2 < result.Count) { newItem.LABEL_FIELD_ID3 = result[i + 2].labeL_FIELD_ID; newItem.LABEL_FIELD_NAME3 = result[i + 2].labeL_FIELD_NAME; newItem.DATA_V3 = result[i + 2].data; } models.Add(newItem); } return Json(new Table() { code = 0, msg = "", data = models, count = models.Count }); } return Json(new Table() { count = 0, data = null }); } //頁面提交,id=0 添加,id>0 修改 [HttpPost] public async Task LAB002CSaveAsync(LabelTemplateMaster model, IFormFile formFile) { if (ModelState.IsValid) { IResultModel result; string FileName = string.Empty; string NewName = string.Empty; string FilePath = string.Empty; if (formFile != null) { if (formFile.Length > 0) { //取得使用者上傳檔案的原始檔名 FileName = Path.GetFileName(formFile.FileName); //取原始檔名中的副檔名 var fileExt = Path.GetExtension(FileName); //為避免使用者上傳的檔案名稱發生重複,重新給一個亂數名稱 NewName = Path.GetRandomFileName() + fileExt; //指定要寫入的路徑、檔名和副檔名 FilePath = $"\\LABFile\\";//本機目錄 if (!System.IO.Directory.Exists(_env.WebRootPath + FilePath)) { System.IO.Directory.CreateDirectory(_env.WebRootPath + FilePath); } using (var stream = new FileStream(_env.WebRootPath + FilePath + NewName, FileMode.Create)) { await formFile.CopyToAsync(stream); } model.IMAGE_NAME = FilePath+ NewName; } } else { ModelState.AddModelError("error", "請選擇要上傳的圖片檔案"); } result = await _labApi.PostLabelTemplateMaster(JsonConvert.SerializeObject(model)); if (result.Success) { //var _msg = "添加成功!"; return Json(new Result() { success = true, msg = result.Msg }); } else { ModelState.AddModelError("error", result.Msg); } } return Json(new Result() { success = false, msg = "缺少必填資料!!" }); } public async Task DeleteLabelTemplateDetailAsync(int id) { var result = await _labApi.DeleteLabelTemplateDetail(id); if (result.Success) { return Json(new Result() { success = true, msg = result.Msg }); } else { return Json(new Result() { success = false, msg = "資料有誤!!" }); } } public async Task LAB002CListSaveAsync(LabelTemplateDetail model) { var result = await _labApi.PostLabelTemplateDetail(JsonConvert.SerializeObject(model)); if (result.Success) { //var _msg = "添加成功!"; return Json(new Result() { success = true, msg = result.Msg }); } else { return Json(new Result() { success = false, msg = "資料有誤!!" }); } } //修改页面 [HttpGet] public async Task LAB002UAsync(int id) { GetUserID(); var result = await _labApi.GetLabelTemplateMaster(id); if (result.LABEL_FILE == null) { return View(); } return View(result); } //頁面提交,id=0 添加,id>0 修改 [HttpPost] public async Task LAB002USaveAsync(LabelTemplateMaster model, IFormFile formFile) { if (ModelState.IsValid) { IResultModel result; string FileName = string.Empty; string NewName = string.Empty; string FilePath = string.Empty; if (formFile != null) { if (formFile.Length > 0) { //取得使用者上傳檔案的原始檔名 FileName = Path.GetFileName(formFile.FileName); //取原始檔名中的副檔名 var fileExt = Path.GetExtension(FileName); //為避免使用者上傳的檔案名稱發生重複,重新給一個亂數名稱 NewName = Path.GetRandomFileName() + fileExt; //指定要寫入的路徑、檔名和副檔名 FilePath = $"\\LABFile\\";//本機目錄 if (!System.IO.Directory.Exists(_env.WebRootPath + FilePath)) { System.IO.Directory.CreateDirectory(_env.WebRootPath + FilePath); } using (var stream = new FileStream(_env.WebRootPath + FilePath + NewName, FileMode.Create)) { await formFile.CopyToAsync(stream); } model.IMAGE_NAME = FilePath + NewName; } } result = await _labApi.PutLabeTemplateMaster(JsonConvert.SerializeObject(model)); if (result.Success) { //var _msg = "添加成功!"; return Json(new Result() { success = true, msg = result.Msg }); } else { ModelState.AddModelError("error", result.Msg); } } return Json(new Result() { success = false, msg = "資料有誤!!" }); //else //{ // ModelState.AddModelError("error", "缺少必填資料"); // return View("LAB002C", model); //} } public async Task LAB002UListSaveAsync(LabelTemplateDetail model) { var result = await _labApi.PostLabelTemplateDetail(JsonConvert.SerializeObject(model)); if (result.Success) { //var _msg = "添加成功!"; return Json(new Result() { success = true, msg = result.Msg }); } else { return Json(new Result() { success = false, msg = "資料有誤!!" }); } } [HttpGet] public async Task LAB002VAsync(int id) { GetUserID(); var result = await _labApi.GetLabelTemplateMaster(id); if (result.LABEL_FILE == null) { return View(); } return View(result); } #endregion #region Lab003 public async Task LAB003(string mat) { var id = GetLogInUserID(); ViewBag.UserID = id; var result = await _sysApi.GetUserInfo(id); var name = result[0].UserName; ViewBag.UserName = name; return View(); } public async Task LAB004() { int id = GetLogInUserID(); var result = await _sysApi.GetUserInfo(id); var name = result[0].UserNo; ViewBag.userno = name; return View(); } [HttpGet] public async Task LAB003U(string OrderNo,string Operate) { LabelItemReqModel model = new LabelItemReqModel(); model = await _labApi.GetLabelItemReq(OrderNo); ViewBag.Operate = Operate; return View(model); } [HttpGet] public async Task GetLab004(string OrderNo, string ModelNo, DateTime? strdate, DateTime? enddate, string UserNo,string Status) { List xx = new List(); UserInfo User = new UserInfo(); if (UserNo != null) User = await _sysApi.GetUserInfoByUserNo(UserNo); var result = await _labApi.GetLabelItemMaster(OrderNo, ModelNo, strdate, enddate, User.UserID, Status); if (result != null) { return Json(new Table() { code = 0, msg = "", data = result.Data, count = result.Data.Count() }); } return Json(new Table() { count = 0, data = null }); } public async Task LAB003P(string LabMat) { var result = await _labApi.GetLabelPicture(LabMat); var x = result.Data.FirstOrDefault(); ViewBag.Picture = x; return View(result.Data); } [HttpPost] public async Task SaveParamLAB003([FromBody] List Lab) { IResultModel result; foreach (var name in Lab) { name.UserId = GetLogInUserID(); } result = await _labApi.PostLabelItemMaster(JsonConvert.SerializeObject(Lab)); return Json(new Result() { success = result.Success, msg = result.Msg }); } [HttpPost] public async Task UpdateParamLAB003U([FromBody] List Lab) { IResultModel result; foreach (var name in Lab) { name.UserId = GetLogInUserID(); } result = await _labApi.UpdateLabelItem(JsonConvert.SerializeObject(Lab)); return Json(new Result() { success = result.Success, msg = result.Msg }); } [HttpGet] public async Task GetLabelField(string Labmat,string ItemNo) { ResultModel result = new ResultModel(); result = await _labApi.GetLabelField(Labmat, ItemNo); if(result.Data.Count()>0) return Json(new Result() { success = result.Success, msg = result.Msg, data = result.Data }); else return Json(new Result() { success = false, msg = result.Msg, data = null }); } [HttpGet] public async Task GetOrderSn(string OrderNo) { int Sn = 1; ResultModel result = new ResultModel(); result = await _labApi.GetLabelItemMaster(OrderNo, null, null, null, -1,"A"); if (result.Data.Any()) { var r2 = result.Data.Max(x => x.ordeR_NO).ToString(); Sn = int.Parse(r2.Substring(7)) + 1; } string sSn = Sn.ToString("000"); return sSn; } [HttpGet] public async Task GetSn(string ItemNo) { ResultModel result = new ResultModel(); result = await _labApi.GetWipBarcodeOtherSN(ItemNo); return Json(new Result() { success = result.Success, msg = result.Msg, data = result.Data.FirstOrDefault() }); } [HttpGet] public async Task GetLabel(string Model) { ResultModel result = new ResultModel(); result = await _labApi.GetLabel(Model); return Json(new Result() { success = result.Success, msg = result.Msg, data = result.Data }); } [HttpGet] public async Task GetMetInfo(string Model) { ResultModel result = new ResultModel(); result = await _labApi.GetMetInfo(Model); return Json(new Result() { success = result.Success, msg = result.Msg, data = result.Data }); } [HttpGet] public async Task GetReqData(int OrderId) { ResultModel result = new ResultModel(); result = await _labApi.GetLabelParamReq(OrderId); return Json(new Result() { success = result.Success, msg = result.Msg, data = result.Data }); } [HttpGet] public async Task FinishReqData(string OrderNo) { ResultModel result = new ResultModel(); result = await _labApi.FinishReqData(OrderNo); return result.Success; } [HttpGet] public async Task LabelApplyNotify(string OrderNo) { ResultModel result = new ResultModel(); await _labApi.LabelApplyNotify(OrderNo); result.Success = true; return result.Success; } public int GetLogInUserID() { int user_id = 0; HttpContext.Request.Cookies.TryGetValue("UserID", out string userID); if (userID != null) { if (int.Parse(userID.ToString()) >= 0) { user_id = int.Parse(userID.ToString()); } } return user_id; } #endregion } }