package com.jiayue.ipfcst.console.service; import com.jiayue.ipfcst.common.core.exception.BusinessException; import com.jiayue.ipfcst.common.data.entity.InverterInfo; import com.jiayue.ipfcst.common.data.entity.WindTurbineInfo; import com.jiayue.ipfcst.common.data.repository.InverterInfoRepository; import com.jiayue.ipfcst.common.data.service.BaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.*; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import javax.persistence.criteria.Predicate; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * 逆变器业务层 * * @author zzy * @version 1.0 * @since 2019/8/7 10:58 */ @Service @Slf4j public class InverterInfoService extends BaseService { private final InverterInfoRepository inverterInfoRepository; @Autowired public InverterInfoService(InverterInfoRepository inverterInfoRepository) { this.inverterInfoRepository = inverterInfoRepository; } /** * 新增逆变器 * * @param inverterInfo 逆变器 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void add(InverterInfo inverterInfo) throws BusinessException { this.inverterInfoRepository.save(inverterInfo); } /** * 批量新增逆变器 * * @param inverterInfo 逆变器 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void addALL(InverterInfo inverterInfo, Integer startValue, Integer endValue) throws BusinessException { for (int i = startValue; i < startValue + endValue; i++) { InverterInfo inverterInfo1 = new InverterInfo(); inverterInfo1.setManufacturer(inverterInfo.getManufacturer()); inverterInfo1.setReport(inverterInfo.getReport()); inverterInfo1.setInstallationTime(inverterInfo.getInstallationTime()); inverterInfo1.setName(inverterInfo.getName() + "-" + i); inverterInfo1.setBatteryModel(inverterInfo.getBatteryModel()); inverterInfo1.setBatteryNumber(inverterInfo.getBatteryNumber()); inverterInfo1.setBox(inverterInfo.getBox()); inverterInfo1.setCapacity(inverterInfo.getCapacity()); inverterInfo1.setCollectorCircuit(inverterInfo.getCollectorCircuit()); inverterInfo1.setEfficiency(inverterInfo.getEfficiency()); inverterInfo1.setGroupSeries(inverterInfo.getGroupSeries()); inverterInfo1.setSample(inverterInfo.getSample()); inverterInfo1.setModelNumber(inverterInfo.getModelNumber()); inverterInfo1.setInterval(inverterInfo.getInterval()); this.inverterInfoRepository.save(inverterInfo1); } // boolean b = this.inverterRepository.existsById(inverter.getNo()); // if (b) {// 逆变器编号已存在 // throw new BusinessException("逆变器编号已存在!"); // } else { // // } } /** * 修改逆变器 * * @param inverterInfo 逆变器 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void update(InverterInfo inverterInfo) throws BusinessException { if (StringUtils.isEmpty(inverterInfo.getId())) { throw new BusinessException("逆变器编号不能为空!"); } else { this.inverterInfoRepository.save(inverterInfo); } } /** * 删除逆变器 * * @param id 逆变器编号 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.SUPPORTS) public void delete(final Integer id) throws BusinessException { if (StringUtils.isEmpty(id)) { throw new BusinessException("逆变器编号不能为空!"); } else { this.inverterInfoRepository.deleteById(id); } } /** * 查询逆变器 * * @param id 逆变器编号 * @return 逆变器 * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public InverterInfo get(final Integer id) throws BusinessException { Optional optional = this.inverterInfoRepository.findById(id); if (optional.isPresent()) { return optional.get(); } else { throw new BusinessException("逆变器不存在!"); } } /* *//** * 查询逆变器【分页查询】 * * @param inverter 查询条件 * @param page 页码 * @param size 每页记录数 * @return 分页结果 *//* @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public Page get(final Inverter inverter, final Integer page, final Integer size) { ExampleMatcher matcher = ExampleMatcher.matching() .withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains()) .withMatcher("box", ExampleMatcher.GenericPropertyMatchers.contains()) .withMatcher("collectorCircuit", ExampleMatcher.GenericPropertyMatchers.contains()); Example example = Example.of(inverter, matcher); Pageable pageable = PageRequest.of(page - 1, size); return this.inverterRepository.findAll(example, pageable); }*/ /** * 查询逆变器【分页查询】 * * @param page 页码 * @param size 每页记录数 * @return 分页结果 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public Page get(final InverterInfo inverterInfo, final Integer page, final Integer size) { ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains()); Example example = Example.of(inverterInfo, matcher); Pageable pageable = PageRequest.of(page - 1, size); return this.inverterInfoRepository.findAll(example, pageable); } /** * 查询逆变器【分页查询】 * @return 分页结果 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public List getAll() { return inverterInfoRepository.findAll(); } /** * 查询所有样板机逆变器 create by xiuwei * * @return 逆变器信息 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public List getAllSample() { InverterInfo inverterInfo = new InverterInfo(); inverterInfo.setSample(true); return inverterInfoRepository.findAll(Example.of(inverterInfo)); } /** * 查询条件 * * @param sample 是否样板机 * @return 查询条件 */ private Specification getDemoQuerySpecification(Boolean sample) { return (Specification) (root, criteriaQuery, cb) -> { List predicates = new ArrayList<>(); if (null != sample) { predicates.add(cb.equal(root.get("sample").as(Boolean.class), sample)); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; } /** * 保存光伏组件信息 * * @param inverterInfo 光伏组件信息 */ @Transactional(propagation = Propagation.SUPPORTS) public void save(InverterInfo inverterInfo) { this.inverterInfoRepository.save(inverterInfo); } /** * 删除逆变器信息 */ @Transactional(propagation = Propagation.SUPPORTS) public boolean delete(String ids) { boolean flag = false; if (!StringUtils.isEmpty(ids)) { String[] idArray = ids.split(","); if (idArray != null && idArray.length > 0) { for (String id : idArray) { this.inverterInfoRepository.deleteById(Integer.valueOf(id)); log.info("删除逆变器ID为[" + id + "]成功!"); } flag = true; } else { this.inverterInfoRepository.deleteById(Integer.valueOf(ids)); flag = true; log.info("删除逆变器ID为[" + ids + "]成功!"); } } return flag; } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveCloud(InverterInfo bean) { // InverterInfo inverterInfo = inverterInfoRepository.findByName(bean.getName()); // if (null != inverterInfo) { // bean.setId(inverterInfo.getId()); // } inverterInfoRepository.save(bean); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void saveCloud(List beans) { if(beans != null && beans.size() > 0){ inverterInfoRepository.deleteAll(); inverterInfoRepository.saveAll(beans); } } /** * 根据场站编号查询逆变器 * * @param stationCode 场站编号 * @return * @throws BusinessException 业务异常 */ @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public List getByStationCode(String stationCode) { List resultList = new ArrayList<>(); resultList = this.inverterInfoRepository.findAll(); if (!"ALL".equals(stationCode)){ resultList = resultList.stream().filter(s->s.getStationCode().equals(stationCode)).collect(Collectors.toList()); } return resultList.stream().sorted(Comparator.comparing(InverterInfo::getStationCode)).collect(Collectors.toList()); } }