where([ 'code'=>$code, 'code.status'=>1, 'batch.status'=>1, 'batch.starttime'=>['<',time()], 'batch.endtime'=>['>',time()] ])->find(); if(!$data){ return false; } return $data; } public function batch() { return $this->belongsTo('Batch', 'batch_id', 'id', [], 'LEFT')->setEagerlyType(0); } /** * 兑换 * @param $userId 用户 * @param $goodsId 商品 * @return bool */ public function exchange($userId,$batchId,$code) { $goods = \app\api\model\app\exchange\Goods::getBatchGoods($batchId); if($goods){ $subscriptionModel = new \app\common\model\user\Subscription(); foreach ($goods as $item){ $subscriptionModel->setUserCourse($userId,$item['goods_id'],'exchange'); \app\api\model\app\exchange\UseLog::insert([ 'user_id'=>$userId, 'uniacid'=>UNIACID, 'goods_id'=>$item['goods_id'], 'code'=>$code, 'batch_id'=>$batchId, 'createtime'=>time() ]); self::where([ 'code'=>$code, 'batch_id'=>$batchId ])->update([ 'user_id'=>$userId, 'use_time'=>time(), 'status'=>2 ]); } } return true; } }