| | |
| | | package com.ycl.platform.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | 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.platform.domain.query.DynamicColumnQuery; |
| | | import com.ycl.system.Result; |
| | | import com.ycl.system.page.PageUtil; |
| | | import com.ycl.utils.uuid.IdUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.util.Assert; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | private final DynamicColumnMapper dynamicColumnMapper; |
| | | |
| | | private final static String TABLE_NAME = "t_monitor"; |
| | | |
| | | /** |
| | | * 添加 |
| | | * @param form |
| | |
| | | */ |
| | | @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(new Date())); |
| | | entity.setCreateTime(new Date()); |
| | | entity.setTableName(TABLE_NAME); |
| | | baseMapper.insert(entity); |
| | | return Result.ok("添加成功"); |
| | | } |
| | | |
| | | /** |
| | | * 修改 |
| | | * @param form |
| | | * @return |
| | | * |
| | | * @param columnList@return |
| | | */ |
| | | @Override |
| | | public Result update(DynamicColumnForm form) { |
| | | DynamicColumn entity = baseMapper.selectById(form.getId()); |
| | | public Result update(List<DynamicColumnForm> 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(); |
| | | } |
| | | |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | BeanUtils.copyProperties(form, entity); |
| | | baseMapper.updateById(entity); |
| | | return Result.ok("修改成功"); |
| | | } |
| | | |
| | | /** |
| | | * 批量删除 |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result remove(List<String> ids) { |
| | | baseMapper.deleteBatchIds(ids); |
| | | return Result.ok("删除成功"); |
| | | } |
| | | }); |
| | | return Result.ok("保存成功"); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分页查询 |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result page(DynamicColumnQuery query) { |
| | | IPage<DynamicColumnVO> page = PageUtil.getPage(query, DynamicColumnVO.class); |
| | | baseMapper.getPage(page, query); |
| | | return Result.ok().data(page.getRecords()).total(page.getTotal()); |
| | | } |
| | | |
| | | /** |
| | | * 根据id查找 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result detail(Integer id) { |
| | | DynamicColumnVO vo = baseMapper.getById(id); |
| | | Assert.notNull(vo, "记录不存在"); |
| | | return Result.ok().data(vo); |
| | | } |
| | | |
| | | /** |
| | | * 列表 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result all() { |
| | | List<DynamicColumn> entities = baseMapper.selectList(null); |
| | | List<DynamicColumn> entities = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .orderByAsc(DynamicColumn::getCreateTime) |
| | | .list(); |
| | | List<DynamicColumnVO> vos = entities.stream() |
| | | .map(entity -> DynamicColumnVO.getVoByEntity(entity, null)) |
| | | .collect(Collectors.toList()); |