coupon.js 3.6 KB
/* eslint-disable array-callback-return */
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_COUPON = 'act_coupon';
const TABLE_COUPON_USER = 'act_coupon_user';
const TABLE_COUPON_NO = 'act_coupon_no';

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

    couponList() {
        return mysqlCli.query(
            `select id, coupon_name couponName, coupon_desc couponDesc,  shop_logo_url shopLogoUrl,  sort  ,type
            from ${TABLE_COUPON} where status = 1
            order by sort desc ,create_time desc`);
    }

    couponUserFlag(userId) {
        return mysqlCli.query(`select cn.send_flag sendFlag, cu.coupon_id couponId 
        from  ${TABLE_COUPON_USER} as cu ,${TABLE_COUPON_NO} as cn where cu.user_id = :userId and cn.id = cu.coupon_no_id `, {userId});
    }

    couponUserOwner(userId) {
        return mysqlCli.query(
            `select c.id, c.coupon_name couponName, c.coupon_desc couponDesc,  c.shop_logo_url shopLogoUrl,cn.coupon_no couponNo
            from ${TABLE_COUPON} c ,${TABLE_COUPON_NO} cn, ${TABLE_COUPON_USER} cu where cu.user_id  =:userId and cu.coupon_no_id  = cn.id and c.id = cn.coupon_id
            order by cu.create_time desc`, {userId});
    }

    async coouponUserGet(obj) {

        // 判断该用户是否有该券
        let num = await mysqlCli.query(`select count(1) as count from ${TABLE_COUPON_USER} where user_id = :userId and coupon_id = :couponId`,
            {
                userId: obj.userId, couponId: obj.couponId
            });

        if (num[0].count) {
            return Promise.resolve({code: 201, result: false, message: '领取失败,该用户已领取过该券'});
        }

        // 获取当前可用券码(如果是type=1、3则no表就一条数据send_flag都为0)
        let couponNolist = await mysqlCli.query(`select id from ${TABLE_COUPON_NO} where coupon_id = :couponId and send_flag = 0 `, {couponId: obj.couponId});

        if (!couponNolist.length) {
            return Promise.resolve({code: 202, result: false, message: '领取失败,该券已发放完毕'});
        }

        let noId = couponNolist[0].id;

        // 如果type=2(一人一码) 则修改第一张待用券码的send状态
        if (obj.type === 2) {
            await mysqlCli.update(`UPDATE ${TABLE_COUPON_NO} SET send_flag = 1 where id = :id`, {id: noId});
        }

        // 在type=2的情况下判断 该券并发是否被别人领取 如果没有再在user表中插入数据
        let insertId = await mysqlCli.insert(`insert into ${TABLE_COUPON_USER} (user_id, coupon_id , coupon_no_id) 
           select :userId,:couponId,:couponNoId from DUAL where NOT EXISTS(
                 SELECT cu.id 
                 FROM ${TABLE_COUPON_USER} cu left join ${TABLE_COUPON} c on c.id = cu.coupon_id
                 WHERE cu.coupon_no_id = :couponNoId and c.type = 2 and c.id = :couponId)`,
            {userId: obj.userId, couponId: obj.couponId, couponNoId: noId});

        if (!insertId) {
            await this.coouponUserGet(obj);
        } else {
            return Promise.resolve({code: 200, result: true});
        }
    }

    couponById(id) {
        if (!id) {
            return new Promise(resolve => {
                return resolve({});
            });
        } else {
            return mysqlCli.query(
                `select id, coupon_name couponName, coupon_desc couponDesc, shop_name shopName, shop_logo_url shopLogoUrl, status, type, sort , create_time createTime 
            from ${TABLE_COUPON}
            where id = :id`, {
                id
            });
        }
    }

}

module.exports = CouponModel;