prize.js 6.91 KB
/**
 * 抽奖model
 * @author: qiujun <jun.qiu@yoho.cn>
 * @date: 30/01/2018
 */

const mySqlCli = global.yoho.utils.mysqlCli;
const TABLE_PRIZE = 'act_prize_user_log';
const TABLE_USER_DETAIL = 'act_user_detail_info';

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

    /**
     * 检查用户的获奖信息
     * @param actId
     * @param userPhone
     */
    checkPrize(actId, userPhone) {
        let strSql = `SELECT id, user_phone, is_get, user_name, user_address, user_shoes_size FROM ${TABLE_PRIZE} 
                    WHERE act_id = :actId 
                    AND user_phone = :userPhone`;

        return mySqlCli.query(strSql, {
            actId,
            userPhone
        }).then(ret => {
            let result = {};

            console.log(ret);
            if (ret.length <= 0) {
                result = {
                    code: 201,
                    message: '未找到获奖信息'
                };
            } else if (ret.length > 0) {
                result = {
                    code: 200,
                    data: ret
                };
            }

            return Promise.resolve(result);
        });
    }

    /**
     * 查询用户是否参与抽奖
     * @param actId
     * @param userPhone
     * @returns {Promise.<*>}
     */
    async checkUserPrize(actId, userPhone) {
        let strFindSql = `SELECT COUNT(*) AS user_prize_count FROM ${TABLE_PRIZE} 
                        WHERE act_id = :actId 
                        AND user_phone = :userPhone;`;

        return mySqlCli.query(strFindSql, {
            actId,
            userPhone
        }).then(ret => {
            return ret[0].user_prize_count || 0;
        });
    }

    /**
     * 查看已中奖的奖品数量
     * @param actId
     * @param isGet
     * @returns {Promise.<*>}
     */
    async checkPrizeNums(actId, isGet) {
        let strSql = `SELECT COUNT(*) AS total_prize_count FROM ${TABLE_PRIZE} 
                    WHERE act_id = :actId 
                    AND is_get = :isGet;`;

        return mySqlCli.query(strSql, {
            actId,
            isGet
        }).then(ret => {
            return ret[0].total_prize_count || 0;
        });
    }

    /**
     * 插入新的中奖信息
     * @param actId
     * @param userPhone
     * @param isGet
     * @returns {Promise.<*>}
     */
    async addNewPrize(actId, userPhone, isGet) {
        let strInsertSql = `INSERT INTO ${TABLE_PRIZE} (act_id, user_phone, is_get) 
                            VALUES(:actId, :userPhone, :isGet)`;

        return mySqlCli.insert(strInsertSql, {
            actId,
            userPhone,
            isGet
        });
    }

    /**
     * 检查并添加中奖者基础信息
     * @param actId
     * @param userPhone
     * @param isGet
     */
    addPrize(actId, userPhone, isGet) {
        let strFindSql = `SELECT COUNT(*) as user_prize_count FROM ${TABLE_PRIZE} 
                    WHERE act_id = :actId 
                    AND user_phone = :userPhone`;

        let strCountSql = `SELECT COUNT(*) AS total_prize_count FROM ${TABLE_PRIZE} 
                        WHERE act_id = :actId AND is_get = 1`;

        let strInsertSql = `INSERT INTO ${TABLE_PRIZE} (act_id, user_phone, is_get) 
                            VALUES(:actId, :userPhone, :isGet)`;

        return mySqlCli.query(strFindSql, {
            actId,
            userPhone
        }).then(ret => {
            console.log(ret);
            if (ret[0] && ret[0].user_prize_count <= 0) {
                return mySqlCli.query(strCountSql, {
                    actId
                }).then(ret2 => {
                    console.log(ret2);
                    let result = {};

                    if (ret2[0].total_prize_count >= 20) {
                        isGet = 0;
                        result = {
                            code: 204,
                            message: '奖品已领取完'
                        };
                    } else {
                        result = {
                            code: 200,
                            message: '抽奖信息添加成功!'
                        };
                    }

                    return mySqlCli.insert(strInsertSql, {
                        actId,
                        userPhone,
                        isGet
                    }).then(ret3 => {
                        return Promise.resolve(Object.assign(result, {data: ret3}));
                    });
                });
            } else {
                return Promise.resolve({
                    code: 203,
                    message: '请勿重复领奖'
                });
            }
        });
    }

    /**
     * 更新中奖者的详细信息
     * @param actId
     * @param userPhone
     * @param name
     * @param address
     * @param shoesSize
     */
    updatePrize(actId, userPhone, name, address, shoesSize) {
        let strSql = `UPDATE ${TABLE_PRIZE} SET 
                      user_name=:name,
                      user_address=:address,
                      user_shoes_size=:shoesSize 
                      WHERE user_phone=:userPhone 
                      AND is_get > 0 
                      AND act_id=:actId`;



        return mySqlCli.update(strSql, {
            actId,
            userPhone,
            name,
            address,
            shoesSize
        }).then(ret => {
            return Promise.resolve({
                code: 200,
                data: ret
            });
        });
    }

    saveUserInfo(actId, name, birthday, gender, phone, email, province, city) {
        let strFind = `SELECT COUNT(*) AS user_count 
                        FROM ${TABLE_USER_DETAIL} 
                        WHERE act_id=:actId 
                        AND user_phone=:phone`;

        let strInsert = `INSERT INTO ${TABLE_USER_DETAIL} 
                        (act_id, user_name, user_birthday, user_gender, 
                        user_phone, user_email, user_province, user_city) 
                        VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;

        return mySqlCli.query(strFind, {
            actId,
            phone
        }).then(ret => {
            if (ret && ret[0].user_count <= 0) {
                return mySqlCli.insert(strInsert, {
                    actId,
                    name,
                    birthday,
                    gender,
                    phone,
                    email,
                    province,
                    city
                }).then(ret2 => {
                    return Promise.resolve({
                        code: 200,
                        message: '用户信息添加成功',
                        data: ret2
                    });
                });
            } else {
                return Promise.resolve({
                    code: 203,
                    message: '已存在的用户信息'
                });
            }
        });
    }
}

module.exports = PrizeModel;