InverterInfoService.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.jiayue.ipfcst.console.service;
  2. import com.jiayue.ipfcst.common.core.exception.BusinessException;
  3. import com.jiayue.ipfcst.common.data.entity.InverterInfo;
  4. import com.jiayue.ipfcst.common.data.entity.WindTurbineInfo;
  5. import com.jiayue.ipfcst.common.data.repository.InverterInfoRepository;
  6. import com.jiayue.ipfcst.common.data.service.BaseService;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.data.domain.*;
  10. import org.springframework.data.jpa.domain.Specification;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Propagation;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.util.StringUtils;
  15. import javax.persistence.criteria.Predicate;
  16. import java.util.ArrayList;
  17. import java.util.Comparator;
  18. import java.util.List;
  19. import java.util.Optional;
  20. import java.util.stream.Collectors;
  21. /**
  22. * 逆变器业务层
  23. *
  24. * @author zzy
  25. * @version 1.0
  26. * @since 2019/8/7 10:58
  27. */
  28. @Service
  29. @Slf4j
  30. public class InverterInfoService extends BaseService {
  31. private final InverterInfoRepository inverterInfoRepository;
  32. @Autowired
  33. public InverterInfoService(InverterInfoRepository inverterInfoRepository) {
  34. this.inverterInfoRepository = inverterInfoRepository;
  35. }
  36. /**
  37. * 新增逆变器
  38. *
  39. * @param inverterInfo 逆变器
  40. * @throws BusinessException 业务异常
  41. */
  42. @Transactional(propagation = Propagation.SUPPORTS)
  43. public void add(InverterInfo inverterInfo) throws BusinessException {
  44. this.inverterInfoRepository.save(inverterInfo);
  45. }
  46. /**
  47. * 批量新增逆变器
  48. *
  49. * @param inverterInfo 逆变器
  50. * @throws BusinessException 业务异常
  51. */
  52. @Transactional(propagation = Propagation.SUPPORTS)
  53. public void addALL(InverterInfo inverterInfo, Integer startValue, Integer endValue) throws BusinessException {
  54. for (int i = startValue; i < startValue + endValue; i++) {
  55. InverterInfo inverterInfo1 = new InverterInfo();
  56. inverterInfo1.setManufacturer(inverterInfo.getManufacturer());
  57. inverterInfo1.setReport(inverterInfo.getReport());
  58. inverterInfo1.setInstallationTime(inverterInfo.getInstallationTime());
  59. inverterInfo1.setName(inverterInfo.getName() + "-" + i);
  60. inverterInfo1.setBatteryModel(inverterInfo.getBatteryModel());
  61. inverterInfo1.setBatteryNumber(inverterInfo.getBatteryNumber());
  62. inverterInfo1.setBox(inverterInfo.getBox());
  63. inverterInfo1.setCapacity(inverterInfo.getCapacity());
  64. inverterInfo1.setCollectorCircuit(inverterInfo.getCollectorCircuit());
  65. inverterInfo1.setEfficiency(inverterInfo.getEfficiency());
  66. inverterInfo1.setGroupSeries(inverterInfo.getGroupSeries());
  67. inverterInfo1.setSample(inverterInfo.getSample());
  68. inverterInfo1.setModelNumber(inverterInfo.getModelNumber());
  69. inverterInfo1.setInterval(inverterInfo.getInterval());
  70. this.inverterInfoRepository.save(inverterInfo1);
  71. }
  72. // boolean b = this.inverterRepository.existsById(inverter.getNo());
  73. // if (b) {// 逆变器编号已存在
  74. // throw new BusinessException("逆变器编号已存在!");
  75. // } else {
  76. //
  77. // }
  78. }
  79. /**
  80. * 修改逆变器
  81. *
  82. * @param inverterInfo 逆变器
  83. * @throws BusinessException 业务异常
  84. */
  85. @Transactional(propagation = Propagation.SUPPORTS)
  86. public void update(InverterInfo inverterInfo) throws BusinessException {
  87. if (StringUtils.isEmpty(inverterInfo.getId())) {
  88. throw new BusinessException("逆变器编号不能为空!");
  89. } else {
  90. this.inverterInfoRepository.save(inverterInfo);
  91. }
  92. }
  93. /**
  94. * 删除逆变器
  95. *
  96. * @param id 逆变器编号
  97. * @throws BusinessException 业务异常
  98. */
  99. @Transactional(propagation = Propagation.SUPPORTS)
  100. public void delete(final Integer id) throws BusinessException {
  101. if (StringUtils.isEmpty(id)) {
  102. throw new BusinessException("逆变器编号不能为空!");
  103. } else {
  104. this.inverterInfoRepository.deleteById(id);
  105. }
  106. }
  107. /**
  108. * 查询逆变器
  109. *
  110. * @param id 逆变器编号
  111. * @return 逆变器
  112. * @throws BusinessException 业务异常
  113. */
  114. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  115. public InverterInfo get(final Integer id) throws BusinessException {
  116. Optional<InverterInfo> optional = this.inverterInfoRepository.findById(id);
  117. if (optional.isPresent()) {
  118. return optional.get();
  119. } else {
  120. throw new BusinessException("逆变器不存在!");
  121. }
  122. }
  123. /*
  124. *//**
  125. * 查询逆变器【分页查询】
  126. *
  127. * @param inverter 查询条件
  128. * @param page 页码
  129. * @param size 每页记录数
  130. * @return 分页结果
  131. *//*
  132. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  133. public Page<Inverter> get(final Inverter inverter, final Integer page, final Integer size) {
  134. ExampleMatcher matcher = ExampleMatcher.matching()
  135. .withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains())
  136. .withMatcher("box", ExampleMatcher.GenericPropertyMatchers.contains())
  137. .withMatcher("collectorCircuit", ExampleMatcher.GenericPropertyMatchers.contains());
  138. Example<Inverter> example = Example.of(inverter, matcher);
  139. Pageable pageable = PageRequest.of(page - 1, size);
  140. return this.inverterRepository.findAll(example, pageable);
  141. }*/
  142. /**
  143. * 查询逆变器【分页查询】
  144. *
  145. * @param page 页码
  146. * @param size 每页记录数
  147. * @return 分页结果
  148. */
  149. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  150. public Page<InverterInfo> get(final InverterInfo inverterInfo, final Integer page, final Integer size) {
  151. ExampleMatcher matcher =
  152. ExampleMatcher.matching().withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains());
  153. Example<InverterInfo> example = Example.of(inverterInfo, matcher);
  154. Pageable pageable = PageRequest.of(page - 1, size);
  155. return this.inverterInfoRepository.findAll(example, pageable);
  156. }
  157. /**
  158. * 查询逆变器【分页查询】
  159. * @return 分页结果
  160. */
  161. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  162. public List<InverterInfo> getAll() {
  163. return inverterInfoRepository.findAll();
  164. }
  165. /**
  166. * 查询所有样板机逆变器 create by xiuwei
  167. *
  168. * @return 逆变器信息
  169. */
  170. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  171. public List<InverterInfo> getAllSample() {
  172. InverterInfo inverterInfo = new InverterInfo();
  173. inverterInfo.setSample(true);
  174. return inverterInfoRepository.findAll(Example.of(inverterInfo));
  175. }
  176. /**
  177. * 查询条件
  178. *
  179. * @param sample 是否样板机
  180. * @return 查询条件
  181. */
  182. private Specification<InverterInfo> getDemoQuerySpecification(Boolean sample) {
  183. return (Specification<InverterInfo>) (root, criteriaQuery, cb) -> {
  184. List<Predicate> predicates = new ArrayList<>();
  185. if (null != sample) {
  186. predicates.add(cb.equal(root.get("sample").as(Boolean.class), sample));
  187. }
  188. return cb.and(predicates.toArray(new Predicate[predicates.size()]));
  189. };
  190. }
  191. /**
  192. * 保存光伏组件信息
  193. *
  194. * @param inverterInfo 光伏组件信息
  195. */
  196. @Transactional(propagation = Propagation.SUPPORTS)
  197. public void save(InverterInfo inverterInfo) {
  198. this.inverterInfoRepository.save(inverterInfo);
  199. }
  200. /**
  201. * 删除逆变器信息
  202. */
  203. @Transactional(propagation = Propagation.SUPPORTS)
  204. public boolean delete(String ids) {
  205. boolean flag = false;
  206. if (!StringUtils.isEmpty(ids)) {
  207. String[] idArray = ids.split(",");
  208. if (idArray != null && idArray.length > 0) {
  209. for (String id : idArray) {
  210. this.inverterInfoRepository.deleteById(Integer.valueOf(id));
  211. log.info("删除逆变器ID为[" + id + "]成功!");
  212. }
  213. flag = true;
  214. } else {
  215. this.inverterInfoRepository.deleteById(Integer.valueOf(ids));
  216. flag = true;
  217. log.info("删除逆变器ID为[" + ids + "]成功!");
  218. }
  219. }
  220. return flag;
  221. }
  222. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  223. public void saveCloud(InverterInfo bean) {
  224. // InverterInfo inverterInfo = inverterInfoRepository.findByName(bean.getName());
  225. // if (null != inverterInfo) {
  226. // bean.setId(inverterInfo.getId());
  227. // }
  228. inverterInfoRepository.save(bean);
  229. }
  230. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  231. public void saveCloud(List<InverterInfo> beans) {
  232. if(beans != null && beans.size() > 0){
  233. inverterInfoRepository.deleteAll();
  234. inverterInfoRepository.saveAll(beans);
  235. }
  236. }
  237. /**
  238. * 根据场站编号查询逆变器
  239. *
  240. * @param stationCode 场站编号
  241. * @return
  242. * @throws BusinessException 业务异常
  243. */
  244. @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  245. public List<InverterInfo> getByStationCode(String stationCode) {
  246. List<InverterInfo> resultList = new ArrayList<>();
  247. resultList = this.inverterInfoRepository.findAll();
  248. if (!"ALL".equals(stationCode)){
  249. resultList = resultList.stream().filter(s->s.getStationCode().equals(stationCode)).collect(Collectors.toList());
  250. }
  251. return resultList.stream().sorted(Comparator.comparing(InverterInfo::getStationCode)).collect(Collectors.toList());
  252. }
  253. }