IpInterfaceServiceImpl.java 6.9 KB

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