zxl
2025-06-05 0ba453c66cd5d20b970c894358031c9c06df7071
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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<DynamicColumnMapper, DynamicColumn> implements DynamicColumnService {
 
    private final DynamicColumnMapper dynamicColumnMapper;
 
    private final static String TABLE_NAME = "t_yw_point";
 
 
 
    /**
     * 添加
     * @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(new Date()));
        entity.setCreateTime(new Date());
        entity.setTableName(TABLE_NAME);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
 
    @Override
    public Result addByTableName(String tableName,DynamicColumnForm form) {
        Long num = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DynamicColumn::getLabelValue, form.getLabelValue())
                .eq(DynamicColumn::getTableName, tableName)
                .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(tableName);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
 
    /**
     * 修改
     *
     * @param columnList@return
     */
    @Override
    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();
                }
            }
        });
        return Result.ok("保存成功");
    }
 
    @Override
    public Result updateByTableName(String tableName, List<DynamicColumnForm> columnList) {
        columnList.stream().filter(item -> StringUtils.hasText(item.getLabelValue())).forEach(column -> {
            if (Objects.isNull(column.getId())) {
                this.addByTableName(tableName,column);
            } else {
                Long num = new LambdaQueryChainWrapper<>(baseMapper)
                        .eq(DynamicColumn::getTableName, tableName)
                        .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);
        //todo 删除字段对应值
        return Result.ok("删除成功");
    }
 
 
    /**
     * 列表
     * @return
     */
    @Override
    public Result all() {
        List<DynamicColumn> entities = new LambdaQueryChainWrapper<>(baseMapper)
                .orderByAsc(DynamicColumn::getCreateTime)
                .eq(DynamicColumn::getTableName,"t_yw_point")
                .list();
        List<DynamicColumnVO> vos = entities.stream()
                .map(entity -> DynamicColumnVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
 
 
    @Override
    public Result allByTableName(String tableName) {
        List<DynamicColumn> entities = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DynamicColumn::getTableName,tableName)
                .orderByAsc(DynamicColumn::getCreateTime)
                .list();
        List<DynamicColumnVO> vos = entities.stream()
                .map(entity -> DynamicColumnVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
}