All Downloads are FREE. Search and download functionalities are using the official Maven repository.

group.rober.base.param.service.impl.ParamServiceImpl Maven / Gradle / Ivy

The newest version!
package group.rober.base.param.service.impl;

import group.rober.base.param.model.ParamEntry;
import group.rober.base.param.model.ParamItemEntry;
import group.rober.base.param.service.ParamService;
import group.rober.base.param.service.impl.po.ParamItemPO;
import group.rober.base.param.service.impl.po.ParamPO;
import group.rober.runtime.kit.BeanKit;
import group.rober.runtime.lang.TreeNodeWrapper;
import group.rober.sql.core.DataAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ParamServiceImpl implements ParamService {

    @Autowired
    DataAccessor accessor;

    public ParamEntry getParam(String paramCode) {
        ParamPO paramPO = getParamPO(paramCode);
        ParamEntry param = new ParamEntry();
        if (null != paramPO) {
            BeanKit.copyProperties(paramPO, param);
            Map itemMap = getParamItemMap(paramCode);
            param.setItemMap(itemMap);
        }

        return param;
    }

    public List getParamItemsByFilter(String paramCode, String startSort) {
        List paramItemPOList = getParamItemPOListBySortNo(paramCode, startSort);
        List result = new ArrayList();
        if (null != paramItemPOList && !paramItemPOList.isEmpty()) {
            ParamItemEntry paramItem;
            for (ParamItemPO paramItemPO : paramItemPOList) {
                paramItem = new ParamItemEntry();
                BeanKit.copyProperties(paramItemPO, paramItem);
                result.add(paramItem);
            }
        }
        return result;
    }

    public List> getParamItemsAsTreeByFilter(String paramCode, String startSort) {
        List paramItems = getParamItemsByFilter(paramCode, startSort);

        return getParamItemsAsTree(paramItems);
    }

    public List> getParamItemsAsTree(String paramCode) {
        List paramItemPOList = getParamItemPOList(paramCode);
        List paramItems = new ArrayList<>();
        if (null != paramItemPOList && !paramItemPOList.isEmpty()) {
            ParamItemEntry paramItem;
            for (ParamItemPO po : paramItemPOList) {
                paramItem = new ParamItemEntry();
                BeanKit.copyProperties(po, paramItem);
                paramItems.add(paramItem);
            }
        }

        return getParamItemsAsTree(paramItems);
    }

    public List> getParamItemsAsTree(List paramItems) {
        List> paramItemTrees = new ArrayList>();
        List usedSortCodes = new ArrayList<>();
        TreeNodeWrapper paramItemTree;
        for (int i = 0; i < paramItems.size(); i++) {
            ParamItemEntry curParamItem = paramItems.get(i);
            String curSortCode = curParamItem.getSortCode();

            if (usedSortCodes.contains(curSortCode)) {
                continue;
            }
            usedSortCodes.add(curSortCode);

            paramItemTree = new TreeNodeWrapper();
            paramItemTree.setValue(curParamItem);

            List> childrenParamItemTrees =
                    getChildrenParamItemTree(paramItems, usedSortCodes, paramItemTree, curSortCode);
            if (!childrenParamItemTrees.isEmpty()) {
                paramItemTree.setChildren(childrenParamItemTrees);
            }

            paramItemTrees.add(paramItemTree);
        }

        return paramItemTrees;
    }


    private Map getParamItemMap(String paramCode) {
        Map itemPOMap = new HashMap<>();
        List paramItemPOList = getParamItemPOList(paramCode);
        if (null != paramItemPOList && !paramItemPOList.isEmpty()) {
            ParamItemEntry paramItem;
            for (ParamItemPO paramItemPO : paramItemPOList) {
                paramItem = new ParamItemEntry();
                BeanKit.copyProperties(paramItemPO, paramItem);
                itemPOMap.put(paramItemPO.getCode(), paramItem);
            }
        }

        return itemPOMap;
    }

    private ParamPO getParamPO(String paramCode) {
        ParamPO paramPO = accessor.selectOneById(ParamPO.class, paramCode);
        return paramPO;
    }

    private List getParamItemPOList(String paramCode) {
        List paramItemPOList = accessor
                .selectList(ParamItemPO.class, "select * from FOWK_PARAM_ITEM where PARAM_CODE=:paramCode ORDER BY SORT_CODE ASC",
                        "paramCode", paramCode);
        return paramItemPOList;
    }

    private List getParamItemPOListBySortNo(String paramCode, String sortNo) {
        List paramItemPOList = accessor
                .selectList(ParamItemPO.class, "select * from FOWK_PARAM_ITEM where PARAM_CODE=:paramCode and SORT_CODE like CONCAT(:sortNo, '%') ORDER BY SORT_CODE ASC",
                        "paramCode", paramCode, "sortNo", sortNo);
        return paramItemPOList;
    }

    private List> getChildrenParamItemTree(List paramItems, List usedSortCodes,
                                                                           TreeNodeWrapper parent, String sortCode) {
        List> result = new ArrayList>();
        TreeNodeWrapper paramItemTree;
        for (int i = 0; i < paramItems.size(); i++) {
            ParamItemEntry curParamItem = paramItems.get(i);
            String curSortCode = curParamItem.getSortCode();

            if (curParamItem.getSortCode().equals(sortCode) || !curSortCode.startsWith(sortCode)) {
                continue;
            }

            if (usedSortCodes.contains(curSortCode)) {
                continue;
            }
            usedSortCodes.add(curSortCode);

            paramItemTree = new TreeNodeWrapper();
            paramItemTree.setValue(curParamItem);

            List> childrenParamItemTrees =
                    getChildrenParamItemTree(paramItems, usedSortCodes, paramItemTree, curSortCode);
            if (!childrenParamItemTrees.isEmpty()) {
                paramItemTree.setChildren(childrenParamItemTrees);
            }

            result.add(paramItemTree);
        }

        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy