IpAccountServiceImpl.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.IpAccountParam;
  7. import com.hr.param.SwitchStatusParam;
  8. import com.hr.repository.domain.IpAccountPO;
  9. import com.hr.repository.jpa.IpAccountRepository;
  10. import com.hr.repository.jpa.IpInterfaceRepository;
  11. import com.hr.service.IpAccountService;
  12. import com.hr.util.SnowflakeIdWorker;
  13. import com.hr.vo.IpAccountVO;
  14. import com.yy.basedevelop.common.data.BasePageResult;
  15. import com.yy.basedevelop.dto.LoginUser;
  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.stereotype.Service;
  20. import java.util.Arrays;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import java.util.concurrent.TimeUnit;
  25. /**
  26. * 账户表;(ip_account)表服务接口实现类
  27. * @author : http://www.yonsum.com
  28. * @date : 2025-10-10
  29. */
  30. @Service
  31. public class IpAccountServiceImpl implements IpAccountService {
  32. @Autowired
  33. private IpAccountRepository ipAccountRepository;
  34. @Autowired
  35. private IpInterfaceRepository ipInterfaceRepository;
  36. private static final Cache<String, IpAccountVO> QUERY_CACHE = CacheBuilder.newBuilder()
  37. .expireAfterWrite(1, TimeUnit.SECONDS)
  38. .build();
  39. /**
  40. * 通过ID查询单条数据
  41. *
  42. * @param id 主键
  43. * @return 实例对象
  44. */
  45. public IpAccountVO queryById(Long id){
  46. IpAccountVO vo = new IpAccountVO();
  47. IpAccountPO ipAccountPO = ipAccountRepository.queryById(id);
  48. if(ipAccountPO != null){
  49. BeanUtils.copyProperties(ipAccountPO, vo);
  50. return vo;
  51. }
  52. return null;
  53. }
  54. @Override
  55. public BasePageResult<IpAccountVO> paginQuery(IpAccountParam ipAccountParam, Integer current, Integer size) {
  56. if (StringUtils.isNotEmpty(ipAccountParam.getStatus())) {
  57. ipAccountParam.setStatusList(Arrays.asList(ipAccountParam.getStatus()));
  58. }
  59. PageHelper.startPage(current, size);
  60. BasePageResult<IpAccountVO> resultList = ipAccountRepository.queryList(ipAccountParam.getAccNo(),ipAccountParam.getStatusList());
  61. return resultList;
  62. }
  63. /**
  64. * 新增数据
  65. *
  66. * @param IpAccountPO 实例对象
  67. * @return 实例对象
  68. */
  69. public void insert(IpAccountPO IpAccountPO){
  70. ipAccountRepository.save(IpAccountPO);
  71. }
  72. /**
  73. * 更新数据
  74. *
  75. * @param IpAccountPO 实例对象
  76. * @return 实例对象
  77. */
  78. public void update(IpAccountPO IpAccountPO){
  79. ipAccountRepository.save(IpAccountPO);
  80. }
  81. @Override
  82. public IpAccountVO queryByAccountNo(String accountNo) {
  83. String key = "queryByAccountNo:" + accountNo;
  84. IpAccountVO cacheValue = QUERY_CACHE.getIfPresent(key);
  85. if(cacheValue != null){
  86. return cacheValue;
  87. }else{
  88. IpAccountVO vo = new IpAccountVO();
  89. IpAccountPO accountPO = ipAccountRepository.findByAccNo(accountNo);
  90. BeanUtils.copyProperties(accountPO,vo);
  91. QUERY_CACHE.put(key, vo);
  92. return vo;
  93. }
  94. }
  95. @Override
  96. public Boolean switchStatus(SwitchStatusParam switchStatusParam,LoginUser user) {
  97. Optional<IpAccountPO> optional = ipAccountRepository.findById(switchStatusParam.getId());
  98. if(!optional.isPresent()){
  99. throw new RuntimeException("接口不存在");
  100. }
  101. IpAccountPO ipAccountPO = optional.get();
  102. ipAccountPO.setUpdateTime(new Date());
  103. ipAccountPO.setUpdateUserId(user.getUserid());
  104. ipAccountPO.setUpdateUserName(user.getUsername());
  105. ipAccountPO.setStatus(switchStatusParam.getStatus());
  106. ipAccountRepository.save(ipAccountPO);
  107. return true;
  108. }
  109. @Override
  110. public void saveOrUpdate(IpAccountParam accountParam, String userName, Long userId) {
  111. IpAccountPO ipAccountPO ;
  112. if(accountParam.getId() == null){
  113. ipAccountPO = ipAccountRepository.findByAccNo(accountParam.getAccNo());
  114. if(ipAccountPO != null){
  115. throw new RuntimeException("账户编号已存在");
  116. }
  117. ipAccountPO = new IpAccountPO();
  118. ipAccountPO.setAccNo(accountParam.getAccNo());
  119. ipAccountPO.setStatus("normal");
  120. }else{
  121. ipAccountPO = ipAccountRepository.findById(accountParam.getId()).get();
  122. }
  123. if(accountParam.getId() == null){
  124. ipAccountPO.setId(SnowflakeIdWorker.nextId());
  125. ipAccountPO.setAddTime(new Date());
  126. ipAccountPO.setAddUserId(userId);
  127. ipAccountPO.setAddUserName(userName);
  128. }
  129. ipAccountPO.setStatus(accountParam.getStatus());
  130. ipAccountPO.setRemark(accountParam.getRemark());
  131. ipAccountPO.setUpdateTime(new Date());
  132. ipAccountPO.setUpdateUserId(userId);
  133. ipAccountPO.setUpdateUserName(userName);
  134. ipAccountRepository.save(ipAccountPO);
  135. }
  136. /**
  137. * 通过主键删除数据
  138. *
  139. * @param id 主键
  140. * @param user
  141. * @return 是否成功
  142. */
  143. public boolean deleteById(Long id, LoginUser user){
  144. IpAccountPO ipAccountPO = ipAccountRepository.findById(id).get();
  145. Integer countNum = ipInterfaceRepository.countEnableAccountRef(id);
  146. if(countNum > 0){
  147. throw new RuntimeException("存在激活的接口关联,请先停用关联的接口");
  148. }
  149. ipAccountPO.setStatus("delete");
  150. ipAccountPO.setUpdateTime(new Date());
  151. ipAccountPO.setUpdateUserId(user.getUserid());
  152. ipAccountPO.setUpdateUserName(user.getUsername());
  153. ipAccountRepository.save(ipAccountPO);
  154. return true;
  155. }
  156. }