package com.ycl.platform.service.impl; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import com.ycl.platform.domain.entity.DynamicColumn; import com.ycl.platform.mapper.DynamicColumnMapper; import com.ycl.platform.service.DynamicColumnService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.platform.domain.form.DynamicColumnForm; import com.ycl.platform.domain.vo.DynamicColumnVO; import com.ycl.system.Result; import com.ycl.utils.uuid.IdUtils; import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; import org.springframework.util.StringUtils; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 动态列 服务实现类 * * @author xp * @since 2024-08-16 */ @Service @RequiredArgsConstructor public class DynamicColumnServiceImpl extends ServiceImpl implements DynamicColumnService { private final DynamicColumnMapper dynamicColumnMapper; private final static String TABLE_NAME = "t_monitor"; /** * 添加 * @param form * @return */ @Override public Result add(DynamicColumnForm form) { Long num = new LambdaQueryChainWrapper<>(baseMapper) .eq(DynamicColumn::getLabelValue, form.getLabelValue()) .eq(DynamicColumn::getTableName, TABLE_NAME) .count(); if (num > 0) { throw new RuntimeException("列名称不能重复"); } DynamicColumn entity = DynamicColumnForm.getEntityByForm(form, null); entity.setPropName(IdUtils.randomNO()); entity.setCreateTime(new Date()); entity.setTableName(TABLE_NAME); baseMapper.insert(entity); return Result.ok("添加成功"); } /** * 修改 * * @param columnList@return */ @Override public Result update(List columnList) { columnList.stream().filter(item -> StringUtils.hasText(item.getLabelValue())).forEach(column -> { if (Objects.isNull(column.getId())) { this.add(column); } else { Long num = new LambdaQueryChainWrapper<>(baseMapper) .eq(DynamicColumn::getTableName, TABLE_NAME) .eq(DynamicColumn::getLabelValue, column.getLabelValue()) .ne(DynamicColumn::getId, column.getId()) .count(); if (num == 0) { new LambdaUpdateChainWrapper<>(baseMapper) .eq(DynamicColumn::getId, column.getId()) .set(DynamicColumn::getLabelValue, column.getLabelValue()) .update(); } } }); return Result.ok("保存成功"); } /** * id删除 * @param id * @return */ @Override public Result removeById(String id) { baseMapper.deleteById(id); return Result.ok("删除成功"); } /** * 列表 * @return */ @Override public Result all() { List entities = new LambdaQueryChainWrapper<>(baseMapper) .orderByAsc(DynamicColumn::getCreateTime) .list(); List vos = entities.stream() .map(entity -> DynamicColumnVO.getVoByEntity(entity, null)) .collect(Collectors.toList()); return Result.ok().data(vos); } }