goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
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
package cn.stylefeng.roses.kernel.system.integration.core.tag;
 
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDictType;
import cn.stylefeng.roses.kernel.system.integration.core.enums.SelectTagHeadTypeEnum;
import lombok.extern.slf4j.Slf4j;
 
import java.io.IOException;
import java.util.List;
 
/**
 * select字典组件
 *
 * @author liuhanqing
 * @date 2021/1/16 19:37
 */
@Slf4j
public class SysDictSelectTag extends SysDictBaseTag {
 
    @Override
    public void render() {
        // 初始属性
        initAttr();
        StringBuilder sb = new StringBuilder();
        // 当字典类型编码不为空
        if (StrUtil.isNotBlank(this.getDictTypeCode())) {
            sb.append("<select name='").append(this.getName())
                    .append("' id='").append(this.getId())
                    .append("'");
 
            // layui 事件过滤器
            if (StrUtil.isNotBlank(this.getLayFilter())) {
                sb.append(" lay-filter='").append(this.getLayFilter()).append("' ");
            }
            // layui 表单验证
            if (StrUtil.isNotBlank(this.getLayVerify())) {
                sb.append(" lay-verify='").append(this.getLayVerify()).append("' ");
            }
            // 原有工作流用到
            if (StrUtil.isNotBlank(this.getWorkflowForm())) {
                sb.append(" workFlowForm='").append(this.getWorkflowForm()).append("' ");
            }
            if (StrUtil.isNotBlank(this.getItemName())) {
                sb.append(" itemName='").append(this.getItemName()).append("' ");
            }
            sb.append(" >");
 
            if (StrUtil.isNotBlank(this.getHeadName())) {
                sb.append("<option value='").append(this.getHeadValue()).append("' selected>")
                        .append(this.getHeadName()).append("</option>");
            } else {
                if (StrUtil.isNotBlank(this.getHeadType())) {
                    if (SelectTagHeadTypeEnum.ALL.getCode().equals(this.getHeadType())) {
                        sb.append("<option value='").append(this.getHeadValue()).append("' selected>")
                                .append(" - 全部 - ").append("</option>");
                    }
                    if (SelectTagHeadTypeEnum.SELECT.getCode().equals(this.getHeadType())) {
                        sb.append("<option value='").append(this.getHeadValue()).append("' selected>")
                                .append(" - 请选择 - ").append("</option>");
                    }
                }
            }
            // 根据字典类型编码去查询字典类型
            SysDictType dictType = getDictType();
            // 判断字典类型不为空
            if (dictType != null) {
                List<SysDict> lst = getDictList();
                // 默认选中值
                String defaultValue = this.getDefaultValue();
                // 循环字典列表,添加下拉选项
                int index = 0;
                for (SysDict option : lst) {
                    sb.append("<option value='").append(option.getDictCode()).append("'");
                    // 设置默认选中值
                    if (StrUtil.isNotBlank(defaultValue) && defaultValue.equals(option.getDictCode())) {
                        sb.append(" selected ");
                    } else {
                        // 未设置headName和headType 默认选中第一个
                        if (index == 0 && StrUtil.isNotBlank(this.getHeadName()) && StrUtil.isNotBlank(this.getHeadType())) {
                            sb.append(" selected ");
                        }
                    }
                    sb.append(">").append(option.getDictName()).append("</option>");
                    index++;
                }
            }
            sb.append("</select>");
        }
        try {
            this.ctx.byteWriter.writeString(sb.toString());
        } catch (IOException e) {
            log.error("select字典初始化异常:",e);
        }
    }
}