我感覺這是我寫過的最有價值的util(前端配置表於表之間的關係,後端根據配置信息進行 表於表之間的數據互動 )

寫了一天的代碼了,休息一下總結一下。

這段代碼的背景是 通過前端配置表於表之間的關係,後端根據配置信息進行 表於表之間的數據互動 

1、通過前端的配置信息封裝一個 list<dto> dto裏面存着 對應的表名,對應表的實體類屬性關係

2、根據前端傳來的原始VO,通過反射 以實體屬性作爲key 屬性value作爲value 存儲一個voMap

3、創建一個paramMap,根據封裝好的 list<dto> 和 voMap進行 封裝paramMap

4、最後就可以拿着paramMap去插入數據庫了

getInsertParam()直接調用這個方法就可以獲得paramMap

package nbpt.ts.zhaf.util;

import nbpt.ts.zhaf.domain.dto.StoreGoodsActionDTO;
import nbpt.ts.zhaf.util.enums.TableEnum;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表之間的數據互通說明(主表-子表;子表-主表 都可互相轉換對應):
 * 一、後臺根據配置添加對應表的思路(子錶轉換主表):
 * 1、調用ActionUtil.storeGoodsActionAllTable(配置,表名)方法返回 List<List<StoreGoodsActionDTO>> actionList
 * 	a)拿到封裝好的配置
 * 		a. sgaProperty;//主表字段屬性
 * 		b. otherProperty;//其他表的字段屬性
 * 		c. ortherTableName;//其他表的名稱
 *
 * 2、調用ActionUtil.getParamList(空map,類)通過反射獲取類中的屬性,屬性不爲空的 以屬性名稱作爲key,屬性值作爲value存儲一個map(在此取名params)
 * 3、創建一個map作爲insert的param
 * 4、循環actionList 根據 sgaProperty;//主表字段屬性  對應的 otherProperty;//其他表的字段屬性進行
 *      組裝map(在此取名paramMap)  以sgaProperty;//主表字段屬性作爲key 通過對應的 otherProperty;//其他表的字段屬性 獲取 反射組裝的map(params)中的值
 * 5、將組裝好的paramMap傳給 mapper層進行數據庫操作
 *
 * 二、前端根據配置回顯form表單的操作(主錶轉換子表)
 * 1、調用ActionUtil.storeGoodsActionAllTable(配置,表名)方法返回 List<List<StoreGoodsActionDTO>> actionList 通過ModelMap傳輸給前臺
 * 2、前端代碼示例根據 表之間屬性對應的list 進行 根據jquery組裝回顯表單
 * var storeGoodsAction = ${t_ordergoods_peijian};
 * for (var i = 0; i < storeGoodsAction.length; i++) {
 *      var itemProperty = storeGoodsAction[i].sgaProperty//主表字段屬性
 *      if(item[itemProperty]==""){
 *          $('#' + storeGoodsAction[i].otherProperty + '').val("暫無");
 *      }else{
 *          $('#' + storeGoodsAction[i].otherProperty + '').val(item[itemProperty]);
 *      }
 * }
 */
public class ActionUtil {

    /**
     *子表insert主表
     * @param action 配置的對應關係
     * @param tableKey 對應的表名稱
     * @param paramClass 參數的class
     * @param companyid 公司id
     * @return
     * @throws Exception
     */
    public static Map<String,Object> getMainInsertParam(String action,String tableKey,Object paramClass,Integer companyid) throws Exception {
        List<List<StoreGoodsActionDTO>> actionList = ActionUtil.storeGoodsActionAllTable(action, tableKey);
        Map params = new HashMap<String, Object>();
        params = ActionUtil.getParamList(params, paramClass);
        Map<String, Object> paramMap = new HashMap<>();
        for (StoreGoodsActionDTO storeGoodsActionDTO : actionList.get(0)) {
            //封裝map
            paramMap.put(storeGoodsActionDTO.getOtherProperty(), params.get(storeGoodsActionDTO.getSgaProperty()));
        }
        paramMap.put("companyid", companyid);
        return  paramMap;
    }

