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

236 lines
7.5 KiB
C#

using System.Reflection;
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 CompanyController : ControllerBase
{
/// <summary>
/// 删除用户
/// </summary>
/// <param name="LLL"></param>
/// <returns></returns>
[HttpPost()]
[Authorize()]
public ReturnInfo DelCom([FromBody] Dictionary<string, int> LLL)
{
ReturnInfo returnInfo = new ReturnInfo();
try
{
int idd = LLL["Id"];
using (var q = new AgentApprovalSystemContext())
{
var FFF = q.AasCompanyInfos.FirstOrDefault(A => A.Id == idd);
if (FFF != null)
{
q.AasCompanyInfos.Remove(FFF);
q.SaveChanges();
returnInfo.isok = true;
}
}
}
catch (Exception ex)
{
returnInfo.isok = false;
returnInfo.message = ex.Message;
}
return returnInfo;
}
/// <summary>
/// 修改用户
/// </summary>
/// <param name="LLL"></param>
/// <returns></returns>
[HttpPost()]
[Authorize()]
public ReturnInfo EditCom([FromBody] Com LLL)
{
ReturnInfo returnInfo = new ReturnInfo();
try
{
using (var q = new AgentApprovalSystemContext())
{
var lll = q.AasCompanyInfos.SingleOrDefault(A => A.Id == LLL.Id);
if (lll != null)
{
lll.NameCn = LLL.NameCn;
lll.NameEn = LLL.NameEn;
lll.Identity = LLL.Identity;
lll.Industry = LLL.Industry;
lll.Code = LLL.Code;
lll.LicenseCode = LLL.LicenseCode;
lll.Region = LLL.Region;
lll.Logoaddress= LLL.Logoaddress;
lll.Updatetime = DateTime.Now;
q.AasCompanyInfos.Update(lll);
q.SaveChanges();
returnInfo.isok = true;
}
}
}
catch (Exception ex)
{
returnInfo.isok = false;
returnInfo.message = ex.Message;
}
return returnInfo;
}
/// <summary>
/// 获取用户信息
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
[HttpPost()]
[Authorize()]
public ReturnInfo GetComInfo([FromBody] QueryAll_Or_Single S)
{
ReturnInfo returnInfo = new ReturnInfo();
try
{
using (var q = new AgentApprovalSystemContext())
{
if (S.IsAll)
{
returnInfo.isok = true;
returnInfo.response = q.AasCompanyInfos.Select(F => new AasCompanyInfo
{
Id = F.Id,
Code = F.Code,
NameCn = F.NameCn,
NameEn = F.NameEn,
Identity = F.Identity,
LicenseCode = F.LicenseCode,
Industry = F.Industry,
Region = F.Region,
Logoaddress = F.Logoaddress
}).ToList();
}
else
{
returnInfo.isok = true;
var a = q.AasCompanyInfos.SingleOrDefault(A => A.Id == S.ID);
if (a != null)
{
AasCompanyInfo u = new AasCompanyInfo();
u.Id = a.Id;
u.Code = a.Code;
u.NameCn = a.NameCn;
u.NameEn = a.NameEn;
u.Updatetime = a.Updatetime;
u.Createtime = a.Createtime;
u.Industry = a.Industry;
u.LicenseCode = a.LicenseCode;
u.Region = a.Region;
returnInfo.response = u;
}
}
}
}
catch (Exception ex)
{
returnInfo.isok = false;
returnInfo.message = ex.Message;
}
return returnInfo;
}
/// <summary>
/// 新增用户
/// </summary>
/// <param name="LLL"></param>
/// <returns></returns>
[HttpPost()]
[Authorize()]
public ReturnInfo AddCom([FromBody] Com LLL)
{
ReturnInfo returnInfo = new ReturnInfo();
try
{
AasCompanyInfo a = new AasCompanyInfo();
a.NameCn = LLL.NameCn;
a.NameEn = LLL.NameEn;
a.Identity = LLL.Identity;
a.Industry = LLL.Industry;
a.Region = LLL.Region;
a.Logoaddress = LLL.Logoaddress;
a.Createtime = DateTime.Now;
a.Updatetime = DateTime.Now;
using (var q = new AgentApprovalSystemContext())
{
var Q = q.AasCompanyInfos.Where(A => A.NameCn.Equals(LLL.NameCn));
if (Q.Count() > 0)
{
returnInfo.isok = false;
returnInfo.message = "此公司已经存在";
}
else
{
using var T = q.Database.BeginTransaction();
Autokeygenerater? QQQ = q.Autokeygeneraters.SingleOrDefault();
QQQ.CompanyCode = QQQ.CompanyCode + 1;
a.Code = QQQ.CompanyCode.ToString();
q.Autokeygeneraters.Update(QQQ);
q.AasCompanyInfos.Add(a);
returnInfo.isok = true;
q.SaveChanges();
T.Commit();
}
}
}
catch (Exception ex)
{
returnInfo.isok = false;
returnInfo.message = ex.Message;
}
return returnInfo;
}
}
public class Com
{
public int Id { get; set; } = -1;
public string? Code { get; set; }
public string? NameCn { get; set; }
public string? NameEn { get; set; }
/// <summary>
/// 所属行业
/// </summary>
public string? Industry { get; set; }
/// <summary>
/// 身份
/// </summary>
public string? Identity { get; set; }
public string? Region { get; set; }
public string? Logoaddress { get; set; }
/// <summary>
/// 营业执照
/// </summary>
public string? LicenseCode { get; set; }
}
}