package com.gunshi.project.xyt.service; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.gunshi.core.result.BusinessResult; import com.gunshi.core.result.exception.BusinessException; import com.gunshi.project.xyt.mapper.StCameraAreaBMapper; import com.gunshi.project.xyt.model.StCameraAreaB; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.bind.MethodArgumentNotValidException; import java.util.Date; import java.util.List; import java.util.Objects; /** * @author Sun Lejun * @version 1.0 * @date 2024/1/24 */ @Service @Slf4j @DSTransactional(rollbackFor = Exception.class) @Data public class BaseDataService { private final StCameraAreaBMapper stCameraAreaBMapper; public List queryStCameraAreaBList() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StCameraAreaB.COL_STATUS, 1); queryWrapper.orderByAsc(StCameraAreaB.COL_SORT_ON); return stCameraAreaBMapper.selectList(queryWrapper); } /** * 新增视频区域 * * @param stCameraAreaB 视频区域 */ public void insertStCameraAreaB(StCameraAreaB stCameraAreaB) { checkStCameraAreaB(stCameraAreaB); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StCameraAreaB.COL_PID, stCameraAreaB.getPid()); queryWrapper.eq(StCameraAreaB.COL_STATUS, 1); queryWrapper.orderByAsc(StCameraAreaB.COL_SORT_ON); StCameraAreaB first = stCameraAreaBMapper.selectList(queryWrapper).getFirst(); if (Objects.isNull(first)) { stCameraAreaB.setSortOn(1); } else { stCameraAreaB.setSortOn(first.getSortOn() + 1); } long id = IdWorker.getId(); stCameraAreaB.setCamArId(id); stCameraAreaB.setStatus(1); stCameraAreaB.setCreateTm(new Date()); stCameraAreaB.setTm(new Date()); stCameraAreaBMapper.insert(stCameraAreaB); } /** * 修改视频区域 * * @param stCameraAreaB 视频区域 */ public void updateStCameraAreaB(StCameraAreaB stCameraAreaB) { Long camArId = stCameraAreaB.getCamArId(); QueryWrapper queryWrapper2 = new QueryWrapper<>(); queryWrapper2.eq(StCameraAreaB.COL_CAM_AR_ID, camArId); boolean exists = stCameraAreaBMapper.exists(queryWrapper2); if (!exists) { throw new IllegalArgumentException("ID不存在"); } checkStCameraAreaB(stCameraAreaB); stCameraAreaB.setTm(new Date()); stCameraAreaBMapper.updateById(stCameraAreaB); } /** * 检查参数是否合法 * @param stCameraAreaB 视频区域 */ private void checkStCameraAreaB(StCameraAreaB stCameraAreaB) { Long pid = stCameraAreaB.getPid(); if (pid != 0) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StCameraAreaB.COL_CAM_AR_ID, pid); queryWrapper.eq(StCameraAreaB.COL_STATUS, 1); boolean exists = stCameraAreaBMapper.exists(queryWrapper); if (!exists) { throw new IllegalArgumentException("父节点不存在"); } queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StCameraAreaB.COL_CAM_AR_NM, stCameraAreaB.getCamArNm()); queryWrapper.eq(StCameraAreaB.COL_STATUS, 1); exists = stCameraAreaBMapper.exists(queryWrapper); if (exists) { throw new IllegalArgumentException("视频区域名称已存在"); } } } }