陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.mzl.flower.service.flower;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.flower.*;
import com.mzl.flower.dto.response.flower.FlowerParamDTO;
import com.mzl.flower.dto.response.flower.FlowerParamItemDTO;
import com.mzl.flower.dto.response.flower.FlowerParamListDTO;
import com.mzl.flower.dto.response.flower.ParamItemDTO;
import com.mzl.flower.entity.flower.FlowerCategory;
import com.mzl.flower.entity.flower.FlowerParam;
import com.mzl.flower.entity.flower.FlowerParamItem;
import com.mzl.flower.mapper.flower.FlowerCategoryMapper;
import com.mzl.flower.mapper.flower.FlowerParamItemMapper;
import com.mzl.flower.mapper.flower.FlowerParamMapper;
import com.mzl.flower.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
@Slf4j
@Service
@Transactional
public class FlowerParamService extends BaseService {
 
    @Autowired
    private FlowerParamMapper paramMapper;
 
    @Autowired
    private FlowerParamItemMapper paramItemMapper;
 
    @Autowired
    private FlowerCategoryMapper categoryMapper;
 
    private final String table_name_pre = "flower_param_";
    private final String table_column_pre = "field_";
 
    public Long addParam(FlowerParamCreateDTO dto){
        FlowerParam g = new FlowerParam();
 
        BeanUtils.copyProperties(dto, g);
        g.create(SecurityUtils.getUserId());
 
        paramMapper.insert(g);
 
        createTable(getTableName(g.getId()), "商品参数表_" + g.getName());
 
        updateCategoryParam(g.getId(), dto.getCategoryIds());
 
        return g.getId();
    }
 
    private void updateCategoryParam(Long id, List<Long> categoryIds){
        categoryMapper.clearParamByParamId(id);
        if(categoryIds != null && categoryIds.size() > 0) {
            categoryMapper.setParamById(id, categoryIds);
        }
    }
 
    private void createTable(String tableName, String tableComment){
        Map<String, Object> tableMap = new HashMap<>();
        tableMap.put("name", tableName);
        tableMap.put("label", tableComment);
 
        List<ParamTableColumn> columns = new ArrayList<>();
        ParamTableColumn c = new ParamTableColumn();
        c.setColumn("id");
        c.setType("bigint(25)");
        c.setPrimaryKey(true);
        c.setComment("商品主键");
        columns.add(c);
        tableMap.put("fields", columns);
 
        paramMapper.createTable(tableMap);
    }
 
    public Long updateParam(FlowerParamUpdateDTO dto){
        FlowerParam g = paramMapper.selectById(dto.getId());
 
        BeanUtils.copyProperties(dto, g);
        g.update(SecurityUtils.getUserId());
 
        paramMapper.updateById(g);
 
        updateCategoryParam(g.getId(), dto.getCategoryIds());
 
        return g.getId();
    }
 
    public FlowerParamDTO getParam(Long id){
        FlowerParam g = paramMapper.selectById(id);
        FlowerParamDTO dto = new FlowerParamDTO();
        BeanUtils.copyProperties(g, dto);
 
        List<FlowerCategory> ls = categoryMapper.selectList(new QueryWrapper<FlowerCategory>().eq("param_id", id));
        List<Long> categoryIds = new ArrayList<>();
        List<String> categoryLs = new ArrayList<>();
        if(ls != null && ls.size() > 0){
            for(FlowerCategory c : ls){
                categoryIds.add(c.getId());
                categoryLs.add(c.getName());
            }
        }
        dto.setCategoryIds(categoryIds);
        dto.setCategories(String.join(",", categoryLs));
 
        return dto;
    }
 
    public Page<FlowerParamListDTO> selectParamList(Page page, FlowerParamQueryDTO dto){
        List<FlowerParamListDTO> ls = paramMapper.selectParamList(page, dto);
 
        page.setRecords(ls);
 
        return page;
    }
 
    public void deleteParam(Long id){
        paramMapper.deleteById(id);
        categoryMapper.clearParamByParamId(id);
        paramItemMapper.delete(new QueryWrapper<FlowerParamItem>().eq("param_id", id));
    }
 
    private String getTableName(Long id){
        return table_name_pre + id;
    }
 
    private String getTableColumn(Long id){
        return table_column_pre + id;
    }
 
