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
package com.vincent.rsf.server.system.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.common.utils.CommonUtil;
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 org.springframework.beans.BeanUtils;
 
import java.util.*;
 
/**
 * @author Ryan
 * @version 1.0
 * @title ExtendFieldsUtils
 * @description
 * @create 2025/3/15 12:40
 */
public class ExtendFieldsUtils {
 
    /**
     * @author Ryan
     * @description 明细单据保存扩展字段
     * @param
     * @return
     * @time 2025/3/15 13:36
     */
    public static Boolean saveFields(Map<String, Object> params) {
        FieldsService fieldsService = SpringUtils.getBean(FieldsService.class);
        List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>()
                .eq(Fields::getStatus, 1)
                .eq(Fields::getFlagEnable, 1));
        List<FieldsItem> fieldsItems = new ArrayList<>();
        if (!fields.isEmpty()) {
            String uuid16 = CommonUtil.randomUUID16();
            for (Fields obj : fields) {
                if (!Objects.isNull(params.get(obj.getFields()))) {
                    FieldsItem item = new FieldsItem();
                    item.setUuid(uuid16)
                            .setValue(params.get(obj.getFields()).toString())
                            .setMatnrId(Long.parseLong(params.get("matnrId").toString()))
                            .setFieldsId(obj.getId());
                    fieldsItems.add(item);
                }
                params.put("fieldsIndex", uuid16);
            }
            FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class);
            if (!fieldsItemService.saveBatch(fieldsItems)) {
                throw new CoolException("单据明细扩展字段保存失败!!");
            }
        }
        return true;
    }
 
 
    /**
     * @author Ryan
     * @description 获取扩展字段key-value值
     * @param
     * @return
     * @time 2025/3/15 15:05
     */
    public static List<Map<String, Object>> getExtendFields(List<Map<String, Object>> params) {
        FieldsService fieldsService = SpringUtils.getBean(FieldsService.class);
        List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>()
                .eq(Fields::getStatus, 1)
                .eq(Fields::getFlagEnable, 1));
        FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> param : params) {
            result.add(param);
            if (Objects.isNull(param.get("fieldsIndex"))) {
                continue;
            }
            List<FieldsItem> itemList = fieldsItemService
                    .list(new LambdaQueryWrapper<FieldsItem>()
                            .eq(FieldsItem::getUuid, param.get("fieldsIndex")));
            if (itemList.isEmpty()) {
                continue;
            }
            fields.forEach(fds -> {
                for (FieldsItem fieldsItem : itemList) {
                    if (!Objects.isNull(fieldsItem.getFieldsId()) && fieldsItem.getFieldsId().equals(fds.getId())) {
                        param.put(fds.getFields(), fieldsItem.getValue());
                    }
                }
            });
        }
 
        return result;
    }
}