admin.js 2.44 KB
/**
 * 管理model
 * @author: leo <qi.li@yoho.cn>
 * @date: 28/06/2017
 */

const mysqlCli = global.yoho.utils.mysqlCli;

const TABLE_ACTIVITY = 'activity';
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_USER = 'user';

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

    /**
     * 创建活动
     * @param title 活动标题
     * @param startTime 活动开始时间
     * @param endTime 活动结束时间
     * @returns {*}
     */
    createActivity({title, startTime, endTime}) {
        return mysqlCli.insert(
            `insert into ${TABLE_ACTIVITY} (title, start_time, end_time) values (:title, :startTime, :endTime);`,
            {
                title,
                startTime,
                endTime
            }
        );
    }

    /**
     * 活动列表
     * @returns {*}
     */
    activityList() {
        return mysqlCli.query(
            `select id, title, start_time startTime, end_time endTime, create_time createTime from ${TABLE_ACTIVITY};`
        );
    }

    /**
     * 删除活动
     * @param actId 活动ID
     * @returns {*}
     */
    deleteActivity(actId) {
        return mysqlCli.delete(
            `delete from ${TABLE_ACTIVITY} where id = :actId;`,
            {
                actId
            }
        );
    }

    /**
     * 活动文章列表
     * @returns {*}
     */
    actArticleList(actId) {
        return mysqlCli.query(
            `select taa.id, taa.content, taa.good_count goodCount, tu.user_name userName 
            from ${TABLE_ACT_ARTICLE} taa
            inner join ${TABLE_USER} tu
            on taa.user_id = tu.id 
            where act_id = :actId;`, {
                actId
            }
        );
    }

    /**
     * 删除文章
     * @param id 文章ID
     * @returns {*}
     */
    deleteArticle(id) {
        return mysqlCli.delete(
            `delete from ${TABLE_ACT_ARTICLE} where id = :id;`,
            {
                id
            }
        );
    }

    /**
     * 参与活动用户列表
     * @returns {*}
     */
    activityUserList(actId) {
        return mysqlCli.query(
            `select tu.user_phone phone
            from ${TABLE_ACTIVITY} ta
            inner join act_article taa
            on ta.id = taa.act_id
            inner join user tu
            on taa.user_id = tu.id
            where ta.id = :actId;`, {
                actId
            }
        );
    }
}

module.exports = AdminModel;