给分类加个排序

This commit is contained in:
luchenqun 2017-03-06 12:43:29 +08:00
parent f88077b7ac
commit 23b4ed211a
4 changed files with 105 additions and 57 deletions

View File

@ -52,15 +52,15 @@ Date.prototype.format = function(fmt) { //author: meizz
// update delete 返回影响的行数 // update delete 返回影响的行数
var db = { var db = {
} }
// var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun1'"; // var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun1'";
// client.query(sql, (err, result) => { // client.query(sql, (err, result) => {
// if (err) { // if (err) {
// console.log(err); // console.log(err);
// } else { // } else {
// console.log(result); // console.log(result);
// } // }
// }); // });
db.addBookmark = function(user_id, bookmark) { db.addBookmark = function(user_id, bookmark) {
var insertSql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + bookmark.title + "', " + client.escape(bookmark.description) + ", '" + bookmark.url + "', '" + bookmark.public + "', '1')"; var insertSql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + bookmark.title + "', " + client.escape(bookmark.description) + ", '" + bookmark.url + "', '" + bookmark.public + "', '1')";
@ -499,13 +499,13 @@ db.getBookmarksNavigate = function(tags) {
// var sql = "SELECT t.id as tag_id, t.name as tag_name, b.* FROM `tags` as t LEFT OUTER JOIN tags_bookmarks as tb ON t.id = tb.tag_id LEFT OUTER JOIN bookmarks as b ON tb.bookmark_id = b.id WHERE t.user_id='" + user_id + "' ORDER BY t.id ASC, b.click_count DESC"; // var sql = "SELECT t.id as tag_id, t.name as tag_name, b.* FROM `tags` as t LEFT OUTER JOIN tags_bookmarks as tb ON t.id = tb.tag_id LEFT OUTER JOIN bookmarks as b ON tb.bookmark_id = b.id WHERE t.user_id='" + user_id + "' ORDER BY t.id ASC, b.click_count DESC";
var sql = ""; var sql = "";
tags.forEach((tag, index) => { tags.forEach((tag, index) => {
var t = 't' + tag.id; var t = 't' + tag.id;
if (index >= 1) { if (index >= 1) {
sql += " UNION " sql += " UNION "
} }
sql += "(SELECT * FROM ((SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.click_count DESC LIMIT 0, 16) UNION (SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.created_at DESC LIMIT 0, 16)) as " + t + " ORDER BY " + t + ".click_count DESC, " + t + ".created_at DESC)"; sql += "(SELECT * FROM ((SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.click_count DESC LIMIT 0, 16) UNION (SELECT t.id AS tag_id, t.`name` as tag_name, t.sort, b.* FROM `tags` as t, `bookmarks`as b, `tags_bookmarks` as tb WHERE t.id = tb.tag_id AND b.id = tb.bookmark_id AND t.id = " + tag.id + " ORDER BY b.created_at DESC LIMIT 0, 16)) as " + t + " ORDER BY " + t + ".click_count DESC, " + t + ".created_at DESC)";
}) })
// console.log('getBookmarksNavigate ', sql); // console.log('getBookmarksNavigate ', sql);
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => { client.query(sql, (err, result) => {
@ -567,16 +567,38 @@ db.getBookmarksByTag = function(params) {
params.currentPage = params.currentPage || 1; params.currentPage = params.currentPage || 1;
params.perPageItems = params.perPageItems || 20; params.perPageItems = params.perPageItems || 20;
var sql = "SELECT bookmarks.id, bookmarks.user_id, bookmarks.title, bookmarks.description, bookmarks.url, bookmarks.public, bookmarks.click_count, DATE_FORMAT(bookmarks.created_at, '%Y-%m-%d') as created_at, DATE_FORMAT(bookmarks.last_click, '%Y-%m-%d') as last_click FROM `tags_bookmarks`, `bookmarks` WHERE tags_bookmarks.tag_id = '" + tag_id + "' AND tags_bookmarks.bookmark_id = bookmarks.id ORDER BY bookmarks.click_count DESC, bookmarks.created_at DESC"; var sql = "SELECT bookmarks.id, bookmarks.user_id, bookmarks.title, bookmarks.description, bookmarks.url, bookmarks.public, bookmarks.click_count, DATE_FORMAT(bookmarks.created_at, '%Y-%m-%d %H:%i:%s') as created_at, DATE_FORMAT(bookmarks.last_click, '%Y-%m-%d %H:%i:%s') as last_click FROM `tags_bookmarks`, `bookmarks` WHERE tags_bookmarks.tag_id = '" + tag_id + "' AND tags_bookmarks.bookmark_id = bookmarks.id";
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => { client.query(sql, (err, result) => {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
var bookmarksClickCount, bookmarksCreatedAt, bookmarksLatestClick;
result.sort((a, b) => {
var click1 = parseInt(a.click_count);
var click2 = parseInt(b.click_count);
if (click1 > click2) {
return -1;
} else if (click1 == click2) {
return a.created_at >= b.created_at ? -1 : 1;
} else {
return 1;
}
})
bookmarksClickCount = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems);
result.sort((a, b) => a.created_at >= b.created_at ? -1 : 1);
bookmarksCreatedAt = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems);
result.sort((a, b) => a.last_click >= b.last_click ? -1 : 1);
bookmarksLatestClick = result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems);
var bookmarksData = { var bookmarksData = {
totalItems: result.length, totalItems: result.length,
bookmarks: result.slice((params.currentPage - 1) * params.perPageItems, params.currentPage * params.perPageItems), bookmarksClickCount: bookmarksClickCount,
bookmarksCreatedAt: bookmarksCreatedAt,
bookmarksLatestClick: bookmarksLatestClick,
} }
resolve(bookmarksData); resolve(bookmarksData);
} }

View File

@ -4,6 +4,8 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
const perPageItems = 20; const perPageItems = 20;
var dialog = null; var dialog = null;
$scope.order = [false, false, false];
$scope.order[($stateParams && $stateParams.orderIndex) || 0] = true;
$scope.loadBookmarks = false; $scope.loadBookmarks = false;
$scope.loadTags = false; $scope.loadTags = false;
$scope.tags = []; // 书签数据 $scope.tags = []; // 书签数据
@ -19,12 +21,25 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
$scope.newTag = ''; $scope.newTag = '';
$scope.waitDelTag = {}; $scope.waitDelTag = {};
$scope.waitDelBookmark = {}; $scope.waitDelBookmark = {};
$scope.bookmarkData = {};
pubSubService.subscribe('MenuCtr.tags', $scope, function(event, data) { pubSubService.subscribe('MenuCtr.tags', $scope, function(event, data) {
console.log('subscribe MenuCtr.tags', data); console.log('subscribe MenuCtr.tags', data);
getTags({}); getTags({});
}); });
$scope.changeOrder = function(index) {
$scope.order = $scope.order.map(() => false);
$scope.order[index] = true;
if ($scope.order[0]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksClickCount;
} else if ($scope.order[1]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksCreatedAt;
} else {
$scope.bookmarks = $scope.bookmarkData.bookmarksLatestClick;
}
}
$scope.getBookmarks = function(tagId, currentPage) { $scope.getBookmarks = function(tagId, currentPage) {
$scope.bookmarkClicked = true; $scope.bookmarkClicked = true;
$scope.currentTagId = tagId; $scope.currentTagId = tagId;
@ -44,10 +59,18 @@ app.controller('tagsCtr', ['$scope', '$filter', '$window', '$stateParams', '$tim
perPageItems: perPageItems, perPageItems: perPageItems,
}; };
$('.js-tags-table').transition('hide'); $('.js-tags-table').transition('hide');
bookmarkService.getBookmarksByTag(params) bookmarkService.getBookmarksByTag(params)
.then((data) => { .then((data) => {
$scope.bookmarks = data.bookmarks; $scope.bookmarkData = data;
$scope.bookmarkCount = data.totalItems; if ($scope.order[0]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksClickCount;
} else if ($scope.order[1]) {
$scope.bookmarks = $scope.bookmarkData.bookmarksCreatedAt;
} else {
$scope.bookmarks = $scope.bookmarkData.bookmarksLatestClick;
}
$scope.bookmarkCount = $scope.bookmarkData.totalItems;
$scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems); $scope.totalPages = Math.ceil($scope.bookmarkCount / perPageItems);
$scope.inputPage = ''; $scope.inputPage = '';

View File

@ -51,13 +51,13 @@
<table class="ui selectable sortable celled table js-tags-table" ng-if="bookmarkCount > 0" style="margin-top:-14px;" ng-show="!loadBookmarks && !editMode"> <table class="ui selectable sortable celled table js-tags-table" ng-if="bookmarkCount > 0" style="margin-top:-14px;" ng-show="!loadBookmarks && !editMode">
<thead> <thead>
<tr> <tr>
<th>标题</th> <th style="cursor:default;">标题</th>
<th>链接</th> <th style="cursor:default;">链接</th>
<th style=" width:90px;" class="sorted descending">点击次数</th> <th style=" width:90px;" class="sorted" ng-class="{descending: order[0]}" ng-click="changeOrder(0)">点击次数</th>
<th style="width:100px;" class="sorted ascending">创建日期</th> <th style="width:100px;" class="sorted" ng-class="{descending: order[1]}" ng-click="changeOrder(1)">创建日期</th>
<th style="width:100px;" class="sorted descending">最后点击</th> <th style="width:100px;" class="sorted" ng-class="{descending: order[2]}" ng-click="changeOrder(2)">最后点击</th>
<th style="width:150px;">分类</th> <th style="width:150px;cursor:default;">分类</th>
<th style="width:88px;">操作</th> <th style="width:88px;cursor:default;">操作</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -72,8 +72,8 @@
<span title="{{bookmark.url}} 点击复制链接" ng-click="copy(bookmark.id, bookmark.url)" id="tagurl{{bookmark.id}}" style="cursor:default;">{{ bookmark.url }}</span> <span title="{{bookmark.url}} 点击复制链接" ng-click="copy(bookmark.id, bookmark.url)" id="tagurl{{bookmark.id}}" style="cursor:default;">{{ bookmark.url }}</span>
</td> </td>
<td>{{ bookmark.click_count }}</td> <td>{{ bookmark.click_count }}</td>
<td>{{ bookmark.created_at }}</td> <td>{{ bookmark.created_at.substr(0, 10) }}</td>
<td>{{ bookmark.last_click }}</td> <td>{{ bookmark.last_click.substr(0, 10) }}</td>
<td> <td>
<div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="getBookmarks(tag.id, 1)"> <div class="ui label" ng-repeat="tag in bookmark.tags" tag-id="{{ tag.id }}" ng-click="getBookmarks(tag.id, 1)">
{{ tag.name }} {{ tag.name }}

View File

@ -358,14 +358,18 @@ api.get('/bookmarksByTag', function(req, res) {
var totalItems = 0; var totalItems = 0;
var totalItems = 0; var totalItems = 0;
var sendData = { var sendData = {
totalItems: totalItems, totalItems: 0,
bookmarks: [] bookmarksClickCount: [],
bookmarksCreatedAt: [],
bookmarksLatestClick: [],
} }
db.getBookmarksByTag(params) db.getBookmarksByTag(params)
.then((bookmarksData) => { .then((bookmarksData) => {
bookmarks = bookmarksData.bookmarks; sendData = bookmarksData;
totalItems = bookmarksData.totalItems; var bookmarkIds = []
var bookmarkIds = bookmarks.map((bookmark) => bookmark.id); .concat(sendData.bookmarksClickCount.map((bookmark) => bookmark.id))
.concat(sendData.bookmarksCreatedAt.map((bookmark) => bookmark.id))
.concat(sendData.bookmarksLatestClick.map((bookmark) => bookmark.id))
return db.getTagsBookmarks(bookmarkIds); return db.getTagsBookmarks(bookmarkIds);
}) })
.then((tbs) => { .then((tbs) => {
@ -373,25 +377,24 @@ api.get('/bookmarksByTag', function(req, res) {
return db.getTags(userId); return db.getTags(userId);
}) })
.then((tags) => { .then((tags) => {
var data = [];
// 获取每个书签的所有分类标签 // 获取每个书签的所有分类标签
bookmarks.forEach(function(bookmark) { var objectName = ['bookmarksClickCount', 'bookmarksCreatedAt', 'bookmarksLatestClick'];
var bookmarkTags = []; objectName.forEach((name) => {
tagsBookmarks.forEach(function(tb) { console.log(JSON.stringify(tagsBookmarks));
if (tb.bookmark_id == bookmark.id) { sendData[name].forEach(function(bookmark, index) {
tags.forEach(function(tag) { var bookmarkTags = [];
if (tb.tag_id == tag.id) { tagsBookmarks.forEach(function(tb) {
bookmarkTags.push(tag) if (tb.bookmark_id == bookmark.id) {
} tags.forEach(function(tag) {
}) if (tb.tag_id == tag.id) {
} bookmarkTags.push(tag)
}); }
bookmark.tags = bookmarkTags; })
data.push(bookmark); }
});
sendData[name][index].tags = bookmarkTags;
})
}) })
sendData.totalItems = totalItems;
sendData.bookmarks = data;
res.json(sendData); res.json(sendData);
}) })
.catch((err) => console.log('bookmarks table or card err', err)) .catch((err) => console.log('bookmarks table or card err', err))
@ -591,13 +594,13 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) {
var tags = []; var tags = [];
item.tags.forEach((tag) => { item.tags.forEach((tag) => {
allTags.forEach((at) => { allTags.forEach((at) => {
if (at.name == tag) { if (at.name == tag) {
tags.push(at.id); tags.push(at.id);
} }
})
}) })
}) // 插入书签
// 插入书签
db.addBookmark(userId, bookmark) // 插入书签 db.addBookmark(userId, bookmark) // 插入书签
.then((bookmark_id) => { .then((bookmark_id) => {
db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类 db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类