public enum ErrorCode {
	
	EXCEPTION("程序异常", "00001"),
	USER_NOT_EXIST("用户未注册", "00002"),
    VERIFY_CODE_WRONG("验证码错误","00003"),
    OLD_PWD_WRONG("旧密码错误","00004"),
    USERNAME_PASSWORD_WRONG("用户名或密码错误","00005"),
    TODAY_HAVE_SIGN("今日已签到","00006");

	private String errorMsg;
	private String errorCode;

	private ErrorCode(final String errorMsg, final String errorCode) {
		this.errorMsg = errorMsg;
		this.errorCode = errorCode;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

} public class ExecuteResult<T> {
	
	private final long timeOut = 50000L;
	private boolean isSuccess;
	private T data;

	private String errorCode;
	private String errorMsg;
	private String fromUrl;

	private Long processTime;
	@SuppressWarnings("unused")
	private Long flushTimeOut;

	public T getData() {
		return data;
	}

	public String getErrorCode() {
		return errorCode == null ? "" : errorCode;
	}

	public String getErrorMsg() {
		return errorMsg == null ? "" : errorMsg;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setData(T data) {
		this.data = data;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	public Date getFlushTime() {
		return new Date();
		// return flushTime;
	}

	public Long getProcessTime() {
		return processTime;
	}

	public void setProcessTime(Long processTime) {
		this.processTime = processTime;
	}

	public String getFromUrl() {
		return fromUrl;
	}

	public void setFromUrl(String fromUrl) {
		this.fromUrl = fromUrl;
	}

	public Long getFlushTimeOut() {
		return timeOut;
		// return flushTimeOut;
	}

	public void setFlushTimeOut(Long flushTimeOut) {
		this.flushTimeOut = flushTimeOut;
	}

} public class PageableResultJson implements Serializable {

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */
	private static final long serialVersionUID = 5544244021994468020L;

	public static final int PAGE_SIZE = 7;

	private long totalPageNumber;
	private int pageSize;
	private Object data;

	public Object getData() {
		return data;
	}

	public int getPageSize() {
		return pageSize;
	}

	public long getTotalPageNumber() {
		return totalPageNumber;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public void setTotalPageNumber(long totalPageNumber) {
		this.totalPageNumber = totalPageNumber;
	}

	@Override
	public String toString() {
		return "DateTableJson [totalPageNumber=" + totalPageNumber + ", pageSize=" + pageSize + ", data=" + data + "]";
	}
	

} @SqlResultSetMappings({

		@SqlResultSetMapping(name = "getBlogByUserIdList", classes = @ConstructorResult(targetClass = Blog.class, columns = {
				@ColumnResult(name = "id", type = Long.class),
				@ColumnResult(name = "typeId", type = Long.class),
				@ColumnResult(name = "title", type = String.class),
				@ColumnResult(name = "subtitle", type = String.class),
				@ColumnResult(name = "bannerImg", type = String.class),
				@ColumnResult(name = "status", type = Integer.class),
				@ColumnResult(name = "collectCount", type = Integer.class),
				@ColumnResult(name = "browseCount", type = Integer.class),
				@ColumnResult(name = "createTime", type = Date.class),
				@ColumnResult(name = "updateTime", type = Date.class),
				@ColumnResult(name = "commentCnt", type = Integer.class) })),

		@SqlResultSetMapping(name = "getBlogById", classes = @ConstructorResult(targetClass = Blog.class, columns = {
				@ColumnResult(name = "id", type = Long.class), @ColumnResult(name = "typeId", type = Long.class),
				@ColumnResult(name = "title", type = String.class),
				@ColumnResult(name = "subtitle", type = String.class),
				@ColumnResult(name = "content", type = String.class),
				@ColumnResult(name = "bannerImg", type = String.class),
				@ColumnResult(name = "collectCount", type = Integer.class),
				@ColumnResult(name = "browseCount", type = Integer.class),
				@ColumnResult(name = "createTime", type = Date.class),
				@ColumnResult(name = "updateTime", type = Date.class),
				@ColumnResult(name = "typeName", type = String.class),
				@ColumnResult(name = "userName", type = String.class),
				@ColumnResult(name = "headImgUrl", type = String.class),
				@ColumnResult(name = "userId", type = Long.class) })),

		@SqlResultSetMapping(name = "getAllBlogList", classes = @ConstructorResult(targetClass = Blog.class, columns = {
				@ColumnResult(name = "id", type = Long.class),
				@ColumnResult(name = "typeId", type = Long.class),
				@ColumnResult(name = "systemTypeId", type = Long.class),
				@ColumnResult(name = "typeName", type = String.class),
				@ColumnResult(name = "systemTypeName", type = String.class),
				@ColumnResult(name = "title", type = String.class),
				@ColumnResult(name = "subtitle", type = String.class),
				@ColumnResult(name = "content", type = String.class),
				@ColumnResult(name = "bannerImg", type = String.class),
				@ColumnResult(name = "collectCount", type = Integer.class),
				@ColumnResult(name = "browseCount", type = Integer.class),
				@ColumnResult(name = "createTime", type = Date.class),
				@ColumnResult(name = "updateTime", type = Date.class),
				@ColumnResult(name = "commentCnt", type = Integer.class),
				@ColumnResult(name = "userName", type = String.class),
				@ColumnResult(name = "headImgUrl", type = String.class),
				@ColumnResult(name = "userId", type = Long.class) })),
		
		@SqlResultSetMapping(name = "getHotBlogUserList", classes = @ConstructorResult(targetClass = Blog.class, columns = {
				@ColumnResult(name = "userName", type = String.class),
				@ColumnResult(name = "headImgUrl", type = String.class),
				@ColumnResult(name = "userId", type = Long.class),
				@ColumnResult(name = "commentCnt", type = Integer.class) }))

})

@Entity
@Table(name = "BLOG")
public class Blog implements Serializable {

	private static final long serialVersionUID = 562199224417013272L;

	/**
	 * 默认开启
	 */
	public static final Integer BLOG_STATE_DEFAULT = 1;
	/**
	 * 关闭
	 */
	public static final Integer BLOG_STATE_CLOSE = 0;

	/**
	 * 草稿
	 */
	public static final Integer BLOG_STATE_DRAFT = 2;

	/**
	 * 发布
	 */
	public static final Integer BLOG_STATE_RELEASE = 3;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private Long id;
	@Column(name = "USER_ID")
	private Long userId;
	@Column(name = "SYSTEM_TYPE_ID")
	private Long systemTypeId;
	@Column(name = "TYPE_ID")
	private Long typeId;
	@Column(name = "TITLE")
	private String title;
	@Column(name = "CONTENT")
	private String content;
	@Column(name = "SUBTITLE")
	private String subtitle;
	@Column(name = "BANNER_IMG")
	private String bannerImg;
	@Column(name = "STATUS")
	private Integer status;
	@Column(name = "COLLECT_COUNT")
	private Integer collectCount;
	@Column(name = "BROWSE_COUNT")
	private Integer browseCount;
	@JsonSerialize(using = CustomDateSerializer.class)
	@Column(name = "CREATE_TIME")
	private Date createTime;
	@JsonSerialize(using = CustomDateSerializer.class)
	@Column(name = "UPDATE_TIME")
	private Date updateTime;

	@Transient
	private String typeName;
	@Transient
	private String systemTypeName;
//	@Transient
//	private Long userId;
	@Transient
	private String userName;
	@Transient
	private String headImgUrl;
	@Transient
	private Integer commentCnt;

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getHeadImgUrl() {
		return headImgUrl;
	}

	public void setHeadImgUrl(String headImgUrl) {
		this.headImgUrl = headImgUrl;
	}

	public Integer getCommentCnt() {
		return commentCnt;
	}

	public void setCommentCnt(Integer commentCnt) {
		this.commentCnt = commentCnt;
	}

	public Long getSystemTypeId() {
		return systemTypeId;
	}

	public void setSystemTypeId(Long systemTypeId) {
		this.systemTypeId = systemTypeId;
	}

	public String getSubtitle() {
		return subtitle;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public String getBannerImg() {
		return bannerImg;
	}

	public void setBannerImg(String bannerImg) {
		this.bannerImg = bannerImg;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getCollectCount() {
		return collectCount;
	}

	public void setCollectCount(Integer collectCount) {
		this.collectCount = collectCount;
	}

	public Integer getBrowseCount() {
		return browseCount;
	}

	public void setBrowseCount(Integer browseCount) {
		this.browseCount = browseCount;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getSystemTypeName() {
		return systemTypeName;
	}

	public void setSystemTypeName(String systemTypeName) {
		this.systemTypeName = systemTypeName;
	}

	public Blog() {
		super();
	}

	public Blog(Long id, Long typeId, String title, String content, String subtitle, String bannerImg, Integer status,
			Integer collectCount, Integer browseCount, Date createTime, Date updateTime) {
		super();
		this.id = id;
		this.typeId = typeId;
		this.title = title;
		this.content = content;
		this.subtitle = subtitle;
		this.bannerImg = bannerImg;
		this.status = status;
		this.collectCount = collectCount;
		this.browseCount = browseCount;
		this.createTime = createTime;
		this.updateTime = updateTime;
	}

	public Blog(Long id, Long typeId, String title, String subtitle, String bannerImg, Integer status,
			Integer collectCount, Integer browseCount, Date createTime, Date updateTime, Integer commentCnt) {
		super();
		this.id = id;
		this.typeId = typeId;
		this.title = title;
		this.subtitle = subtitle;
		this.bannerImg = bannerImg;
		this.status = status;
		this.collectCount = collectCount;
		this.browseCount = browseCount;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.commentCnt = commentCnt;
	}

	public Blog(Long id, Long typeId,Long systemTypeId, String typeName, String systemTypeName, String title, String subtitle,
			String content, String bannerImg, Integer collectCount, Integer browseCount, Date createTime,
			Date updateTime, Integer commentCnt, String userName, String headImgUrl, Long userId) {
		super();
		this.id = id;
		this.typeId = typeId;
		this.systemTypeId = systemTypeId;
		this.typeName = typeName;
		this.systemTypeName = systemTypeName;
		this.title = title;
		this.subtitle = subtitle;
		this.content = content;
		this.bannerImg = bannerImg;
		this.collectCount = collectCount;
		this.browseCount = browseCount;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.commentCnt = commentCnt;
		this.userName = userName;
		this.headImgUrl = headImgUrl;
		this.userId = userId;
	}

	public Blog(Long id, Long typeId, String title, String subtitle, String content, String bannerImg,
			Integer collectCount, Integer browseCount, Date createTime, Date updateTime, String typeName,
			String userName, String headImgUrl, Long userId) {
		super();
		this.id = id;
		this.typeId = typeId;
		this.title = title;
		this.subtitle = subtitle;
		this.content = content;
		this.bannerImg = bannerImg;
		this.collectCount = collectCount;
		this.browseCount = browseCount;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.typeName = typeName;
		this.userName = userName;
		this.headImgUrl = headImgUrl;
		this.userId = userId;
	}

	public Blog(String userName, String headImgUrl, Long userId, Integer commentCnt) {
		super();
		this.userName = userName;
		this.headImgUrl = headImgUrl;
		this.userId = userId;
		this.commentCnt = commentCnt;
	}

	@Override
	public String toString() {
		return "Blog [id=" + id + ", typeId=" + typeId + ", title=" + title + ", content=" + content + ", subtitle="
				+ subtitle + ", bannerImg=" + bannerImg + ", status=" + status + ", collectCount=" + collectCount
				+ ", browseCount=" + browseCount + ", createTime=" + createTime + ", updateTime=" + updateTime + "]";
	}

} @Entity
@Table(name = "BLOG_TYPE")
public class BlogType implements Serializable {

	/**
	 * 默认开启
	 */
	public static final Integer LABEL_STATE_DEFAULT = 1;
	/**
	 * 关闭
	 */
	public static final Integer LABEL_STATE_CLOSE = 0;

	private static final long serialVersionUID = 2159291991132749505L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private Long id;
	@Column(name = "NAME")
	private String name;
	@Column(name = "STATUS")
	private Integer status;
	@Column(name = "USER_ID")
	private Long userId;
	@JsonSerialize(using = CustomDateSerializer.class)
	@Column(name = "CREATE_TIME")
	private Date createTime;
	@JsonSerialize(using = CustomDateSerializer.class)
	@Column(name = "UPDATE_TIME")
	private Date updateTime;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public BlogType(Long id, String name, Integer status, Long userId, Date createTime,
			Date updateTime) {
		super();
		this.id = id;
		this.name = name;
		this.status = status;
		this.userId = userId;
		this.createTime = createTime;
		this.updateTime = updateTime;
	}

	public BlogType() {
		super();
	}

	@Override
	public String toString() {
		return "BlogType [id=" + id + ", name=" + name  + ", status="
				+ status + ", userId=" + userId + ", createTime=" + createTime + ", updateTime=" + updateTime + "]";
	}

} @Entity
@Table(name = "LABEL")
public class Label implements Serializable {

	/**
	 * 标签
	 */
	public static final Integer LABLE = 1;
	/**
	 * 开源
	 */
	public static final Integer OPEN_SOURCE = 2;
	/**
	 * 问与答
	 */
	public static final Integer Q_A = 3;

	/**
	 * 默认开启
	 */
	public static final Integer LABEL_STATE_DEFAULT = 1;
	/**
	 * 关闭
	 */
	public static final Integer LABEL_STATE_CLOSE = 0;

	private static final long serialVersionUID = 2159291991132749505L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private Long id;
	@Column(name = "LABLE_NAME")
	private String lableName;
	@Column(name = "PARENT_ID")
	private Long parentId;
	@Column(name = "SORT")
	private Integer sort;
	@Column(name = "TYPE")
	private Integer type;
	@Column(name = "STATUS")
	private Integer status;
	@JsonSerialize(using = CustomDateSerializer.class)
	@Column(name = "CREATE_TIME")
	private Date createTime;
	@JsonSerialize(using = CustomDateSerializer.class)
	@Column(name = "UPDATE_TIME")
	private Date updateTime;

	@Transient
	private List<Label> childs;
	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public List<Label> getChilds() {
		return childs;
	}

	public void setChilds(List<Label> childs) {
		this.childs = childs;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLableName() {
		return lableName;
	}

	public void setLableName(String lableName) {
		this.lableName = lableName;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Label() {
		super();
	}

	public Label(Long id, String lableName, Long parentId, Integer sort, Integer type, Date createTime,
			Date updateTime) {
		super();
		this.id = id;
		this.lableName = lableName;
		this.parentId = parentId;
		this.sort = sort;
		this.type = type;
		this.createTime = createTime;
		this.updateTime = updateTime;
	}

	@Override
	public String toString() {
		return "Label [id=" + id + ", lableName=" + lableName + ", parentId=" + parentId + ", sort=" + sort + ", type="
				+ type + ", createTime=" + createTime + ", updateTime=" + updateTime + "]";
	}

} public interface BlogDao extends JpaRepository<Blog, Long>{

	@Query(nativeQuery = true, value = "SELECT COUNT(1) FROM BLOG WHERE TYPE_ID =:typeId")
	int getBlogByUserIdCnt(@Param("typeId") Long typeId);

	@Query(nativeQuery = true, value = "SELECT COUNT(1) FROM BLOG")
	int getBoleCnt();
	
	@Query(nativeQuery = true, value = "SELECT COUNT(1) FROM BLOG WHERE SYSTEM_TYPE_ID=:systemTypeId")
	int getBoleBySystemTypeIdCnt(@Param("systemTypeId") Long systemTypeId);

} public interface BlogTypeDao extends JpaRepository<BlogType, Long>{

	@Query(nativeQuery = true, value = "SELECT * FROM BLOG_TYPE WHERE STATUS=:status AND USER_ID=:userId ORDER BY CREATE_TIME ASC")
	public List<BlogType> findUserBlogTypeByStatusAndUserId(@Param("status")Integer status, @Param("userId")Long userId);

	public List<BlogType> findBlogTypeByUserIdAndName(Long userId, String name);

} @Service
public class BlogService {

	@Autowired
	BlogDao blogDao;
	@Autowired
	EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public Page<Blog> findBlogByUserIdAndLabelIdList(Long userId, Long typeId, Integer page, int rows) {
		int firstRecord = PageUtil.calcPage(page) * rows;
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT o.ID id,o.TYPE_ID typeId,o.TITLE title,o.SUBTITLE subtitle,o.BANNER_IMG bannerImg,o.`STATUS` status,o.COLLECT_COUNT collectCount,o.BROWSE_COUNT browseCount,o.CREATE_TIME createTime,")
				.append("o.UPDATE_TIME updateTime,(SELECT COUNT(1) FROM COMMENT WHERE THEME_ID = o.ID AND TYPE=3) as commentCnt")
				.append(" FROM blog AS o LEFT JOIN BLOG_TYPE AS l ON o.TYPE_ID = l.ID")
				.append(" WHERE TYPE_ID=:typeId AND l.USER_ID=:userId ORDER BY o.CREATE_TIME DESC");
		List<Blog> list = entityManager.createNativeQuery(sql.toString(), "getBlogByUserIdList")
				.setParameter("typeId", typeId).setParameter("userId", userId).setFirstResult(firstRecord)
				.setMaxResults(rows).getResultList();
		int total = blogDao.getBlogByUserIdCnt(typeId);
		Pageable pageable = new PageRequest(page, rows);
		Page<Blog> pages = new PageImpl<>(list, pageable, total);
		return pages;
	}

	public void saveBlog(Blog blog) {
		if (blog.getId() != null) {
			// TODO 用户修改
		} else {
			blog.setBrowseCount(0);
			blog.setCollectCount(0);
			blog.setCreateTime(new Date());
			blogDao.save(blog);
		}
	}

	@SuppressWarnings("unchecked")
	public Blog getBlogById(Long id) {
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT b.ID id,b.TYPE_ID typeId,b.TITLE title,B.SUBTITLE subTitle,b.CONTENT content,b.COLLECT_COUNT collectCount,b.BROWSE_COUNT browseCount,b.BANNER_IMG bannerImg,b.CREATE_TIME createTime,")
				.append("b.UPDATE_TIME updateTime,t.`NAME` typeName,u.USER_NAME userName,u.HEAD_IMG_URL headImgUrl,u.ID userId")
				.append(" FROM blog AS b LEFT JOIN blog_type AS t ON t.ID = b.TYPE_ID")
				.append(" LEFT JOIN user AS u ON u.ID = t.USER_ID WHERE b.ID=:id");
		final List<Blog> list = entityManager.createNativeQuery(sql.toString(), "getBlogById").setParameter("id", id)
				.getResultList();
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public Page<Blog> getBlogList(Integer page, Integer rows, Long systemTypeId) {

		int firstRecord = PageUtil.calcPage(page) * rows;
		int total;
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT o.ID id,l.ID typeId,t.ID systemTypeId,l.`NAME` typeName,t.`NAME` systemTypeName,o.TITLE title,o.SUBTITLE subtitle,o.CONTENT content,o.BANNER_IMG bannerImg,o.`STATUS` status,o.COLLECT_COUNT collectCount,o.BROWSE_COUNT browseCount,o.CREATE_TIME createTime,")
				.append("o.UPDATE_TIME updateTime,(SELECT COUNT(1) FROM COMMENT WHERE THEME_ID = o.ID AND TYPE=3) as commentCnt,u.USER_NAME userName,u.HEAD_IMG_URL headImgUrl,l.USER_ID userId")
				.append(" FROM blog AS o LEFT JOIN BLOG_TYPE AS l ON o.TYPE_ID = l.ID LEFT JOIN BLOG_TYPE AS t ON o.SYSTEM_TYPE_ID = t.ID LEFT JOIN user AS u ON u.ID = l.USER_ID");

		if (systemTypeId != 0) {
			sql.append(" WHERE o.SYSTEM_TYPE_ID=" + systemTypeId);
			total = blogDao.getBoleBySystemTypeIdCnt(systemTypeId);
		} else {
			total = blogDao.getBoleCnt();
		}
		sql.append(" ORDER BY o.CREATE_TIME DESC");

		List<Blog> list = entityManager.createNativeQuery(sql.toString(), "getAllBlogList").setFirstResult(firstRecord)
				.setMaxResults(rows).getResultList();
		Pageable pageable = new PageRequest(page, rows);
		Page<Blog> pages = new PageImpl<>(list, pageable, total);
		return pages;
	}

	@SuppressWarnings("unchecked")
	public Page<Blog> getHotBlogList(Integer page, Integer rows) {
		int firstRecord = PageUtil.calcPage(page) * rows;
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT o.ID id,l.ID typeId,t.ID systemTypeId,l.`NAME` typeName,t.`NAME` systemTypeName,o.TITLE title,o.SUBTITLE subtitle,o.CONTENT content,o.BANNER_IMG bannerImg,o.`STATUS` status,o.COLLECT_COUNT collectCount,o.BROWSE_COUNT browseCount,o.CREATE_TIME createTime,")
				.append("o.UPDATE_TIME updateTime,(SELECT COUNT(1) FROM COMMENT WHERE THEME_ID = o.ID AND TYPE=3) as commentCnt,u.USER_NAME userName,u.HEAD_IMG_URL headImgUrl,l.USER_ID userId")
				.append(" FROM blog AS o LEFT JOIN BLOG_TYPE AS l ON o.TYPE_ID = l.ID LEFT JOIN BLOG_TYPE AS t ON o.SYSTEM_TYPE_ID = t.ID LEFT JOIN user AS u ON u.ID = l.USER_ID ORDER BY o.BROWSE_COUNT DESC");
		List<Blog> list = entityManager.createNativeQuery(sql.toString(), "getAllBlogList").setFirstResult(firstRecord)
				.setMaxResults(rows).getResultList();
		int total = blogDao.getBoleCnt();
		Pageable pageable = new PageRequest(page, rows);
		Page<Blog> pages = new PageImpl<>(list, pageable, total);
		return pages;
	}

	@SuppressWarnings("unchecked")
	public Page<Blog> getHotBlogUserList(Integer page, Integer rows) {
		int firstRecord = PageUtil.calcPage(page) * rows;
		StringBuilder sql = new StringBuilder();
		sql.append(
				"SELECT u.USER_NAME userName,u.HEAD_IMG_URL headImgUrl,l.USER_ID userId,(SELECT COUNT(1) FROM COMMENT WHERE THEME_ID = o.ID AND TYPE=3) as commentCnt")
				.append(" FROM blog AS o LEFT JOIN BLOG_TYPE AS l ON o.TYPE_ID = l.ID LEFT JOIN BLOG_TYPE AS t ON o.SYSTEM_TYPE_ID = t.ID")
				.append(" LEFT JOIN user AS u ON u.ID = l.USER_ID GROUP BY l.USER_ID ORDER BY commentCnt DESC,o.BROWSE_COUNT DESC,o.COLLECT_COUNT DESC");
		List<Blog> list = entityManager.createNativeQuery(sql.toString(), "getHotBlogUserList")
				.setFirstResult(firstRecord).setMaxResults(rows).getResultList();
		int total = blogDao.getBoleCnt();
		Pageable pageable = new PageRequest(page, rows);
		Page<Blog> pages = new PageImpl<>(list, pageable, total);
		return pages;
	}

} @Service
public class BlogTypeService {

	@Autowired
	BlogTypeDao blogTypeDao;

	public Integer save(BlogType blogType) {
		if (blogType.getId() != null) {
			//TODO 修改
		} else {
			List<BlogType> list = blogTypeDao.findBlogTypeByUserIdAndName(blogType.getUserId(),blogType.getName());
			if (list.isEmpty()) {
				blogType.setCreateTime(new Date());
				blogTypeDao.save(blogType);
				return 1;
			} else {
				return -1;// 相同类型相同名称已存在
			}
		}
		return 0;
	}

	
}
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.geekhome.common.vo.ErrorCode;
import com.geekhome.common.vo.ExecuteResult;
import com.geekhome.common.vo.PageableResultJson;
import com.geekhome.entity.Blog;
import com.geekhome.entity.BlogType;
import com.geekhome.entity.Label;
import com.geekhome.entity.dao.BlogDao;
import com.geekhome.entity.dao.BlogTypeDao;
import com.geekhome.entity.service.BlogService;
import com.geekhome.entity.service.BlogTypeService;
package com.geekhome.controller;




@RestController
@RequestMapping("blog")
public class BlogController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	BlogService blogService;
	@Autowired
	BlogDao blogDao;
	@Autowired
	BlogTypeDao blogTypeDao;
	@Autowired
	BlogTypeService blogTypeService;

	@RequestMapping(value = "getBloglabelList")
	@CrossOrigin
	public ExecuteResult<List<BlogType>> getBloglabelList(Long userId) {
		final ExecuteResult<List<BlogType>> result = new ExecuteResult<>();
		try {
			List<BlogType> types = blogTypeDao.findUserBlogTypeByStatusAndUserId(Label.LABEL_STATE_DEFAULT, userId);
			result.setData(types);
			result.setSuccess(true);
		} catch (final Exception e) {
			logger.error("", e);
			result.setSuccess(false);
			result.setErrorCode(ErrorCode.EXCEPTION.getErrorCode());