package com.iailab.module.model.mpk.service.impl; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.service.impl.CrudServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.model.mpk.dao.MpkFileDao; import com.iailab.module.model.mpk.dao.ProjectDao; import com.iailab.module.model.mpk.dto.MpkFileDTO; import com.iailab.module.model.mpk.dto.ProjectDTO; import com.iailab.module.model.mpk.dto.ProjectModelDTO; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; import com.iailab.module.model.mpk.entity.ProjectEntity; import com.iailab.module.model.mpk.entity.ProjectPackageHistoryEntity; import com.iailab.module.model.mpk.service.ProjectModelService; import com.iailab.module.model.mpk.service.ProjectPackageHistoryModelService; import com.iailab.module.model.mpk.service.ProjectPackageHistoryService; import com.iailab.module.model.mpk.service.ProjectService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.util.*; import java.util.stream.Collectors; /** * @description: 项目表 * @author: dzd * @date: 2024/8/22 14:40 **/ @Service @Slf4j public class ProjectServiceImpl extends BaseServiceImpl implements ProjectService { @Autowired ProjectModelService projectModelService; @Autowired ProjectPackageHistoryService projectPackageHistoryService; @Autowired ProjectPackageHistoryModelService projectPackageHistoryModelService; @Autowired MpkFileDao mpkFileDao; @Override public PageData page(Map params) { IPage page = baseDao.getPageList( getPage(params, null, false), params ); return getPageData(page, ProjectDTO.class); } @Override public List list(Map params) { List entityList = baseDao.selectList(getWrapper(params).orderByDesc("create_time")); return ConvertUtils.sourceToTarget(entityList, ProjectDTO.class); } @Override public ProjectDTO get(String id) { ProjectEntity entity = baseDao.selectById(id); ProjectDTO dto = ConvertUtils.sourceToTarget(entity, ProjectDTO.class); Map map = new HashMap<>(); map.put("projectId",id); List projectModelList = projectModelService.list(map); dto.setModels(projectModelList.stream().map(e -> { MpkFileDTO mdkFileDTO = new MpkFileDTO(); mdkFileDTO.setId(e.getModelId()); return mdkFileDTO; }).collect(Collectors.toList())); return dto; } @Override @DSTransactional(rollbackFor = Exception.class) public void save(ProjectDTO dto) { ProjectEntity entity = ConvertUtils.sourceToTarget(dto, ProjectEntity.class); String projectId = UUID.randomUUID().toString(); entity.setId(projectId); entity.setCreateTime(new Date()); insert(entity); //关联模型 List models = dto.getModels(); if (CollectionUtils.isNotEmpty(models)) { projectModelService.create(models,projectId); } } @Override @DSTransactional(rollbackFor = Exception.class) public void update(ProjectDTO dto) { ProjectEntity entity = ConvertUtils.sourceToTarget(dto, ProjectEntity.class); entity.setUpdateTime(new Date()); updateById(entity); String projectId = dto.getId(); //删除关联 Map map = new HashMap<>(); map.put("projectId",projectId); projectModelService.deleteByMap(map); //关联模型 List models = dto.getModels(); if (CollectionUtils.isNotEmpty(models)) { projectModelService.create(models,projectId); } } @Override @DSTransactional(rollbackFor = Exception.class) public void delete(String id) { //删除备份文件 Map map = new HashMap<>(); map.put("projectId",id); List list = projectPackageHistoryService.list(map); list.forEach(e -> { File file = new File(e.getFilePath()); if (file.exists()) { file.delete(); log.info("删除打包备份文件:" + e.getFilePath()); } }); //删除 (级联删除) baseDao.deleteById(id); //删除模型关联 // projectModelService.deleteByMap(map); //删除打包历史 // projectPackageHistoryService.deleteByMap(map); //删除打包历史模型关联 // projectPackageHistoryModelService.deleteByMap(map); } @Override public PageData getProjectModel(Map params) { String projectId = (String) params.get("projectId"); int total = mpkFileDao.getProjectModelCount(projectId); if (total == 0) { return new PageData<>(new ArrayList<>(), 0); } int page = Integer.parseInt((String) params.get("page")); int pageSize = Integer.parseInt((String) params.get("pageSize")); int offset = (page - 1)*pageSize; params.put("offset",offset); params.put("pageSize",pageSize); List list = mpkFileDao.getProjectModel(params); return new PageData<>(list,total); } public QueryWrapper getWrapper(Map params){ String id = (String)params.get("id"); String projectName = (String)params.get("projectName"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(id), "id", id) .like(StringUtils.isNotBlank(projectName),"project_name",projectName); return wrapper; } }