y100.js 10.7 KB
const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;

const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_Y100 = 'act_article_y100';
const playExpireTime = 45 * 1000;
const showExpireTime = 30 * 1000;

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


    /**
     * 获取Y100列表
     * @returns {*}
     */
    articleY100List({actId, pageNo, pageSize, tag, top}) {
        const params = {
            actId,
            start: (pageNo - 1) * pageSize,
            page: _.parseInt(pageSize)
        };
        let sql = `
            SELECT 
                AAY.id, 
                AA.good_count,
                AA.create_time,
                AA.id article_id,
                AAY.name,
                AAY.img_url,
                AAY.career,
                AAY.interest,
                AAY.name,
                AAY.style,
                AAY.tag,
                AAY.desc
            FROM ${TABLE_ACT_ARTICLE} AA
            INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
            WHERE AA.act_id = :actId`;

        if (tag) {
            sql += ' and AAY.tag like :tag';
            params.tag = `%${tag}%`;

        }
        if (top === 1 || top === true) {
            sql += ' ORDER BY AAY.top DESC,AA.create_time DESC';
        } else {
            sql += ' ORDER BY AA.good_count DESC';
        }
        sql += ' LIMIT :start, :page';


        return mysqlCli.query(sql, params);
    }

    /**
     * 获取Y100列表-随机
     * @returns {*}
     */
    async articleY100RandomList({actId, num}) {
        const params = {
            actId,
        };
        let maxSql = `
            SELECT
                max(\`index\`) as maxIndex
            FROM ${TABLE_ACT_ARTICLE_Y100} AAY
            WHERE AAy.act_id = :actId`;

        const maxResult = await mysqlCli.query(maxSql, params);
        let maxIndex = maxResult[0].maxIndex;

        if (num > maxIndex) {
            num = maxIndex;

            // throw '随机数量超过数据库大小';
        }
        const ids = [],
            selectIds = [];

        for (let i = 0; i <= maxIndex; i++) {
            ids.push(i);
        }

        for (let i = 0; i < num; i++) {
            const index = parseInt(Math.random() * ids.length, 10);

            selectIds.push(ids[index]);
            ids.splice(index, 1);
        }

        let sql = `
            SELECT 
                AA.id article_id, 
                AA.good_count,
                AA.create_time,
                AAY.id,
                AAY.name,
                AAY.img_url
            FROM ${TABLE_ACT_ARTICLE} AA
            INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
            WHERE AA.act_id = :actId`;

        sql += ` and AAY.index in (${selectIds.join(',')})`;
        sql += ` order by field(AAY.index,${selectIds.join(',')});`;

        return mysqlCli.query(sql, params);
    }

    /**
     * 获取Y100详情
     * @returns {*}
     */
    async articleY100Detail(id) {
        let sql = `
            SELECT 
                AAY.id, 
                AAY.act_id, 
                AAY.article_id, 
                AAY.img_url, 
                AAY.tag, 
                AAY.name, 
                AAY.career, 
                AAY.style, 
                AAY.interest, 
                AAY.skns, 
                AAY.desc,
                AAY.create_time,
                AA.good_count
            FROM ${TABLE_ACT_ARTICLE_Y100} AAY
            INNER JOIN ${TABLE_ACT_ARTICLE} AA ON AAY.article_id = AA.id
            where AAY.id = :id`;

        const query = await mysqlCli.query(sql, {
            id
        });

        if (query.length === 1) {
            const result = query[0];
            const {skns} = query[0];

            if (skns) {

                const product_skns = skns.split('|')[0] || '';
                const recommend_skns = skns.split('|')[1] || '';

                const productsResult = await this.get({
                    data: {
                        method: 'h5.product.batch',
                        limit: product_skns.split(',').length,
                        productSkn: product_skns,
                        contain_all: 'Y'
                    },
                    param: {
                        cache: true
                    }
                });

                const recommendResult = await this.get({
                    data: {
                        method: 'h5.product.batch',
                        limit: recommend_skns.split(',').length,
                        productSkn: recommend_skns,
                        contain_all: 'Y'
                    },
                    param: {
                        cache: true
                    }
                });

                if (productsResult.code === 200 && productsResult.data) {
                    result.products = productsResult.data.product_list;
                    result.recommend = recommendResult.data.product_list;
                }
            }
            return result;
        }
    }

    /**
     * 使用设备更新用户信息
     * @returns {*}
     */
    async deviceUse({openId, userName, avatar, deviceNo, actId}) {
        const deviceQuery = await mysqlCli.query(
            `SELECT
                user_id,
                user_name,
                user_avatar,
                user_image,
                user_label,
                expire_time,
                status
            FROM
                act_devices_y100
            WHERE
                act_id = :actId
            AND
                device_no = :deviceNo`,
            {
                deviceNo,
                actId,
            });

        if (deviceQuery.length !== 1) {
            return {
                code: 400,
                message: '设备信息错误'
            };
        }
        const device = _.head(deviceQuery);

        if (device.expire_time && device.expire_time > Date.now()) {
            return {
                code: 401,
                message: `用户${device.user_name}正在使用大屏,请稍等一会`
            };
        }
        const expireTime = Date.now() + playExpireTime;

        await mysqlCli.update(`
            UPDATE
                act_devices_y100
            SET
                user_id = :openId,
                user_name = :userName,
                user_avatar = :avatar,
                user_image = '',
                user_label = '',
                expire_time = :expireTime,
                status = 0
            WHERE
                act_id = :actId
            AND
                device_no = :deviceNo`, {
                    openId,
                    userName,
                    avatar,
                    expireTime,
                    actId,
                    deviceNo
                });
        return {
            code: 200,
            message: '扫描成功',
            data: {
                expireTime
            }
        };
    }

    /**
     * 使用设备更新用户信息
     * @returns {*}
     */
    async devicePost({openId, deviceNo, actId, userImage, userLabel}) {
        const deviceQuery = await mysqlCli.query(
            `SELECT
                user_id,
                user_name,
                user_avatar,
                user_image,
                user_label,
                expire_time,
                status
            FROM
                act_devices_y100
            WHERE
                act_id = :actId
            AND
                device_no = :deviceNo`,
            {
                deviceNo,
                actId,
            });

        if (deviceQuery.length !== 1) {
            return {
                code: 400,
                message: '设备信息错误'
            };
        }
        const device = _.head(deviceQuery);

        if (device.expire_time <= Date.now()) {
            return {
                code: 400,
                message: '提交超时请再玩一次吧'
            };
        }

        if (device.user_id !== openId) {
            return {
                code: 401,
                message: `用户${device.user_name}正在使用大屏,请稍等一会`
            };
        }
        const expireTime = Date.now() + showExpireTime;

        await mysqlCli.update(`
            UPDATE
                act_devices_y100
            SET
                user_image = :userImage,
                user_label = :userLabel,
                expire_time = :expireTime,
                status = 1
            WHERE
                act_id = :actId
            AND
                device_no = :deviceNo`, {
                    openId,
                    expireTime,
                    userImage,
                    userLabel,
                    actId,
                    deviceNo
                });
        return {
            code: 200,
            message: '提交成功',
        };
    }

    /**
     * 检查设备状态
     * @returns {*}
     */
    async checkDeviceStatus({deviceNo, actId}) {
        const deviceQuery = await mysqlCli.query(
            `SELECT
                user_id,
                user_name,
                user_avatar,
                user_image,
                user_label,
                expire_time,
                status
            FROM
                act_devices_y100
            WHERE
                act_id = :actId
            AND
                device_no = :deviceNo`,
            {
                deviceNo,
                actId,
            });

        if (deviceQuery.length !== 1) {
            return {
                code: 400,
                message: '设备信息错误'
            };
        }
        const device = _.head(deviceQuery);

        if (device.expire_time > Date.now()) {
            if (device.status === 0) {
                return {
                    code: 1002,
                    message: 'playing',
                    data: {
                        openId: device.user_id,
                        userName: device.user_name,
                        avatar: device.user_avatar,
                        expireTime: device.expire_time
                    }
                };
            }
            if (device.status === 1) {
                return {
                    code: 10003,
                    message: 'show image',
                    data: {
                        openId: device.user_id,
                        userName: device.user_name,
                        avatar: device.user_avatar,
                        userImage: device.user_image,
                        userLabel: device.user_label,
                        expireTime: device.expire_time
                    }
                };
            }
        }
        return {
            code: 1001,
            message: 'show qrcode'
        };
    }
}

module.exports = Y100Model;