$vip_id, 'status' => 1, 'merchantid' => $merchant_id, ); $vip_info = $VipModel->getFind($vMap); if (!$vip_info) { $res = array('status' => 0, 'msg' => '会员查询失败'); return $res; } // 获取会员等级 $grade_id = $VipIntegralModel->getOrderFindField(array('min_integral' => array('elt', $vip_info['grade_integral'])), 'id', 'min_integral desc'); $vip_info['grade_id'] = $grade_id ? $grade_id : 0; // 会员注册福利查询条件 $wMap = array( $NewVipWelfareModel::STATUS => 1, $NewVipWelfareModel::MERCHANT_ID => $merchant_id, $NewVipWelfareModel::TYPE => 1, $NewVipWelfareModel::M_COUPON_ID => array('neq', 0), ); if ($add_scene == 1) { $wMap[$NewVipWelfareModel::CASHIER_REGISTER] = 1; } else if ($add_scene == 2) { $wMap[$NewVipWelfareModel::WX_REGISTER] = 1; } else if ($add_scene == 3) { // 小程序 return true; } else { return false; // 未知的注册 } // 查询注册福利关联的优惠券 $coupon_ids = $NewVipWelfareModel->getFindField($wMap, $NewVipWelfareModel::M_COUPON_ID, true); if (!$coupon_ids) { $res = array('status' => 0, 'msg' => '暂无注册福利'); return $res; } // 优惠券查询条件 $store_id = $vip_info[$VipModel::STORE_ID]; $cMap = array( $MCouponModel::ID => array('in', $coupon_ids), $MCouponModel::MERCHANT_ID => $merchant_id, $MCouponModel::STATUS => 1, $MCouponModel::COUPON_NUM => array('gt', 0), $MCouponModel::TYPE => 1, $MCouponModel::STORE_IDS => array( // 判断门店 array('eq', 0), array('eq', $store_id), array('like', "{$store_id},%"), array('like', "%,{$store_id},%"), array('like', "%,{$store_id}"), 'OR', ), array( array( $MCouponModel::COUPON_TYPE => 1, // 判断有效期 $MCouponModel::START_TIME => array('lt', $time), $MCouponModel::END_TIME => array('gt', $time), ), array($MCouponModel::COUPON_TYPE => array('in', '2,3')), '_logic' => "OR" ), ); //判断会员等级限制 if ($vip_type == 2) { //plus会员 $cMap[$MCouponModel::COUPON_GRADE_ID] = array('in', '-1,0'); } else { //普通会员 $cMap[$MCouponModel::COUPON_GRADE_ID] = array( array('eq', 0), array('eq', $grade_id), array('like', "{$grade_id},%"), array('like', "%,{$grade_id},%"), array('like', "%,{$grade_id}"), 'OR', ); } // 查询优惠券 $coupon = $MCouponModel->getSelect($cMap); if (!$coupon) { $res = array('status' => 0, 'msg' => '暂无可用的优惠券发放'); return $res; } $XcxUserModel = new RetailSmallProgramUserModel(); $uid = $XcxUserModel->getFindField(['vid' => $vip_id], 'id'); $isVip = $uid ? true : false; foreach ($coupon as &$val) { // 优惠券发放 $RechargeApi = new RechargeController(); $result = $RechargeApi->publicGiveCoupon($vip_info[$VipModel::ID], $val, $merchant_id); if ($result['status'] == 1) { // 发放成功,减库存 $dec_result = $MCouponModel->toSetDec(array($MCouponModel::ID => $val[$MCouponModel::ID]), $MCouponModel::COUPON_NUM, 1); unset($dec_result); if ($isVip) { $RetailMessageModel = new RetailMessageModel(); $RetailMessageModel->couponSkipAdd($uid, $val['id']); } } unset($result); } return true; } }