asg_backend/asg_form/Controllers/auditAndFilingController.cs
2024-11-11 16:44:05 +08:00

190 lines
7.6 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Manganese.Array;
namespace asg_form.Controllers {
public class FileDB
{
public string proj_name { get; set; }
public string proj_no { get; set; }
public string? budget_use { get; set; }
public string? budget_name { get; set; }
public string biz_type { get; set; }
public int? budget_id { get; set; }
public string start_time { get; set; }
public decimal budget_money { get; set; }
public int start_person_id { get; set; }
public string start_person { get; set; }
public string now_auth_person { get; set; }
public int now_auth_person_id { get; set; }
public string id { get; set; }
public string description { get; set; }
public string reason { get; set; }
public string supplementary_info { get; set; }
public string status { get; set; }
}
public class FileFront
{
public string projName { get; set; }
public string projNo { get; set; }
public string budgetUse { get; set; }
public string budgetName { get; set; }
public string bizType { get; set; }
public int budgetId { get; set; }
public string startTime { get; set; }
public decimal budgetMoney { get; set; }
public int startPersonId { get; set; }
public string startPerson { get; set; }
public string nowAuthPerson { get; set; }
public int nowAuthPersonId { get; set; }
public string Id { get; set; }
public string description { get; set; }
public string reason { get; set; }
public string supplementaryInfo { get; set; }
public string status { get; set; }
}
public class auditAndFilingController : ControllerBase
{
private readonly RoleManager<Role> roleManager;
private readonly UserManager<User> userManager;
public auditAndFilingController(
RoleManager<Role> roleManager, UserManager<User> userManager)
{
this.roleManager = roleManager;
this.userManager = userManager;
}
/// <summary>
/// 新增/修改审批
/// </summary>
/// <param name="auditinfo"></param>
/// <returns></returns>
[Route("api/v1/admin/AuditPost")]
[HttpPost]
[Authorize]
public async Task<ActionResult<object>> auditPost([FromBody] FileFront auditinfo)
{
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return Ok(new error_mb { code = 401, message = "无权访问" });
}
using (TestDbContext sub = new TestDbContext())
{
var query = sub.T_Audit.AsQueryable();
if (query.Any(n => n.id == auditinfo.Id))
{
var au = query.FirstOrDefault(n => n.id == auditinfo.Id);
au.proj_no = auditinfo.projNo;
au.proj_name = auditinfo.projName;
au.budget_use = auditinfo.budgetUse;
au.status = auditinfo.status;
au.budget_name = auditinfo.budgetName;
au.biz_type = auditinfo.bizType;
au.budget_id = auditinfo.budgetId;
au.start_time = auditinfo.startTime;
au.budget_money = auditinfo.budgetMoney;
au.now_auth_person = auditinfo.nowAuthPerson;
au.now_auth_person_id = auditinfo.nowAuthPersonId;
au.supplementary_info = auditinfo.supplementaryInfo;
au.description = auditinfo.description;
au.reason = auditinfo.reason;
au.start_person_id = auditinfo.startPersonId;
sub.SaveChanges();
return Ok(new { code = 200, message = "成功修改" });
}
else
{
var newAudit = new FileDB
{
id = auditinfo.Id,
proj_no = auditinfo.projNo,
proj_name = auditinfo.projName,
budget_use = auditinfo.budgetUse,
status = auditinfo.status,
budget_name = auditinfo.budgetName,
biz_type = auditinfo.bizType,
budget_id = auditinfo.budgetId,
start_time = auditinfo.startTime,
budget_money = auditinfo.budgetMoney,
now_auth_person = auditinfo.nowAuthPerson,
now_auth_person_id = auditinfo.nowAuthPersonId,
supplementary_info = auditinfo.supplementaryInfo,
description = auditinfo.description,
reason = auditinfo.reason,
start_person_id = auditinfo.startPersonId,
};
sub.T_Audit.Add(newAudit);
sub.SaveChanges();
return Ok(new { code = 200, message = "成功新增" });
}
}
}
/// <summary>
/// 查询审批
/// </summary>
[Route("api/v1/admin/AuditFind")]
[HttpGet]
[Authorize]
public async Task<ActionResult<object>> auditFind([FromQuery] string archive="",string projName = "", string projNo = "", string bizType = "", string startPerson = "", string budgetUse = "", short page = 1, short limit = 10)
{
string userId = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
var user = await userManager.FindByIdAsync(userId);
if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "admin"))
{
return Ok(new error_mb { code = 401, message = "无权访问" });
}
using (TestDbContext sub = new TestDbContext())
{
try
{
var query = sub.T_Audit
.Where(n => n.proj_name.Contains(projName) || n.proj_no.Contains(projNo) || n.biz_type.Contains(bizType) || n.start_person.Contains(startPerson) || n.budget_use.Contains(budgetUse)).AsQueryable();
if (archive == "1")
{
var rows = query
.Skip((page - 1) * limit)
.Take(limit)
.ToList();
int total = query.Count();
var data = new
{
rows,
total,
};
return Ok(new { code = 200, message = "", data });
}
else
{
var rows = query
.Where(n =>n.now_auth_person_id ==(int)user.Id)
.Skip((page - 1) * limit)
.Take(limit)
.ToList();
int total = query.Count();
var data = new
{
rows,
total,
};
return Ok(new { code = 200, message = "", data });
}
}
catch (Exception ex)
{
return Ok(new { code = 500, message = "服务器错误", ex });
}
}
}
}
}