1. 程式人生 > 其它 >SpringBoot列舉轉化程式碼項統一解決方案

SpringBoot列舉轉化程式碼項統一解決方案

定義結構及介面

public class CodeItem {
private String _itemText;
private Object _itemValue;

public CodeItem(){

}

public CodeItem(String itemText, Object itemValue){
_itemText = itemText;
_itemValue = itemValue;
}

public String get_itemText() {
return _itemText;
}

public void set_itemText(String _itemText) {
this._itemText = _itemText;
}

public Object get_itemValue() {
return _itemValue;
}

public void set_itemValue(Object _itemValue) {
this._itemValue = _itemValue;
}
}
public interface IConvertEnumToCodeItem {
String getCodeName();
}

規範化列舉定義

定義列舉包含name和value欄位,實現列舉轉換介面,返回程式碼項名稱

public enum EnableOrDisable implements IConvertEnumToCodeItem {
Enable(100, "啟用"), Disabele(300, "禁用");
private int _value;
private String _name;
private EnableOrDisable(int value, String name) {
set_value(value);
set_name((name));
}
public int get_value() {
return _value;
}
public void set_value(int _value) {
this._value = _value;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
@Override
public String toString() {
return _name;
}

@Override
public String getCodeName() {
return "啟用禁用";
}
}

掃描轉換列舉類

掃描專案包下的所有實現IConvertEnumToCodeItem介面需要轉化的列舉類,拿到所有列舉項,呼叫介面的getCodeName方法獲取程式碼項名稱,最後組成程式碼項

public class CodeItemUtil {
private static HashMap<String,List<CodeItem>> codeDic = new HashMap<>();

public static void initCodeItem(Class runtimeClass){
List<Class> filterClassList = new ArrayList<>();
filterClassList.add(IConvertEnumToCodeItem.class);
List<Class<?>> enumClassList = ScanUtil.getAllClassByPackageName(runtimeClass.getPackage(),filterClassList);
try{
for (Class clazz:enumClassList) {
if (clazz.isEnum()){
List<CodeItem> codeItemList = new ArrayList<>();
Object[] oo = clazz.getEnumConstants();
for (Object obj : oo) {
String itemText = "";
Object itemValue = null;
List<Field> fs = Arrays.asList(clazz.getDeclaredFields());
for (Field field:fs) {
field.setAccessible(true);
if(field.getName().equals("_name")){
itemText = ConvertOp.convert2String(field.get(obj));
}else if(field.getName().equals("_value")){
itemValue = field.get(obj);
}
}
if(!StringUtil.isEmpty(itemText) && null!=itemValue){
CodeItem codeItem = new CodeItem(itemText,itemValue);
codeItemList.add(codeItem);
}
}
Method getCodeNameMethod = clazz.getDeclaredMethod("getCodeName");
String codeName = ConvertOp.convert2String(getCodeNameMethod.invoke(oo[0]));
if(codeDic.containsKey(codeName)){
throw new Exception("存在同樣的列舉程式碼項,名稱:"+codeName);
}
codeDic.put(codeName,codeItemList);
}
}
if(codeDic.keySet().size()>0){
System.out.println("掃描並初始化列舉程式碼項成功,程式碼項數量:"+codeDic.keySet().size()+"個");
}
}catch (Exception e){
e.printStackTrace();
}
}

系統啟動掃描

新建class實現ApplicationRunner重寫run方法,通過Order註解確定執行順序

@Component
@Order(value = 99)
public class StartUpContext implements ApplicationRunner {
@Autowired
private ApplicationContext context;

@Override
public void run(ApplicationArguments args) throws Exception {
Map<String, Object> annotatedBeans = context.getBeansWithAnnotation(SpringBootApplication.class);
Class bootClass = annotatedBeans.values().toArray()[0].getClass();
CodeItemUtil.initCodeItem(bootClass);
}
}

實際應用

通用介面

@RestController
@RequestMapping("/common/enumcode")
public class EnumCodeController extends BaseController {
@ResponseBody
@PostMapping("/getCodeDetail")
public Object getCodeDetail(@RequestBody Map<String, Object> params){
String codeName = ConvertOp.convert2String(params.get("codeName"));
if(StringUtil.isEmpty(codeName)){
return Result.errorResult().add("msg","程式碼項名稱不能為空");
}
List<CodeItem> codeItemList = CodeItemUtil.getCodeList(codeName);
if(null==codeItemList){
return Result.errorResult().add("msg","程式碼項不存在");
}else{
return Result.okResult().add("obj",codeItemList);
}

}
}

頁面呼叫

//獲取程式碼項詳細
function bindEnumCode(codeName,callback) {
var codeItemUrl = apiFrameUrl + "/common/enumcode/getCodeDetail";
var data = {
"codeName": codeName
}
JsonAjax_Sync(JSON.stringify(data), codeItemUrl, function (result, status) {
if (callback) {
callback(result, status);
}
})
}
//獲取程式碼項詳細例項
var pageData = {
moduleStatusList: []
}
bindEnumCode("模組狀態", function(result, status) {
Vue.set(pageData, "moduleStatusList", result.data.obj);
});
//列表渲染程式碼項
Vue.prototype.getCodeText = function (page, codeName, codeValue) {
var text = "";
if (!page[codeName]) {
bindEnumCode(codeName, function (result, status) {
page[codeName] = result.data.obj;
})
}
page[codeName].forEach(element => {
if (element.itemValue == codeValue) {
text = element.itemText
}
});
return text;
}

//列表渲染程式碼項例項
<el-table-column prop="moduleStatus" align="center" header-align="center" label="模組狀態" width="100">
<template slot-scope="scope">
<span v-text="getCodeText(this,'模組狀態',scope.row.moduleStatus)"></span>
</template>
</el-table-column>