    /**
     *主表insert子表
     * @param action 配置的對應關係
     * @param tableKey 對應的表名稱
     * @param paramClass 參數的class
     * @param companyid 公司id
     * @return
     * @throws Exception
     */
    public static Map<String,Object> getChildInsertParam(String action,String tableKey,Object paramClass,Integer companyid) throws Exception {
        List<List<StoreGoodsActionDTO>> actionList = ActionUtil.storeGoodsActionAllTable(action, tableKey);
        Map params = new HashMap<String, Object>();
        params = ActionUtil.getParamList(params, paramClass);
        Map<String, Object> paramMap = new HashMap<>();
        for (StoreGoodsActionDTO storeGoodsActionDTO : actionList.get(0)) {
            //封裝map
            paramMap.put(storeGoodsActionDTO.getSgaProperty(), params.get(storeGoodsActionDTO.getOtherProperty()));
        }
        paramMap.put("companyid", companyid);
        return  paramMap;
    }

    /**
     *
     * @param action
     * @param ortherTableNameVO 對應其他表的表名(不爲空時查詢指定的        封裝成List<List<StoreGoodsActionDTO>>
     *                                              爲null/""時查詢所有表配置 封裝成List<List<StoreGoodsActionDTO>>)
     * @return
     * @throws Exception
     */
    public static List<List<StoreGoodsActionDTO>> storeGoodsActionAllTable(String action, String ortherTableNameVO) throws Exception {
        List<List<StoreGoodsActionDTO>> allTableAction = new ArrayList<>();
        String[] tableAction = action.split(";");
        for (int a = 0; a < tableAction.length; a++) {
            List<StoreGoodsActionDTO> tableActionList = new ArrayList<>();
            String[] tablePropertys = tableAction[a].split("\\|");
            if(StringUtils.isEmpty(ortherTableNameVO)){
                allTableAction = get(tablePropertys,tableAction,tableActionList,allTableAction);
            }else if (ortherTableNameVO.equals(tablePropertys[0])) {
                allTableAction = get(tablePropertys,tableAction,tableActionList,allTableAction);
            }
        }
        return allTableAction;
    }

    public static List<List<StoreGoodsActionDTO>> get(String[] tablePropertys, String[] tableAction, List<StoreGoodsActionDTO> tableActionList, List<List<StoreGoodsActionDTO>> allTableAction) throws Exception {
        if (tablePropertys.length != 3) {
            throw new Exception("第" + tableAction[0] + "個表關聯 配置格式有問題!");
        }
        String[] tableOtherPropertys = tablePropertys[1].split(",");
        String[] tableStoreGoodsPropertys = tablePropertys[2].split(",");
        if (tableOtherPropertys.length != tableStoreGoodsPropertys.length) {
            throw new Exception(tablePropertys[0] + "表配置字段無法對應,請配置相對應的字段!");
        }

        String ortherTableName = tablePropertys[0];
        for (int i = 0; i < tableOtherPropertys.length; i++) {
            StoreGoodsActionDTO correspondingDTO = new StoreGoodsActionDTO();
            correspondingDTO.setOrtherTableName(ortherTableName);
            correspondingDTO.setOtherProperty(tableOtherPropertys[i]);
            correspondingDTO.setSgaProperty(tableStoreGoodsPropertys[i]);
            tableActionList.add(correspondingDTO);
        }
        allTableAction.add(tableActionList);
        return allTableAction;
    }


    /**
     *
     * @param params 空map用於返回 和 以後的擴展
     * @param vo 帶有參數的vo
     * @return
     */
    public static Map<String, Object> getParamList(Map<String, Object> params, Object vo) {
        Field[] vos = vo.getClass().getDeclaredFields();
        for (Field field : vos) {
            field.setAccessible(true);
            try {
                if (!StringUtils.isEmpty(field.get(vo))) {
                    params.put(field.getName(),field.get(vo));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return params;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章