article.js 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/**
 * 文章model
 * @author: leo <qi.li@yoho.cn>
 * @date: 28/06/2017
 */

const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;

const TABLE_USER = 'user';
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_IMG = 'act_article_img';
const TABLE_ACT_ARTICLE_GOOD = 'act_article_good';

class ArticleModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 获取文章列表
     * @returns {*}
     */
    articleList({actId, pageNo, pageSize, orderBy, order}) {
        const orderMapping = {
            goodCount: 'good_count',
            createTime: 'create_time'
        };

        let limitSql;
        let orderSql;
        let whereSql;

        whereSql = 'WHERE AA.act_id = :actId';
        orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`;
        limitSql = 'LIMIT :start, :page';

        return mysqlCli.query(
            `SELECT 
                AA.id, 
                AA.good_count, 
                AAI.img_url, 
                AA.content, 
                AA.create_time,
                USR.user_name 
            FROM ${TABLE_ACT_ARTICLE} AA
            INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AAI
            INNER JOIN ${TABLE_USER} USR  
            ON AA.id = AAI.article_id AND AA.user_id = USR.id
            ${whereSql} ${orderSql} ${limitSql};`, {
                actId,
                start: (pageNo - 1) * pageSize,
                page: _.parseInt(pageSize)
            }
        );
    }

    /**
     * 获取文章无用户
     * @returns {*}
     */
    articleListWithoutUser({actId, pageNo, pageSize, orderBy, order, dateTime, limitDate}) {
        const orderMapping = {
            goodCount: 'good_count',
            createTime: 'create_time'
        };

        const date = dateTime || '';
        const limit_date = limitDate || '';

        let limitSql;
        let orderSql;
        let whereSql;

        whereSql = 'WHERE AA.act_id = :actId';
        if (date !== '') {
            whereSql += ' AND to_days(AA.create_time) = to_days(\'' + date + '\')';
        }
        if (limit_date !== '') {
            whereSql += ' AND to_days(AA.create_time) <= to_days(\'' + limit_date + '\')';
        }
        orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`;
        limitSql = 'LIMIT :start, :page';

        return mysqlCli.query(
            `SELECT 
                AA.id, 
                AA.good_count, 
                AAI.img_url, 
                AA.content, 
                AA.create_time,
                AA.user_name 
            FROM ${TABLE_ACT_ARTICLE} AA
            INNER JOIN ${TABLE_ACT_ARTICLE_IMG} AAI
            ON AA.id = AAI.article_id 
            ${whereSql} ${orderSql} ${limitSql};`, {
                actId,
                start: (pageNo - 1) * pageSize,
                page: _.parseInt(pageSize)
            }
        );
    }

    /**
     * 获取无图但有用户的文章列表,(用于获取用户得分列表)
     * @param actId
     * @param pageNo
     * @param pageSize
     * @param orderBy
     * @param order
     * @returns {*}
     */
    articleListWithUser({actId, pageNo, pageSize, orderBy, order}) {
        const orderMapping = {
            goodCount: 'good_count',
            createTime: 'create_time'
        };

        let limitSql;
        let orderSql;
        let whereSql;

        whereSql = 'WHERE AA.act_id = :actId';
        orderSql = `ORDER BY AA.${orderMapping[orderBy]} ${order}`;
        limitSql = 'LIMIT :start, :page';

        let strSql = `SELECT @row:=@row+1 as rank, UA.* 
                        FROM(
                            SELECT AA.id, AA.good_count, AA.user_id, U.user_avatar, U.user_name 
                            FROM ${TABLE_ACT_ARTICLE} AA 
                            INNER JOIN ${TABLE_USER} U 
                            ON AA.user_id = U.id 
                            ${whereSql} ${orderSql} ${limitSql}
                            ) AS UA,
                      (select @row:=0) as record;`;

        return mysqlCli.query(strSql, {
            actId,
            start: (pageNo - 1) * pageSize,
            page: _.parseInt(pageSize)
        });
    }

    /**
     * 获取文章列表无图
     * @returns {*}
     */
    articleListWithOutImg({actId, pageNo, pageSize, orderBy, order}) {
        const orderMapping = {
            goodCount: 'good_count',
            createTime: 'create_time',
            id: 'id'
        };

        let limitSql;
        let orderSql;
        let whereSql;

        whereSql = `WHERE act_id = ${actId}`;
        orderSql = `ORDER BY ${orderMapping[orderBy]} ${order}`;
        limitSql = `LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}`;
        return mysqlCli.query(
            `SELECT 
                id, 
                good_count, 
                content, 
                create_time
            FROM ${TABLE_ACT_ARTICLE}
            ${whereSql} ${orderSql} ${limitSql};`
        );
    }

    /**
     * 获取文章总数
     * @returns {*}
     */
    allArticlesNum(actId) {
        return mysqlCli.query(
            `SELECT COUNT(*) AS total FROM ${TABLE_ACT_ARTICLE} WHERE act_id = :actId;`, {
                actId
            }
        ).then(res => {
            return res[0].total;
        });
    }

    /**
     * 获取文章总数及投票总数
     * @returns {*}
     */
    allArticlesNumVote(actId) {
        return mysqlCli.query(
            `SELECT COUNT(*) AS total, SUM(good_count) AS total_count FROM 
            ${TABLE_ACT_ARTICLE} WHERE act_id = :actId;`, {
                actId
            }
        ).then(res => {
            return {total: res[0].total, total_count: res[0].total_count};
        });
    }

    /**
     * 发表文章
     * @param actId 活动ID
     * @param content 文章内容
     * @param imgUrl 图片链接
     * @returns {*}
     */
    createArticle({actId, content, imgUrl}) {
        const session = this.ctx.req.session;
        const userId = _.get(session, 'user.id');

        return mysqlCli.insert(
            `insert into ${TABLE_ACT_ARTICLE} (act_id, user_id, content) 
                values (:actId, :userId, :content);`,
            {
                actId,
                userId,
                content,
                imgUrl
            }
        );
    }

    /**
     * 发布无图片的文章(主要用于保存用户得分)
     * @param actId
     * @param userId
     * @param score
     * @returns {*}
     */
    createArticleWithoutImg(actId, userId, score) {
        let strSql = `insert into ${TABLE_ACT_ARTICLE} (act_id, user_id, good_count)
                values (:actId, :userId, :score);`;

        return mysqlCli.insert(strSql, {
            actId,
            userId,
            score
        });
    }

    /**
     * 通过actId与userId查找文章
     * @param actId
     * @param userId
     * @returns {*}
     */
    queryArticleByUserId(actId, userId) {
        let strSql = `select * from ${TABLE_ACT_ARTICLE} where 
                    act_id = :actId 
                    and user_id = :userId;`;

        return mysqlCli.query(strSql, {
            actId,
            userId
        });
    }

    /**
     * 通过articleId更新用户的goodCount
     * @param articleId
     * @param score
     * @returns {*}
     */
    updateArticleById(articleId, score) {
        let strSql = `update ${TABLE_ACT_ARTICLE} set 
                        good_count = :score where
                        id = :articleId;`;

        return mysqlCli.update(strSql, {
            score,
            articleId
        });
    }

    /**
     * 文章图片插入
     * @param articleId
     * @param imgUrl
     * @returns {*}
     */
    insertArticleImg(articleId, imgUrl) {
        return mysqlCli.insert(
            `insert into ${TABLE_ACT_ARTICLE_IMG} (article_id, img_url) values (:articleId, :imgUrl);`,
            {
                imgUrl,
                articleId
            }
        );
    }

    /**
     * 获取单个文章详情
     * @param articleId
     * @returns {*}
     */
    getSingleArticle(actId, articleId) {
        return mysqlCli.query(`SELECT AA.id AS id,
            AA.content AS content,
            AA.good_count AS good_count,
            AA.user_id as user_id,
            U.user_name as user_name,
            AA.create_time AS create_time,
            AAI.img_url 
            FROM act_article AS AA 
            LEFT JOIN act_article_img  AS AAI ON AA.id = AAI.article_id
            LEFT JOIN user AS U ON AA.user_id = U.id 
            WHERE AA.act_id = :actId 
            AND AA.id = :articleId;`,
            {
                actId,
                articleId
            }
        );
    }

    /**
     * 通过编号或者姓名获取单个文章详情
     * @param codeName
     * @returns {*}
     */
    getArticleByCodeName(actId, codeName) {
        return mysqlCli.query(`SELECT AA.id AS id,
            AA.content AS content,
            AA.good_count AS good_count,
            AA.create_time AS create_time,
            AA.user_name AS user_name,
            AAI.img_url 
            FROM act_article AS AA 
            LEFT JOIN act_article_img  AS AAI ON AA.id = AAI.article_id
            WHERE AA.act_id = :actId 
            AND (AA.content = :codeName 
            OR AA.user_name = :codeName);`,
            {
                actId,
                codeName
            }
        );
    }

    /**
     * 通过文章ID获取单个文章排名
     * @param articleId
     * @param actId
     * @returns {*}
     */
    getRankById(actId, articleId) {
        return mysqlCli.query(`select rownum from 
            (SELECT (@rowNo := @rowNo + 1) AS rownum, id, good_count FROM 
            (SELECT * from act_article WHERE act_id = :actId order by good_count desc) A, 
            (SELECT @rowNo := 0) B) C  WHERE C.id = :articleId;`,
            {
                actId,
                articleId
            }
        );
    }

    /**
     * 获取单个文章详情
     * @param articleId
     * @param actId
     * @returns {*}
     */
    getArticleById(actId, articleId) {
        return mysqlCli.query(`SELECT AA.id AS id,
            AA.content AS content,
            AA.good_count AS good_count,
            AA.user_name as user_name,
            AA.create_time AS create_time,
            AAI.img_url 
            FROM act_article AS AA 
            LEFT JOIN act_article_img  AS AAI ON AA.id = AAI.article_id
            WHERE AA.act_id = :actId 
            AND AA.id = :articleId;`,
            {
                actId,
                articleId
            }
        );
    }

    /**
     * 获取当前点赞用户点赞的文章的投票限制状态(repeat字段表示该文章是否可以重复投票,vote_limit字段表示该文章)
     * @param actId
     * @param articleId
     * @returns {*}
     */
    getActLimit(actId) {
        let sqlStr = `SELECT repeat_limit, vote_limit FROM activity WHERE 
                        id = ${actId}`;

        return mysqlCli.query(sqlStr);
    }

    /**
     * 获取当前点赞用户点赞时的IP,对应到当天(如果repeat为false,那么代表当前文章不能重复投票)
     * @param actId 活动id
     * @param articleId 文章id
     * @param repeat 是否对于该文章可以重复投票
     * @returns {*}
     */
    getArticleIp(actId, articleId, ip) {
        let sqlStr = `SELECT COUNT(*) as vote_count 
                    FROM ${TABLE_ACT_ARTICLE_GOOD} WHERE act_id = :actId 
                    AND ip = :ip
                    AND article_id = :articleId 
                    AND to_days(create_time) = to_days(now())`;

        return mysqlCli.query(sqlStr, {
            actId,
            ip,
            articleId
        });
    }

    /**
     * 获取当前点赞用户的ip今天已经投票的次数
     * @param actId
     * @param ip
     * @returns {*}
     */
    getIpCount(actId, ip) {
        let sqlStr = `SELECT count(*) AS vote_count
                    FROM ${TABLE_ACT_ARTICLE_GOOD} WHERE act_id = :actId 
                    AND ip = :ip 
                    AND to_days(create_time) = to_days(now())`;

        return mysqlCli.query(sqlStr, {
            actId,
            ip
        });
    }

    /**
     * 文章点赞
     * @param actId
     * @param articleId
     * @returns {*}
     */
    likeArticle(actId, articleId) {
        return mysqlCli.update(
            `UPDATE ${TABLE_ACT_ARTICLE} AA
            SET good_count = good_count + 1
            WHERE AA.act_id = :actId 
            AND AA.id = :articleId;`,
            {
                actId,
                articleId
            }
        );
    }

    /**
     * 插入点赞详情
     * @param actId
     * @param articleId
     * @returns {*}
     */
    insertLikeDetail(actId, articleId) {
        const userId = _.get(this.ctx.req.session, 'user.id', 0);
        const ip = this.header['X-Forwarded-For'] || this.ctx.req.ip || this.ctx.req.connection.remoteAddress;

        return mysqlCli.insert(
            `INSERT INTO ${TABLE_ACT_ARTICLE_GOOD} (act_id, article_id, user_id, ip)
            VALUES (:actId, :articleId, :userId, :ip);`,
            {
                ip,
                userId,
                actId,
                articleId
            }
        );
    }
}

module.exports = ArticleModel;