Ames 昶亨 專案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
8.5 KiB

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
{
/// <summary>
/// 途程除外站點维护
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class ExceptStationsController : ControllerBase
{
private readonly AMESContext _context;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public ExceptStationsController(AMESContext context)
{
_context = context;
}
/// <summary>
/// 获取全部途程除外站點
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ResultModel<ExceptStations>> GetExceptStations(int page = 0, int limit = 10)
{
IQueryable<ExceptStations> q = _context.ExceptStations;
ResultModel<ExceptStations> result = new ResultModel<ExceptStations>();
// 紀錄筆數
result.DataTotal = q.Count();
// Table 頁數
if (page > 0)
{
q = q.Skip((page - 1) * limit).Take(limit);
}
result.Data = await q.ToListAsync();
return result;
}
/// <summary>
/// 用ID获取该途程除外站點
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<ActionResult<IEnumerable<ExceptStations>>> GetExceptStations(int id)
{
IQueryable<ExceptStations> q = _context.ExceptStations;
q = q.Where(p => p.ExceptStationsID.Equals(id));
var ExceptStations = await q.ToListAsync();
if (ExceptStations == null)
{
return NotFound();
}
return ExceptStations;
}
/// <summary>
/// 用生產單位获取该途程除外站點
/// </summary>
/// <param name="id">報工生產單位</param>
/// <returns></returns>
[HttpGet("Unit/{id}")]
public async Task<ActionResult<IEnumerable<ExceptStations>>> GetExceptStationsbyUnit(int id)
{
IQueryable<ExceptStations> 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;
}
/// <summary>
/// 用生產單位获取该途程除外站點
/// </summary>
/// <param name="id">報工生產單位</param>
/// <returns></returns>
[HttpGet("MultiUnit/{id}")]
public async Task<ResultModel<dynamic>> 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<dynamic> result = new ResultModel<dynamic>();
// 紀錄筆數
result.DataTotal = q.Count();
// Table 頁數
if (page > 0)
{
q = q.Skip((page - 1) * limit).Take(limit);
}
result.Data = await q.ToListAsync();
return result;
}
/// <summary>
/// 更新條途程除外站點
/// </summary>
/// <param name="id"></param>
/// <param name="ExceptStations"></param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task<ResultModel<ExceptStations>> PutExceptStations(int id, [FromBody] ExceptStations ExceptStations)
{
ResultModel<ExceptStations> result = new ResultModel<ExceptStations>();
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;
}
}
/// <summary>
/// 新增途程除外站點
/// </summary>
/// <param name="ExceptStations"></param>
/// <returns></returns>
[HttpPost]
public async Task<ResultModel<ExceptStations>> PostExceptStations(ExceptStations ExceptStations)
{
ResultModel<ExceptStations> result = new ResultModel<ExceptStations>();
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;
}
/// <summary>
/// 停用/啟用途程除外站點
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("{id}/{uid}")]
public async Task<ResultModel<ExceptStations>> DeleteExceptStations(int id,int uid)
{
ResultModel<ExceptStations> result = new ResultModel<ExceptStations>();
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);
}
}
}