package com.its.op.service.cctv; import com.its.op.model.dto.cctv.CctvMonitoringDto; import com.its.op.model.entity.cctv.CctvMonitoring; import com.its.op.repository.cctv.CctvMonitoringRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @Slf4j @RequiredArgsConstructor @Service public class TbCctvMonitoringService { private final CctvMonitoringRepository repo; public List findAll() { try { return this.repo.findAll(); } catch (Exception e) { log.error("{}.findAll: Exception: {}", getClass().getSimpleName(), e.getMessage()); } return new ArrayList(); } public List findByName(String name) { try { return this.repo.findByName(name); } catch (Exception e) { log.error("{}.findByName: Object: {}, Exception: {}", getClass().getSimpleName(), name, e.getMessage()); } return new ArrayList(); } @Transactional public List mergeInfo(List req) { try { HashSet names = new HashSet(); for (CctvMonitoringDto.CctvMonitoringUpdateReq obj : req) { names.add(obj.getMONITORING_NM()); } for (String name : names) { this.repo.deleteByName(name); } //this.repo.flush(); for (CctvMonitoringDto.CctvMonitoringUpdateReq obj : req) { CctvMonitoring data = new CctvMonitoring(3, obj); //this.repo.save(data); this.repo.insertValue(data.getMONITORING_TYPE(), data.getMONITORING_NM(), data.getMONITORING_SEQ(), data.getCCTV_CTLR_NMBR(), data.getVIEW_MODE()); } return req; } catch (Exception e) { log.error("{}.mergeInfo: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage()); } return null; } @Transactional public List deleteByName(String name) { try { List result = this.repo.findByName(name); if (result != null && result.size() > 0) { this.repo.deleteByName(name); } return result; } catch (Exception e) { log.error("{}.deleteByName: Object: {}, Exception: {}", getClass().getSimpleName(), name, e.getMessage()); } return new ArrayList(); } }