IpInterfaceServiceImpl.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.hr.service.impl;
  2. import com.hr.param.InterfaceConfigParam;
  3. import com.hr.param.IpInterfaceParam;
  4. import com.hr.param.SwitchStatusParam;
  5. import com.hr.repository.domain.IpInterfacePO;
  6. import com.hr.repository.jpa.IpInterfaceRepository;
  7. import com.hr.service.IpInterfaceService;
  8. import com.hr.util.SnowflakeIdWorker;
  9. import com.hr.vo.IpInterfaceVO;
  10. import com.hr.vo.NameValueVO;
  11. import org.springframework.beans.BeanUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.domain.*;
  14. import org.springframework.stereotype.Service;
  15. import java.time.LocalDateTime;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.Optional;
  19. import java.util.stream.Collectors;
  20. /**
  21. * 接口表;(ip_interface)表服务接口实现类
  22. * @author : http://www.yonsum.com
  23. * @date : 2025-10-10
  24. */
  25. @Service
  26. public class IpInterfaceServiceImpl implements IpInterfaceService {
  27. @Autowired
  28. private IpInterfaceRepository ipInterfaceRepository;
  29. /**
  30. * 通过ID查询单条数据
  31. *
  32. * @param id 主键
  33. * @return 实例对象
  34. */
  35. public IpInterfaceVO queryById(Long id){
  36. // IpInterfacePO ipInterfacePO = ipInterfaceRepository.findById(id).get();
  37. Optional<IpInterfacePO> optional = ipInterfaceRepository.findById(id);
  38. if(optional.isPresent()) {
  39. IpInterfacePO ipInterfacePO=optional.get();
  40. IpInterfaceVO ipInterfaceVO = new IpInterfaceVO();
  41. if (ipInterfacePO != null) {
  42. BeanUtils.copyProperties(ipInterfacePO, ipInterfaceVO);
  43. return ipInterfaceVO;
  44. }
  45. }
  46. return null;
  47. }
  48. @Override
  49. public Page<IpInterfaceVO> paginQuery(IpInterfaceParam interfaceParam, Integer current, Integer size) {
  50. IpInterfacePO ipInterfacePO = new IpInterfacePO();
  51. BeanUtils.copyProperties(interfaceParam, ipInterfacePO);
  52. ExampleMatcher matcher = ExampleMatcher.matchingAll()
  53. .withIgnoreNullValues() // 忽略 null 字段
  54. .withIgnoreCase();
  55. Pageable pageAble = PageRequest.of(current, size);
  56. Example<IpInterfacePO> example = Example.of(ipInterfacePO, matcher);
  57. Page<IpInterfacePO> page = ipInterfaceRepository.findAll(example, pageAble);
  58. List<IpInterfaceVO> resultList = new ArrayList<>();
  59. page.getContent().forEach(item -> {
  60. IpInterfaceVO vo = new IpInterfaceVO();
  61. BeanUtils.copyProperties(item, vo);
  62. resultList.add(vo);
  63. });
  64. Page<IpInterfaceVO> resultPage = new PageImpl(resultList);
  65. BeanUtils.copyProperties(page, resultPage);
  66. return resultPage;
  67. }
  68. /**
  69. * 新增数据
  70. *
  71. * @param IpInterfacePO 实例对象
  72. * @return 实例对象
  73. */
  74. public void insert(IpInterfacePO IpInterfacePO){
  75. ipInterfaceRepository.save(IpInterfacePO);
  76. }
  77. /**
  78. * 更新数据
  79. *
  80. * @param IpInterfacePO 实例对象
  81. * @return 实例对象
  82. */
  83. public void update(IpInterfacePO IpInterfacePO){
  84. ipInterfaceRepository.save(IpInterfacePO);
  85. }
  86. @Override
  87. public void interfaceConfig(InterfaceConfigParam param) {
  88. }
  89. @Override
  90. public List<NameValueVO> optionList() {
  91. List<IpInterfacePO> list = ipInterfaceRepository.findAll();
  92. List<IpInterfacePO> resultList = list.stream().filter(item -> item.getStatus().equals("normal")).collect(Collectors.toList());
  93. List<NameValueVO> result = new ArrayList<>();
  94. resultList.forEach(item -> {
  95. NameValueVO vo = new NameValueVO();
  96. vo.setName(item.getInterfaceName());
  97. vo.setId(item.getId());
  98. result.add(vo);
  99. });
  100. return result;
  101. }
  102. @Override
  103. public Boolean switchStatus(SwitchStatusParam switchStatusParam) {
  104. Optional<IpInterfacePO> optional = ipInterfaceRepository.findById(switchStatusParam.getId());
  105. if(!optional.isPresent()){
  106. throw new RuntimeException("接口不存在");
  107. }
  108. IpInterfacePO ipInterfacePO = optional.get();
  109. ipInterfacePO.setStatus(switchStatusParam.getStatus());
  110. ipInterfaceRepository.save(ipInterfacePO);
  111. return true;
  112. }
  113. @Override
  114. public void saveOrUpdate(IpInterfaceParam ipInterfaceParam, String username, Long userid) {
  115. IpInterfacePO ipInterfacePO ;
  116. if(ipInterfaceParam.getId() == null){
  117. ipInterfacePO = ipInterfaceRepository.findByInterfaceName(ipInterfaceParam.getInterfaceName());
  118. if(ipInterfacePO != null){
  119. throw new RuntimeException("接口名称已存在");
  120. }
  121. ipInterfacePO = ipInterfaceRepository.findByInterfaceNo(ipInterfaceParam.getInterfaceNo());
  122. if(ipInterfacePO != null){
  123. throw new RuntimeException("接口编号已存在");
  124. }
  125. }else{
  126. ipInterfacePO = ipInterfaceRepository.findByInterfaceName(ipInterfaceParam.getInterfaceName());
  127. if(ipInterfaceParam != null && ipInterfacePO.getInterfaceName().equals(ipInterfaceParam.getInterfaceName()) && !ipInterfacePO.getId().equals(ipInterfaceParam.getId())){
  128. throw new RuntimeException("接口名称已存在");
  129. }
  130. ipInterfacePO = ipInterfaceRepository.findByInterfaceNo(ipInterfaceParam.getInterfaceNo());
  131. if(ipInterfaceParam != null && ipInterfacePO.getInterfaceNo().equals(ipInterfaceParam.getInterfaceNo()) && !ipInterfacePO.getId().equals(ipInterfaceParam.getId())){
  132. throw new RuntimeException("接口编号已存在");
  133. }
  134. }
  135. ipInterfacePO = new IpInterfacePO();
  136. BeanUtils.copyProperties(ipInterfaceParam, ipInterfacePO);
  137. if(ipInterfaceParam.getId() == null){
  138. ipInterfacePO.setId(SnowflakeIdWorker.nextId());
  139. ipInterfacePO.setAddTime(LocalDateTime.now());
  140. ipInterfacePO.setAddUserId(userid);
  141. ipInterfacePO.setAddUserName(username);
  142. }
  143. ipInterfacePO.setUpdateTime(LocalDateTime.now());
  144. ipInterfacePO.setUpdateUserId(userid);
  145. ipInterfacePO.setUpdateUserName(username);
  146. ipInterfaceRepository.save(ipInterfacePO);
  147. }
  148. /**
  149. * 通过主键删除数据
  150. *
  151. * @param iid 主键
  152. * @return 是否成功
  153. */
  154. public boolean deleteById(Long iid){
  155. ipInterfaceRepository.deleteById(iid);
  156. return true;
  157. }
  158. }