PartnerTypeService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using FMSAdmin.Data;
  8. using FMSAdmin.Helpers;
  9. using FMSAdmin.Entities;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using Microsoft.IdentityModel.Tokens;
  14. using FMSAdmin.Models;
  15. namespace FMSAdmin.Services {
  16. public class PartnerTypeService {
  17. private readonly ILogger<PartnerTypeService> _logger;
  18. private readonly FMSContext _context;
  19. public PartnerTypeService(
  20. ILogger<PartnerTypeService> logger,
  21. FMSContext context) {
  22. _logger = logger;
  23. _context = context;
  24. }
  25. public void Save(CmPartnerType data) {
  26. var persist = _context.CmPartnerType
  27. .Where(x => x.PartnerTypeId == data.PartnerTypeId).FirstOrDefault();
  28. if (persist == null) {
  29. var check = _context.CmPartnerType.Where(x => x.Name == data.Name || x.PartnerTypeId == data.PartnerTypeId).Count();
  30. if (check > 0) {
  31. throw new ServiceException("이미 동일한 정보가 존재합니다.");
  32. }
  33. if (data.Name.Trim() == "검사기관") {
  34. throw new ServiceException("거래처 유형 명 [검사기관]은 등록할 수 없습니다.");
  35. }
  36. _context.CmPartnerType.Add(data);
  37. _context.SaveChanges();
  38. } else {
  39. //거래처 유형 명이 [검사기관]은 수정, 삭제할 수 없다. 2020-03-19
  40. if (data.PartnerTypeId == 1) {
  41. throw new ServiceException("거래처 유형 [검사기관]은 수정할 수 없습니다.");
  42. }
  43. //거래처 유형 명이 [검사기관]은 수정, 삭제할 수 없다. 2020-03-19
  44. if (data.PartnerTypeId != 1 && data.Name == "검사기관") {
  45. throw new ServiceException("거래처 유형 명을 [검사기관]으로 수정할 수 없습니다.");
  46. }
  47. //거래처 유형 명 중복 체크
  48. var check1 = _context.CmPartnerType.Where(x => x.Name == data.Name && x.PartnerTypeId != data.PartnerTypeId).Count();
  49. if (check1 > 0) {
  50. throw new ServiceException("거래처 유형 명이 동일한 정보가 존재합니다.");
  51. }
  52. persist.PartnerTypeId = data.PartnerTypeId;
  53. persist.Name = data.Name;
  54. persist.IsUse = data.IsUse;
  55. //
  56. _context.SaveChanges();
  57. }
  58. }
  59. public IQueryable<CmPartnerType> GetAll() {
  60. var query = _context.CmPartnerType;
  61. return query;
  62. }
  63. public IQueryable<CmPartnerType> Get(int id) {
  64. var data = _context.CmPartnerType.Where(x => x.PartnerTypeId == id);
  65. return data;
  66. }
  67. public void Delete(int id) {
  68. var data = _context.CmPartnerType.First(x => x.PartnerTypeId == id);
  69. //data.IsUse = false;
  70. //거래처 유형 명 [검사기관]은 삭제할 수 없다. 2020-03-19
  71. if (id == 1 || data.Name == "검사기관") {
  72. throw new ServiceException("거래처 유형이 [검사기관]은 삭제할 수 없습니다.");
  73. }
  74. _context.CmPartnerType.Remove(data); //실제 행이 삭제될 경우 처리
  75. _context.SaveChanges();
  76. }
  77. }
  78. }