    public List<FlowerParamItemDTO> getParamItems(Long id){
        List<FlowerParamItem> ls = paramItemMapper.selectList(
                new QueryWrapper<FlowerParamItem>().eq("param_id", id).orderByAsc("sort_by"));
        List<FlowerParamItemDTO> result = new ArrayList<>();
        if(ls != null && ls.size() > 0){
            for(FlowerParamItem t : ls){
                FlowerParamItemDTO dto = new FlowerParamItemDTO();
                BeanUtils.copyProperties(t, dto);
 
                result.add(dto);
            }
        }
 
        return result;
    }
 
    public FlowerParamItemDTO getParamItem(Long id){
        FlowerParamItem t = paramItemMapper.selectById(id);
        FlowerParamItemDTO dto = new FlowerParamItemDTO();
        BeanUtils.copyProperties(t, dto);
 
        return dto;
    }
 
    public Long addParamItem(FlowerParamItemCreateDTO dto){
        FlowerParamItem t = new FlowerParamItem();
        BeanUtils.copyProperties(dto, t);
        t.create(SecurityUtils.getUserId());
 
        paramItemMapper.insert(t);
 
        ParamTableColumn c = new ParamTableColumn();
        c.setColumn(getTableColumn(t.getId()));
        c.setType("varchar(255)");
        c.setPrimaryKey(false);
        c.setComment(t.getName());
        paramItemMapper.addTableColumn(getTableName(t.getParamId()), c);
 
        return t.getId();
    }
 
    public Long updateParamItem(FlowerParamItemUpdateDTO dto){
        FlowerParamItem t = paramItemMapper.selectById(dto.getId());
        BeanUtils.copyProperties(dto, t);
        t.update(SecurityUtils.getUserId());
 
        paramItemMapper.updateById(t);
 
        return t.getId();
    }
 
    public void deleteParamItem(Long id){
        paramItemMapper.deleteById(id);
    }
 
    public List<ParamItemDTO> getParamItemListByCategoryId(Long categoryId){
        FlowerCategory fc = categoryMapper.selectById(categoryId);
        if(fc == null){
            throw new ValidationException("分类不存在");
        }
 
        if(fc.getParamId() == null){
            return new ArrayList<>();
        }
 
        return getParamItemListByParamId(fc.getParamId());
    }
 
    public List<ParamItemDTO> getParamItemListByParamId(Long paramId){
        List<ParamItemDTO> result = new ArrayList<>();
        List<FlowerParamItem> ls = paramItemMapper.selectList(
                new QueryWrapper<FlowerParamItem>().eq("param_id", paramId).orderByAsc("sort_by"));
        if(ls != null && ls.size() > 0){
            for(FlowerParamItem t : ls){
                ParamItemDTO d = new ParamItemDTO();
                d.setId(t.getId());
                d.setColumn(getTableColumn(t.getId()));
                d.setName(t.getName());
                d.setValues(splitParam(t.getContent()));
                result.add(d);
            }
        }
 
        return result;
    }
 
    public void insertTableData(Long paramId, Long id, List<ParamDTO> columns){
        List<String> columnList = new ArrayList<>();
        List<Object> valueList = new ArrayList<>();
 
        columnList.add("id");
        valueList.add(id);
 
        for (ParamDTO f : columns){
            Long fId = f.getId();
            columnList.add(getTableColumn(fId));
            valueList.add(f.getValue());
        }
 
        String tableName = getTableName(paramId);
        paramMapper.insertTableData(tableName, columnList, valueList);
    }
 
    public void updateTableData(Long paramId, Long id, List<ParamDTO> columns){
        List<Map> valueMapList = new ArrayList<>();
        for (ParamDTO f : columns){
            Long fId = f.getId();
            Map<String, Object> map = new HashMap<>();
            map.put("column", getTableColumn(fId));
            map.put("value", f.getValue());
            valueMapList.add(map);
        }
 
        String tableName = getTableName(paramId);
        paramMapper.updateTableDataById(tableName, valueMapList, "id", id);
    }
 
    public Map getTableDataById(Long paramId, Long id){
        String tableName = getTableName(paramId);
        return paramMapper.getTableDataById(tableName, id);
    }
 
    public Long getParamIdByCategoryId(Long categoryId){
        if(categoryId == null){
            return null;
        }
        FlowerCategory c = categoryMapper.selectById(categoryId);
        return c.getParamId();
    }
}