欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 列表数据上下移动或置顶

程序员文章站 2022-07-14 16:46:22
...
/**
	 * 排序
	 * @param id	公告序号
	 * @param type	排序类型 
	 * 				0:上移
	 * 				1:下移
	 * 				2:置顶
	 * @return
	 */
	@RequestMapping("sortThirdNotice")
	@ResponseBody
	public Result sortThirdNotice(Integer id, int type) {
		Result result = new Result();
		try {
			resourseMgtService.sortThirdNotice(id, type);
			result.setStatus(Result.SUCCESS);
			result.setReason("排序成功");
		} catch (Exception e) {
			log.error("error", e);
			result.setStatus(Result.FAILED);
			result.setReason("排序失败");
		}
		return result;
	}
@Override
	public void sortThirdNotice(Integer id, int type) {
		ThirdNoticeInfo notice = (ThirdNoticeInfo) getSession().createCriteria(ThirdNoticeInfo.class).add(Restrictions.ne("flag", -1))
				.add(Restrictions.eq("id", id)).uniqueResult();
		int sort = notice.getSort();
		Criteria c = getSession().createCriteria(ThirdNoticeInfo.class).add(Restrictions.ne("flag", -1));
		if (type == 0) {
			c.add(Restrictions.gt("sort", sort));
			c.addOrder(Order.asc("sort"));
		} else if (type == 1) {
			c.add(Restrictions.lt("sort", sort));
			c.addOrder(Order.desc("sort"));
		} else {
			c.add(Restrictions.gt("sort", sort));
			c.addOrder(Order.desc("sort"));
		}
		c.setMaxResults(1);
		ThirdNoticeInfo sortNotice = (ThirdNoticeInfo) c.uniqueResult();
		if (sortNotice != null) {
			if (type == 2) {
				this.updateThirdNoticeSort(notice.getId(), sortNotice.getSort()+1);
			} else {
				this.updateThirdNoticeSort(notice.getId(), sortNotice.getSort());
				this.updateThirdNoticeSort(sortNotice.getId(), sort);
			}
		}
	}

	/** 
	* @Title: updateThirdNoticeSort 
	* @Description: (公告排序) 
	* @param @param id
	* @param @param sort    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	private void updateThirdNoticeSort(Integer id, int sort) {
		getSession().createQuery("update ThirdNoticeInfo set sort=:s where id=:id ")
		.setParameter("s", sort).setParameter("id", id).executeUpdate();
	}