示例#1
0
文件: vote.go 项目: yonglehou/ohlala
func VoteLink(linkId int64, userId int64, score int, siteRunTime string) *Vote {

	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	var update bool = false
	var vote *Vote = &Vote{0, 0, false, ""}
	checkRows, checkErr := db.Query("SELECT `user_id` FROM `link` WHERE `id` = ? LIMIT 0,1", linkId)
	if checkErr != nil {
		goku.Logger().Errorln(checkErr.Error())
		vote.Errors = "数据库错误"
	} else if checkRows.Next() == false {
		vote.Errors = "参数错误"
	} else {
		var luid int64
		checkRows.Scan(&luid)
		if luid == userId {
			vote.Errors = "不可以对自己投票"
		}
	}
	if vote.Errors != "" {
		return vote
	}

	rows, err := db.Query("SELECT score FROM `link_support_record` WHERE `link_id` = ? AND `user_id` = ? LIMIT 0,1", linkId, userId)
	if err == nil {

		var upVote int64
		var downVote int64
		var createTime time.Time
		if rows.Next() { //投过票的情况
			var scoreTemp int
			rows.Scan(&scoreTemp)

			//已投了支持,再投反对
			if scoreTemp == 1 && score == -1 {

				update = true
				rows, err = db.Query("SELECT vote_up,vote_down,create_time FROM `link` WHERE id=?", linkId)
				rows.Next()
				rows.Scan(&upVote, &downVote, &createTime)
				upVote = upVote - 1
				downVote = downVote + 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_up=?,vote_down=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, downVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}
				_, err = db.Query("UPDATE `link_support_record` SET score=-1,vote_time=NOW() WHERE `link_id` = ? AND `user_id` = ?", linkId, userId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

				//已投了反对,再投支持
			} else if scoreTemp == -1 && score == 1 {

				update = true
				rows, err = db.Query("SELECT vote_up,vote_down,create_time FROM `link` WHERE id=?", linkId)
				rows.Next()
				rows.Scan(&upVote, &downVote, &createTime)
				upVote = upVote + 1
				downVote = downVote - 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_up=?,vote_down=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, downVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}
				_, err = db.Query("UPDATE `link_support_record` SET score=1,vote_time=NOW() WHERE `link_id` = ? AND `user_id` = ?", linkId, userId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

			} else {
				vote.Errors = "您已对此投票"
			}

		} else { //没投过票的情况

			update = true
			rows, err = db.Query("SELECT vote_up,vote_down,create_time FROM `link` WHERE id=?", linkId)
			rows.Next()
			rows.Scan(&upVote, &downVote, &createTime)
			if score == 1 {

				upVote = upVote + 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_up=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

			} else {

				downVote = downVote + 1
				score := utils.LinkSortAlgorithm(createTime, upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				_, err = db.Query("UPDATE `link` SET vote_down=?,reddit_score=?,dispute_score=? WHERE `id`=?;", downVote, score, dispute_score, linkId)
				if err != nil {
					goku.Logger().Errorln(err)
				}

			}
			_, err = db.Query("INSERT INTO `link_support_record` (link_id, user_id, score,vote_time) VALUES (?, ?, ?, NOW())", linkId, userId, score)
			if err != nil {
				goku.Logger().Errorln(err)
			}
		}

		if update {
			vote.Id = linkId
			vote.VoteNum = upVote - downVote
			vote.Success = true
			// 存入`tui_link_for_handle` 链接处理队列表
			_, err = db.Query("INSERT ignore INTO tui_link_for_handle(link_id,create_time,user_id,insert_time,data_type) VALUES (?, ?, ?, NOW(), ?)",
				linkId, createTime, userId, 2)
			if err != nil {
				goku.Logger().Errorln(err)
			}
		}
	}
	if err != nil {
		vote.Success = false
		goku.Logger().Errorln(err.Error())
		vote.Errors = "数据库错误"
	}

	return vote
}
示例#2
0
文件: vote.go 项目: yonglehou/ohlala
func VoteComment(commentId int64, userId int64, score int, siteRunTime string) *Vote {

	var db *goku.MysqlDB = GetDB()
	defer db.Close()

	var vote *Vote = &Vote{0, 0, false, ""}
	var updateChildrenScore bool = false

	checkRows, checkErr := db.Query("SELECT `user_id` FROM `comment` WHERE `id` = ? LIMIT 0,1", commentId) //reddit_score
	if checkErr != nil {
		goku.Logger().Errorln(checkErr.Error())
		vote.Errors = "数据库错误"
	} else if checkRows.Next() == false {
		vote.Errors = "参数错误"
	} else {
		var luid int64
		checkRows.Scan(&luid)
		if luid == userId {
			vote.Errors = "不可以对自己投票"
		}
	}
	if vote.Errors != "" {
		return vote
	}

	rows, err := db.Query("SELECT score FROM `comment_support_record` WHERE `comment_id` = ? AND `user_id` = ? LIMIT 0,1", commentId, userId)
	if err == nil {
		var upVote, downVote int64
		if rows.Next() { //投过票的情况
			var scoreTemp int
			rows.Scan(&scoreTemp)
			//已投了支持,再投反对
			if scoreTemp == 1 && score == -1 {

				updateChildrenScore = true
				rows, err = db.Query("SELECT vote_up,vote_down FROM `comment` WHERE id=?", commentId)
				rows.Next()
				rows.Scan(&upVote, &downVote)
				upVote -= 1
				downVote += 1
				score := utils.CommentSortAlgorithm(upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				db.Query("UPDATE `comment` SET vote_up=?,vote_down=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, downVote, score, dispute_score, commentId)
				db.Query("UPDATE `comment_support_record` SET score=-1,vote_time=NOW() WHERE `comment_id` = ? AND `user_id` = ?", commentId, userId)

			} else if scoreTemp == -1 && score == 1 { //已投了反对,再投支持

				updateChildrenScore = true
				rows, err = db.Query("SELECT vote_up,vote_down FROM `comment` WHERE id=?", commentId)
				rows.Next()
				rows.Scan(&upVote, &downVote)
				upVote += 1
				downVote -= 1
				score := utils.CommentSortAlgorithm(upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				db.Query("UPDATE `comment` SET vote_down=?,vote_up=?,reddit_score=?,dispute_score=? WHERE `id`=?;", downVote, upVote, score, dispute_score, commentId)
				db.Query("UPDATE `comment_support_record` SET score=1,vote_time=NOW() WHERE `comment_id` = ? AND `user_id` = ?", commentId, userId)

			} else {
				vote.Errors = "您已对此投票"
			}

		} else { //没投过票的情况

			updateChildrenScore = true
			rows, err = db.Query("SELECT vote_up,vote_down FROM `comment` WHERE id=?", commentId)
			rows.Next()
			rows.Scan(&upVote, &downVote)
			if score == 1 {
				upVote += 1
				score := utils.CommentSortAlgorithm(upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				db.Query("UPDATE `comment` SET vote_up=?,reddit_score=?,dispute_score=? WHERE id=?;", upVote, score, dispute_score, commentId)

			} else {
				downVote += 1
				score := utils.CommentSortAlgorithm(upVote, downVote)
				dispute_score := utils.DisputeLinkSortAlgorithm(upVote, downVote)
				db.Query("UPDATE `comment` SET vote_down=?,reddit_score=?,dispute_score=? WHERE `id`=?;", downVote, score, dispute_score, commentId)

			}
			db.Query("INSERT INTO `comment_support_record` (comment_id, user_id, score, vote_time) VALUES (?, ?, ?,NOW())", commentId, userId, score)

		}

		if updateChildrenScore {

			//rows, err = db.Query("SELECT vote_up-vote_down AS vote FROM `comment` WHERE `id` = ? LIMIT 0,1", commentId) //, reddit_score,link_id
			//if err == nil && rows.Next() {
			//var voteNum int64 = 0
			vote.Id = commentId
			//rows.Scan(&voteNum) //, &childScore, &linkId
			vote.VoteNum = upVote - downVote
			vote.Success = true
			//}
		}
	}
	if err != nil {
		goku.Logger().Errorln(err.Error())
		vote.Success = false
		vote.Errors = "数据库错误"
	}

	return vote

}