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.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.AMES { /// /// 途程除外站點维护 /// [Route("api/[controller]")] [ApiController] public class ExceptStationsController : ControllerBase { private readonly AMESContext _context; /// /// /// /// public ExceptStationsController(AMESContext context) { _context = context; } /// /// 获取全部途程除外站點 /// /// [HttpGet] public async Task> GetExceptStations(int page = 0, int limit = 10) { IQueryable q = _context.ExceptStations; 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; } /// /// 用ID获取该途程除外站點 /// /// /// [HttpGet("{id}")] public async Task>> GetExceptStations(int id) { IQueryable q = _context.ExceptStations; q = q.Where(p => p.ExceptStationsID.Equals(id)); var ExceptStations = await q.ToListAsync(); if (ExceptStations == null) { return NotFound(); } return ExceptStations; } /// /// 用生產單位获取该途程除外站點 /// /// 報工生產單位 /// [HttpGet("Unit/{id}")] public async Task>> GetExceptStationsbyUnit(int id) { IQueryable q = _context.ExceptStations; if (id != -99) { q = q.Where(p => p.UnitNo.Equals(id)); } var ExceptStations = await q.ToListAsync(); if (ExceptStations == null) { return NotFound(); } return ExceptStations; } /// /// 用生產單位获取该途程除外站點 /// /// 報工生產單位 /// [HttpGet("MultiUnit/{id}")] public async Task> GetExceptStationsMulti(string id,int page = 0, int limit = 10) { var q = from q1 in _context.ExceptStations join q3 in _context.Stationses on q1.StationID equals q3.StationID join q2 in _context.FactoryUnits on q1.UnitNo equals q2.UnitNo select new { ExceptStationsID = q1.ExceptStationsID, StationID = q1.StationID, ExceptStationsName = q3.StationName, UnitNo = q1.UnitNo, UnitName = q2.UnitName, CreateUserID = q1.CreateUserID, CreateDate = q1.CreateDate, UpdateDate = q1.UpdateDate, statusNo = q1.StatusNo }; if (id != "0") { q = q.Where(p => p.UnitNo.Equals(id)); } 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; } /// /// 更新條途程除外站點 /// /// /// /// [HttpPut("{id}")] public async Task> PutExceptStations(int id, [FromBody] ExceptStations ExceptStations) { ResultModel result = new ResultModel(); if (id != ExceptStations.ExceptStationsID) { result.Success = false; result.Msg = "序號錯誤"; return result; } ExceptStations.UpdateDate = DateTime.Now; _context.Entry(ExceptStations).State = EntityState.Modified; try { await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; return result; } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; return result; //throw; } } /// /// 新增途程除外站點 /// /// /// [HttpPost] public async Task> PostExceptStations(ExceptStations ExceptStations) { ResultModel result = new ResultModel(); if (ExceptStationExists(ExceptStations)) { result.Success = false; result.Msg = "途程站別重覆!!"; return result; } Helper helper = new Helper(_context); ExceptStations.ExceptStationsID = helper.GetIDKey("ExceptStations_ID").Result; ExceptStations.CreateDate = DateTime.Now; ExceptStations.UpdateDate = DateTime.Now; _context.ExceptStations.Add(ExceptStations); try { await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } catch (DbUpdateException ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } /// /// 停用/啟用途程除外站點 /// /// /// [HttpDelete("{id}/{uid}")] public async Task> DeleteExceptStations(int id,int uid) { ResultModel result = new ResultModel(); var workingunit = await _context.ExceptStations.Where(m => m.ExceptStationsID == id).FirstOrDefaultAsync(); if (workingunit == null) { result.Success = false; result.Msg = "序號錯誤"; return result; } ////// var workingunitNew = new ExceptStations(); workingunitNew = workingunit; _context.Entry(workingunitNew).State = EntityState.Modified; if (workingunit.StatusNo == "A") workingunitNew.StatusNo = "S"; else workingunitNew.StatusNo = "A"; workingunitNew.UpdateDate = DateTime.Now; workingunitNew.UpdateUserID = uid; try { await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; } catch (Exception ex) { result.Success = false; result.Msg = ex.InnerException.Message; } return result; } private bool ExceptStationExists(ExceptStations model) { return _context.ExceptStations.Any(e => e.StationID == model.StationID); } } }