package com.iailab.module.knowledge.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.framework.common.constant.Constant; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.knowledge.dao.KnowledgeDetDao; import com.iailab.module.knowledge.dto.KnowledgeDetDTO; import com.iailab.module.knowledge.entity.KnowledgeDetEntity; import com.iailab.module.knowledge.service.KnowledgeDetService; import com.iailab.module.oss.dto.OssFileDTO; import com.iailab.module.oss.service.OssFileService; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2023年12月25日 17:33:00 */ @Service public class KnowledgeDetServiceImpl extends BaseServiceImpl implements KnowledgeDetService { @Resource private OssFileService ossAppService; private final String BUSINESS_TYPE = "knowledge"; @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, Constant.CREATE_DATE, false), getWrapper(params) ); PageData result = getPageData(page, KnowledgeDetDTO.class); result.getList().forEach(item -> { List fileDTOS = ossAppService.getByBusinessId(item.getId()); if (!CollectionUtils.isEmpty(fileDTOS)) { OssFileDTO fileDTO = fileDTOS.get(0); item.setFileName(fileDTO.getFileName()); item.setFilePostfix(fileDTO.getFilePostfix()); item.setUrl(fileDTO.getUrl()); } }); return result; } private QueryWrapper getWrapper(Map params) { String treeId = (String) params.get("treeId"); String title = (String) params.get("title"); String keyWords = (String) params.get("keyWords"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(treeId), "tree_id", treeId) .like(StringUtils.isNotBlank(title), "title", title) .like(StringUtils.isNotBlank(keyWords), "key_words", keyWords); return wrapper; } @Override public KnowledgeDetDTO get(String id) { KnowledgeDetEntity entity = baseDao.selectById(id); KnowledgeDetDTO dto = ConvertUtils.sourceToTarget(entity, KnowledgeDetDTO.class); List fileDTOS = ossAppService.getByBusinessId(dto.getId()); if (!CollectionUtils.isEmpty(fileDTOS)) { OssFileDTO fileDTO = fileDTOS.get(0); dto.setFileName(fileDTO.getFileName()); dto.setFilePostfix(fileDTO.getFilePostfix()); dto.setUrl(fileDTO.getUrl()); } return dto; } @Override @Transactional(rollbackFor = Exception.class) public void save(KnowledgeDetDTO dto) { KnowledgeDetEntity entity = ConvertUtils.sourceToTarget(dto, KnowledgeDetEntity.class); entity.setId(UUID.randomUUID().toString()); entity.setCreateDate(new Date()); insert(entity); OssFileDTO fileDTO = new OssFileDTO(); fileDTO.setBusinessId(entity.getId()); fileDTO.setType(BUSINESS_TYPE); fileDTO.setFileName(dto.getFileName()); fileDTO.setFilePostfix(dto.getFilePostfix()); fileDTO.setUrl(dto.getUrl()); fileDTO.setSort(0); ossAppService.save(fileDTO); } @Override public void update(KnowledgeDetDTO dto) { KnowledgeDetEntity entity = ConvertUtils.sourceToTarget(dto, KnowledgeDetEntity.class); updateById(entity); } @Override @Transactional(rollbackFor = Exception.class) public void delete(String[] ids) { baseDao.deleteBatchIds(Arrays.asList(ids)); } }