gunshi-project-ss/src/main/java/com/gunshi/project/ss/service/ProjectSafetyService.java

140 lines
5.2 KiB
Java
Raw Normal View History

package com.gunshi.project.ss.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gunshi.project.ss.entity.dto.ProjectSafetyDto;
import com.gunshi.project.ss.mapper.ProjectSafetyMapper;
import com.gunshi.project.ss.mapper.ProjectSafetyTypeMapper;
import com.gunshi.project.ss.model.FileAssociations;
import com.gunshi.project.ss.model.ProjectSafety;
import com.gunshi.project.ss.model.ProjectSafetyType;
import com.gunshi.project.ss.repository.TsgObjectRepository;
import com.gunshi.project.ss.util.EsCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author lyf
* @since 2025-07-21
*/
@Service
public class ProjectSafetyService extends ServiceImpl<ProjectSafetyMapper, ProjectSafety> {
@Autowired
private ProjectSafetyTypeMapper typeMapper;
@Autowired
private TsgObjectRepository tsgObjectRepository;
@Autowired
private FileAssociationsService fileAssociationsService;
public List<ProjectSafetyType> listCategory() {
QueryWrapper<ProjectSafetyType> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("id");
return typeMapper.selectList(queryWrapper);
}
public Page<ProjectSafety> page(ProjectSafetyDto dto) {
QueryWrapper<ProjectSafety> queryWrapper = new QueryWrapper<>();
if(dto.getType() !=null){
queryWrapper.eq("type", dto.getType());
}
if(dto.getStm() !=null && dto.getEtm() !=null){
queryWrapper.between("tm", dto.getStm(), dto.getEtm());
}
if (dto.getName() != null) queryWrapper.like("name", dto.getName());
int pageNumber = dto.getPageSo().getPageNumber();
int pageSize = dto.getPageSo().getPageSize();
queryWrapper.orderByDesc("_update_tm");
Page<ProjectSafety> pageParam = new Page<>(pageNumber,pageSize);
Page<ProjectSafety> ret = page(pageParam, queryWrapper);
return ret;
}
public ProjectSafetyType addCategory(String name) {
Long cnt = typeMapper.selectCount(new LambdaQueryWrapper<ProjectSafetyType>().eq(ProjectSafetyType::getName, name));
if (cnt == 0) {
ProjectSafetyType entity = new ProjectSafetyType();
entity.setName(name);
typeMapper.insert(entity);
return entity;
}else{
throw new IllegalArgumentException("名称重复,请检查");
}
}
public Boolean delCategory(Integer id) {
ProjectSafetyType type = typeMapper.selectById(id);
if (type == null) {
return false;
}
Long cnt = getBaseMapper().selectCount(new LambdaQueryWrapper<ProjectSafety>().eq(ProjectSafety::getType, type.getName()));
if (cnt > 0) {
throw new IllegalArgumentException("请先删除关联数据");
}
return typeMapper.deleteById(id) > 0;
}
@Transactional
public Boolean updateCategory(ProjectSafetyType entity) {
ProjectSafetyType projectSafetyType = typeMapper.selectById(entity.getId());
if (projectSafetyType == null) {
return false;
}
String oldName = projectSafetyType.getName();
//防止重复
Long cnt = typeMapper.selectCount(new LambdaQueryWrapper<ProjectSafetyType>().eq(ProjectSafetyType::getName, entity.getName()));
if(cnt > 0){
throw new IllegalArgumentException("名称重复,请检查");
}
projectSafetyType.setName(entity.getName());
boolean flag1 = typeMapper.updateById(projectSafetyType) > 0;
return true;
}
public ProjectSafety getOneById(Serializable id) {
ProjectSafety projectSafety = baseMapper.selectById(id);
if(projectSafety != null){
List<FileAssociations> files = fileAssociationsService.getFiles("projectSafety", projectSafety.getId().toString());
if(files == null){
projectSafety.setFiles(new ArrayList<>());
projectSafety.setFileCount(0);
}else{
projectSafety.setFileCount(files.size());
projectSafety.setFiles(files);
}
}
return projectSafety;
}
public void updateEsCache(ProjectSafety projectSafety) {
EsCacheUtil.updateCache(tsgObjectRepository,
projectSafety,
"工程安全知识库",
ProjectSafety::getId,
ProjectSafety::getName,
null,
null,
null
);
}
public void deleteEsCache(Serializable id) {
EsCacheUtil.deleteEsCache(tsgObjectRepository,id.toString(),"工程安全知识库");
}
public void add(ProjectSafety entity) {
entity.setId(IdWorker.getId());
entity.setUpdateTm(new Date());
save(entity);
}
}