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

com.ajaxjs.website.section.TreeLikeService Maven / Gradle / Ivy

Go to download

AJAXJS aims to full-stack, not only the server-side framework, but also integrates the front-end library. It'€™s written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated, emphasizing the lightweight, and fast, very suitable for the Internet fast application.

The newest version!
package com.ajaxjs.website.section;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.ajaxjs.framework.BaseModel;
import com.ajaxjs.framework.BaseService;
import com.ajaxjs.sql.JdbcConnection;
import com.ajaxjs.sql.orm.Repository;

@Component
public class TreeLikeService extends BaseService {
	public static TreeLikeoDao DAO = new Repository().bind(TreeLikeoDao.class);

	{
		setUiName("分类");
		setShortName("catalog");
		setDao(DAO);
	}

	/**
	 * 获取 pid 下面直接一级的子节点,
	 * 
	 * @param pid
	 * @return
	 */
	public List getDirectChildren(int pid) {
		return DAO.findList(by("pid", pid));
	}

	/**
	 * 获取全部的分类
	 * 
	 * @return
	 */
	public List getAllChildren() {
		return DAO.findList(sql -> sql + " ORDER BY pid");
	}

	/**
	 * 获取 pid 下面的所有子节点,无论下面有多少级
	 * 
	 * @param pid
	 * @return
	 */
	public List getAllChildren(int pid) {
		return DAO.getAllChildren(pid);
	}

	/**
	 * 转换为适合前端显示的 map
	 * 
	 * @param pid
	 * @return
	 */
	public Map getAllChildrenAsMap(int pid) {
		return idAskey(getAllChildren(pid));
	}

	/**
	 * 把列表(BaseModel 结构)转换为 map,以 id 作为键值。key 本来是 long,为照顾 el 转换为 int
	 * 
	 * @param list 实体列表
	 * @return 以 id 作为键值的 map
	 */
	public static Map idAskey(List list) {
		if (CollectionUtils.isEmpty(list))
			return null;

		Map map = new HashMap<>();
		list.forEach(item -> map.put(item.getId(), item));

		return map;
	}

	/**
	 * 把列表(Map结构)转换为 map,以 id 作为键值。key 本来是 long,为照顾 el 转换为 int
	 * 
	 * @param list 实体列表
	 * @return 以 id 作为键值的 map
	 */
	public static Map idAsKey(List> list) {
		if (CollectionUtils.isEmpty(list))
			return null;

		Map map = new HashMap<>();
		list.forEach(item -> map.put(new Integer(item.get("id").toString()), item));

		return map;
	}

	@Override
	public Long create(TreeNode bean) {
		if (bean.getPid() != -1) { // 非根节点
			TreeNode parent = findById(bean.getPid().longValue()); // 保存路径信息

			String path = "";

			// if (parent.getPid() != -1)
			path += parent.getPath();

			path += "/";
			bean.setPath(path);
		}

		Long newlyId = super.create(bean);

		if (newlyId != null) { // 需要创建了之后才有自己的 id
			TreeNode updatePath = new TreeNode();
			updatePath.setId(bean.getId());
			updatePath.setPath((bean.getPid() == -1 ? "/" : bean.getPath()) + bean.getId());

			update(updatePath);
		}

		return newlyId;
	}

	@Override
	public boolean delete(TreeNode bean) {
		return DAO.deleteAll(bean.getId().intValue());
	}

	/**
	 * 供其它实体关联时候用,可以获取下级所有子分类
	 */
	public final static String PATH_LIKE_MYSQL_ID = "SELECT id FROM common_catalog WHERE `path` LIKE ( CONCAT (( SELECT `path` FROM common_catalog WHERE id = %d ) , '%%'))";

	public final static String PATH_LIKE_SQLITE_ID = "SELECT id FROM common_catalog WHERE `path` LIKE ( ( SELECT `path` FROM common_catalog WHERE id = %d ) || '%%')";

	/**
	 * IN 查询用,多用于分页统计总数 用于 catelogId 查询的,通常放在 LEFT JOIN 后面还需要,WHERE e.catelog =
	 * c.id。 还需要预留一个 catelogId 的参数 另外也可以用 IN 查询
	 */
	public final static String CATALOG_FIND = "e.catalogId IN ( " + PATH_LIKE_MYSQL_ID + ")";

	/**
	 * 直接查询 catalogId,不进行递归
	 * 
	 * @param catalogId
	 * @return
	 */
	public static Function byCatalogId(int catalogId) {
		return by("catalogId", catalogId);
	}

	/**
	 * 
	 * @param catalogId
	 * @param domainCatalogId
	 * @return
	 */
	public static Function setCatalog(int catalogId, int domainCatalogId) {
		if (catalogId == 0)
			catalogId = domainCatalogId;

		return setCatalog(catalogId);
	}

	public static Function setCatalog(int catalogId) {
		String find;
		switch (JdbcConnection.getDaoContext().getDbType()) {
		case SQLITE:
			find = "e.catalogId IN ( " + PATH_LIKE_SQLITE_ID + ")";
			break;
		case MYSQL:
		default:
			find = CATALOG_FIND;
			break;

		}
		return setWhere(catalogId == 0 ? null : String.format(find, catalogId));
	}

	public static Function setCatalog() {
		Object v = getValue("catalogId", int.class);

		return v == null ? setWhere(null) : setCatalog((int) v);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy