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 { /// /// 删除用户 /// /// /// [HttpPost()] [Authorize()] public ReturnInfo DelCom([FromBody] Dictionary 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; } /// /// 修改用户 /// /// /// [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; } /// /// 获取用户信息 /// /// /// [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; } /// /// 新增用户 /// /// /// [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; } /// /// 所属行业 /// public string? Industry { get; set; } /// /// 身份 /// public string? Identity { get; set; } public string? Region { get; set; } public string? Logoaddress { get; set; } /// /// 营业执照 /// public string? LicenseCode { get; set; } } }