skyouc
2025-03-20 ef68e47e7a9f07ad84075504ff3cc8654c299ce7
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
package com.vincent.rsf.server.common.utils;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.framework.common.SpringUtils;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.system.entity.Fields;
import com.vincent.rsf.server.system.entity.FieldsItem;
import com.vincent.rsf.server.system.service.FieldsItemService;
import com.vincent.rsf.server.system.service.FieldsService;
 
import java.util.*;
 
/**
 * @author Ryan
 * @version 1.0
 * @title FieldsUtils
 * @description
 * @create 2025/3/12 12:33
 */
 
public class FieldsUtils {
 
    /**
     * @author Ryan
     * @description 通过字段唯一标识获取动态字段对象key-value
     * @param
     * @return
     * @time 2025/3/12 12:50
     */
    public static Map<String, String> getFields(String uuid) {
        Map<String, String> fieldsMap = new HashMap<>();
        FieldsService fieldsService = SpringUtils.getBean(FieldsService.class);
        List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>().eq(Fields::getFlagEnable, 1).eq(Fields::getStatus, 1));
        if (fields.isEmpty()) {
            return null;
        }
        FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class);
        List<FieldsItem> fieldsItems = fieldsItemService.list(new LambdaQueryWrapper<FieldsItem>().eq(FieldsItem::getUuid, uuid));
        for (Fields field : fields ) {
            if (fieldsItems.isEmpty()) {
                fieldsMap.put(field.getFields(), null);
                continue;
            }
            fieldsItems.forEach(fieldsItem -> {
                if (fieldsItem.getFieldsId().equals(field.getId())) {
                    fieldsMap.put(field.getFields(), fieldsItem.getValue());
                }
            });
        }
 
        return  fieldsMap;
    }
    
    /**
     * @param template
     * @return
     * @author Ryan
     * @description 动态字段value保存
     * @time 2025/3/18 15:00
     */
    public static void saveFields(Map<String, ?> template, String uuid) {
        List<Fields> fields = getFieldsSta();
        FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class);
        if (fields.isEmpty()) {
            throw new CoolException("扩展字段不存在!!");
        }
        List<FieldsItem> fieldsItems = new ArrayList<>();
        for (Fields field : fields) {
            if (!Objects.isNull(template.get(field.getFields()))) {
                FieldsItem item = new FieldsItem();
                item.setFieldsId(field.getId())
                        .setUuid(uuid)
                        .setValue(template.get(field.getFields()).toString());
                fieldsItems.add(item);
            }
        }
       if (!fieldsItemService.saveBatch(fieldsItems)) {
           throw new CoolException("动态字段值保存失败!!");
       }
    }
 
    /**
     * 获取所有开启动态扩展字段
     * @return
     */
    public static List<Fields> getFieldsSta() {
        FieldsService fieldsService = SpringUtils.getBean(FieldsService.class);
        return fieldsService.list(new LambdaQueryWrapper<Fields>().eq(Fields::getStatus, 1).eq(Fields::getFlagEnable, 1));
    }
 
    public static void updateFieldsValue(Map<String, Object> params) {
        List<Fields> fields = getFieldsSta();
        if (fields.isEmpty()) { return; }
        Object fieldsIndex = params.get("fieldsIndex");
        if (!Objects.isNull(fieldsIndex)) {
            String index = fieldsIndex.toString();
            FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class);
            for (Fields field : fields) {
                if (!Objects.isNull(params.get(field.getFields()))) {
                    FieldsItem indexItem = fieldsItemService.getOne(new LambdaQueryWrapper<FieldsItem>()
                            .eq(FieldsItem::getUuid, index)
                            .eq(FieldsItem::getFieldsId, field.getId()));
                    //如果子表为空,执行插入操作,否则就执行修改操作
                    if (Objects.isNull(indexItem)) {
                        FieldsItem item = new FieldsItem();
                        item.setUuid(index)
                                .setFieldsId(field.getId())
                                .setValue(params.get(field.getFields()).toString());
                        if (fieldsItemService.save(item)) {
                            throw new CoolException("扩展字段修改失败!!");
                        }
                    } else {
                        indexItem.setValue(params.get(field.getFields()).toString());
                        if (fieldsItemService.updateById(indexItem)) {
                            throw new CoolException("扩展字段修改失败!!");
                        }
                    }
                }
            }
        } else {
            String uuid16 = CommonUtil.randomUUID16();
            saveFields(params, uuid16);
        }
    }
}