package com.ruoyi.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.utils.MapUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.domain.ArchiveAnnotation; import com.ruoyi.domain.ArchiveSignature; import com.ruoyi.mapper.ArchiveAnnotationMapper; import com.ruoyi.service.IArchiveAnnotationService; import com.ruoyi.util.ErrorcodeExceptionextends; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.*; @Service public class ArchiveAnnotationServiceImpl extends ServiceImpl implements IArchiveAnnotationService { private LambdaQueryWrapper buildCondition(ArchiveAnnotation archiveAnnotation) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.like(!StringUtils.isEmpty(archiveAnnotation.getName()), ArchiveAnnotation::getName, archiveAnnotation.getName()); lqw.orderByDesc(ArchiveAnnotation::getSrt); return lqw; } @Override public ArchiveAnnotation selectArchiveAnnotationById(Long id) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(id != null, ArchiveAnnotation::getId, id); List lists = this.list(lqw); if (!lists.isEmpty()) return lists.get(0); else return null; } @Override public AjaxResult selectArchiveAnnotationList(ArchiveAnnotation archiveAnnotation, Integer pageNum, Integer pageSize) { LambdaQueryWrapper lqw = buildCondition(archiveAnnotation); Page archiveSignaturePage = new Page<>(pageNum, pageSize); Page pageResult = page(archiveSignaturePage, lqw); List beanRecords = pageResult.getRecords();//得到查询出来的数据 // List beanRecords = list(lqw); // log.info("从数据库中查到的为:{}", beanRecords); // return markOwnData(familyId, fatherFaId, motherFaId, beanRecords); HashMap data = MapUtils.getResult(pageResult, beanRecords); return AjaxResult.success(data); } @Override public List selectArchiveAnnoList(ArchiveAnnotation archiveAnnotation) { return Collections.emptyList(); } @Override public int insertArchiveAnnotation(ArchiveAnnotation archiveAnnotation) { LocalDateTime time = LocalDateTime.now(); if (archiveAnnotation.getName() == null || StringUtils.isEmpty(archiveAnnotation.getName())) throw new RuntimeException("注解名称不能为空!"); Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant()); archiveAnnotation.setCreateTime(date); // 根据注解名称查询,是否已经存在,存在的话不让插入 LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(!StringUtils.isEmpty(archiveAnnotation.getName()), ArchiveAnnotation::getName, archiveAnnotation.getName()); List lis = list(lambdaQueryWrapper); if (!lis.isEmpty()) { return 0; } boolean res = this.save(archiveAnnotation); // 0表示失败,1表示成功 if (res) return 1; else return 0; } @Override public int updateArchiveAnnotation(ArchiveAnnotation archiveAnnotation) { boolean result = false; try { // 使用LambdaUpdateWrapper构造更新条件,确保null值也能更新到数据库 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.eq(ArchiveAnnotation::getId, archiveAnnotation.getId()); // 明确设置需要更新的字段 updateWrapper.set(ArchiveAnnotation::getName, archiveAnnotation.getName()); updateWrapper.set(ArchiveAnnotation::getSrt, archiveAnnotation.getSrt()); updateWrapper.set(ArchiveAnnotation::getCreateTime, archiveAnnotation.getCreateTime()); // 执行更新操作 result = update(updateWrapper); } catch (Exception e) { System.out.println(e); throw new ErrorcodeExceptionextends(500, "不允许注解名称重复!"); } if (result) return 1; else return 0; } @Override public int deleteArchiveAnnotationByIds(Long[] ids) { if (this.removeByIds(Arrays.asList(ids))) { return 1; } else { return 0; } } @Override public int deleteArchiveAnnotationById(Long id) { return this.baseMapper.deleteById(id); } }