using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using AMESCoreStudio.WebApi.Models.AMES; using AMESCoreStudio.CommonTools.Result; using AMESCoreStudio.WebApi.DTO.AMES; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace AMESCoreStudio.WebApi.Controllers.AMES { [Route("api/[controller]")] [ApiController] public class WipLabelController : ControllerBase { private readonly AMESContext _context; /// <summary> /// /// </summary> /// <param name="context"></param> public WipLabelController(AMESContext context) { _context = context; } /// <summary> /// 工單投產纪錄資料文件 /// </summary> /// <returns></returns> [HttpGet] public async Task<ActionResult<IEnumerable<WipLabel>>> GetWipLabel() { IQueryable<WipLabel> q = _context.WipLabels; q = q.OrderBy(p => p.WipNO); var WipLabel = await q.ToListAsync(); return WipLabel; } [HttpGet("{id}")] public async Task<ActionResult<WipLabel>> GetWipLabel(string id) { IQueryable<WipLabel> q = _context.WipLabels; var WipLabel = await q.Where(p => p.WipNO == id).FirstOrDefaultAsync(); //if (WipLabel == null) //{ // return NotFound(); //} return WipLabel; } ///// <summary> ///// SOP文件查詢 ///// </summary> ///// <param name="itemNo">料號</param> ///// <param name="unitNo">生產單位</param> ///// <param name="fileName">檔案名稱</param> ///// <param name="state">狀態</param> ///// <param name="date_str">建立日期起</param> ///// <param name="date_end">建立日期迄</param> ///// <returns></returns> //[Route("[action]")] //[HttpGet] //public async Task<ResultModel<WipLabelDto>> GetWipLabelQuery(string itemNo = null, string unitNo = null // , string fileName = null, string state = null, string date_str = null, string date_end = null) //{ // IQueryable<WipLabel> q = _context.WipLabels; // if (!string.IsNullOrWhiteSpace(itemNo)) // q = q.Where(w => w.ItemNo == itemNo); // if (!string.IsNullOrWhiteSpace(unitNo)) // q = q.Where(w => w.UnitNo == unitNo); // if (!string.IsNullOrWhiteSpace(fileName)) // q = q.Where(w => w.FileName.Contains(fileName)); // if (!string.IsNullOrWhiteSpace(state)) // q = q.Where(w => w.State == state); // DateTime dateValue; // if (DateTime.TryParse(date_str, out dateValue)) // { // q = q.Where(w => w.CreateDate >= DateTime.Parse(date_str)); // } // if (DateTime.TryParse(date_end, out dateValue)) // { // q = q.Where(w => w.CreateDate <= DateTime.Parse(date_end)); // } // ResultModel<WipLabelDto> result = new ResultModel<WipLabelDto>(); // result.Data = await q.Select(s => new WipLabelDto // { // WipLabelID = s.WipLabelID, // ItemNo = s.ItemNo, // UnitName = s.FactoryUnit.UnitName, // FileName = s.FileName, // FilePath = s.FilePath, // State = s.State == "Y" ? "使用中" : "停用", // NewName = s.NewName, // CreateDate = s.CreateDate, // CreateUserID = s.CreateUserID, // UpdateDate = s.UpdateDate, // UpdateUserID = s.UpdateUserID // }).ToListAsync(); // return result; //} [HttpPost] public async Task<ResultModel<WipLabel>> PostWipLabel([FromBody] WipLabel wipLabel) { ResultModel<WipLabel> result = new ResultModel<WipLabel>(); _context.WipLabels.Add(wipLabel); try { await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } /// <summary> /// 更新工單標籤 /// </summary> /// <param name="wipLabel"></param> /// <returns></returns> [HttpPut] public async Task<ResultModel<WipLabel>> PutWipLabel(WipLabel wipLabel) { ResultModel<WipLabel> result = new ResultModel<WipLabel>(); var wipNo = wipLabel.WipNO; try { if (_context.WipLabels.Any(e => e.WipNO == wipNo)) { _context.Entry(wipLabel).State = EntityState.Modified; _context.Entry<WipLabel>(wipLabel).Property("CreateDate").IsModified = false; _context.Entry<WipLabel>(wipLabel).Property("CreateUserID").IsModified = false; wipLabel.UpdateDate = DateTime.Now; } else { _context.WipLabels.Add(wipLabel); } await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } // DELETE api/<WipLabelController>/5 [HttpDelete("{id}")] public void Delete(int id) { } } }