Files
Web_SupplierManager_Prod/SupplierManager/Controllers/AasProjectShenpisController.cs
2025-11-20 09:14:00 +08:00

140 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SupplierManager.Models;
using ViewModels;
namespace SupplierManager.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class AasProjectShenpisController : ControllerBase
{
private readonly AgentApprovalSystemContext _context;
public AasProjectShenpisController(AgentApprovalSystemContext context)
{
_context = context;
}
// GET: api/AasProjectShenpis
[HttpPost()]
[Authorize()]
public async Task<ReturnInfo> GetAasProjectShenpis([FromBody] QueryAll_Or_Single_ProjectInfo S)
{
ReturnInfo r = new ReturnInfo();
try
{
if (S.IsAll)
{
r.response = await _context.AasProjectShenpis.ToListAsync();
r.isok = true;
}
else
{
var aasProjectShenpi = await _context.AasProjectShenpis.FindAsync(S.ID);
r.isok = true;
r.response = aasProjectShenpi;
}
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
return r;
}
[HttpPost()]
[Authorize()]
public async Task<ReturnInfo> EditAasProjectShenpi([FromBody] AasProjectShenpi aasProjectShenpi)
{
ReturnInfo r = new ReturnInfo();
try
{
aasProjectShenpi.Updatetime = DateTime.Now;
_context.Entry(aasProjectShenpi).State = EntityState.Modified;
await _context.SaveChangesAsync();
r.isok = true;
}
catch (Exception ex)
{
r.isok = false;
}
return r;
}
[HttpPost()]
[Authorize()]
public async Task<ReturnInfo> AddAasProjectShenpi(AasProjectShenpi aasProjectShenpi)
{
ReturnInfo r = new ReturnInfo();
r.isok = false;
try
{
var Q = _context.AasProjectShenpis.Where(x => x.ProjectCode.Equals(aasProjectShenpi.ProjectCode));
if (Q.Count() > 0)
{
var Q1 = Q.OrderByDescending(A => A.ShenpiNumber).FirstOrDefault();
aasProjectShenpi.ShenpiNumber = Q1?.ShenpiNumber + 1;
}
else
{
aasProjectShenpi.ShenpiNumber = 0;
}
aasProjectShenpi.Updatetime = DateTime.Now;
_context.AasProjectShenpis.Add(aasProjectShenpi);
await _context.SaveChangesAsync();
r.isok = true;
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
return r;
}
// DELETE: api/AasProjectShenpis/5
[HttpPost()]
[Authorize()]
public async Task<ReturnInfo> DeleteAasProjectShenpi(long id)
{
ReturnInfo r = new ReturnInfo();
try
{
var aasProjectShenpi = await _context.AasProjectShenpis.FindAsync(id);
if (aasProjectShenpi == null)
{
r.isok = false;
r.message = "找不到相应的数据";
}
_context.AasProjectShenpis.Remove(aasProjectShenpi);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
r.isok = false;
r.message = ex.Message;
}
return r;
}
private bool AasProjectShenpiExists(long id)
{
return _context.AasProjectShenpis.Any(e => e.Id == id);
}
}
}