where(['sjId' =>$sjId,'takeStatus'=>0]); $query->andWhere(['>','deadline',$now]); $count = $query->count(); if($count >= $num){ return ['code'=>'A0002','msg'=>'您还有邀请码没有使用哦']; } $maxInvite = xhInvite::find()->where(['sjId' =>$sjId])->orderBy('id desc')->asArray()->one(); $invteId = 0; if(!empty($maxInvite)){ $invteId = $maxInvite['id']; } for($n=0;$n<5;$n++){ $invteId++; $rand = strtolower(stringUtil::getRandNum(3));//随机数不能太小,防止被猜中 $deadline = $now + 86400*30;//一个月有效期 $randNum = rand(0,1); $amount = [3900,3899.99]; $save = $amount[$randNum]; $save = (int)3900; $newData = ['sjId'=>$sjId,'code'=>$sjId.'-'.$invteId.'-'.$rand, 'save'=>$save,'prize'=>0,'deadline'=>$deadline,'addTime'=>$now,'createTime'=>date("Y-m-d H:i:s",$now)]; self::add($newData); } return ['code'=>'A0001','msg'=>'操作成功']; } public static function add($data) { $return = xhInvite::add($data); $code = $return['code']; $cacheKey = dict::getCacheKey('xhInvite').$code; $params = [$cacheKey]; if(!empty($return)){ foreach($return as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params); return $return; } public static function getByCode($code) { $cacheKey = dict::getCacheKey('xhInvite').$code; $invite = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]); if(!empty($invite)){ $arr = []; $i = 0; $x = 0; foreach($invite as $key => $val){ $i++; if($i%2 == 0){ $arr[$x]['value'] = $val; $x++; }else{ $arr[$x]['key'] = $val; } } $inviteArr = []; foreach($arr as $key => $val){ $inviteArr[$val['key']] = $val['value']; } unset($inviteArr['info_already_get_by_sql']); return $inviteArr; } $invite = xhInvite::find()->where(['code'=>$code])->asArray()->one(); $params = [$cacheKey]; $params[] = 'info_already_get_by_sql'; $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过 if(!empty($invite)){ foreach($invite as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params); return $invite; } public static function updateByCode($code,$data) { xhInvite::updateByCondition(['code'=>$code], $data); self::getByCode($code); $cacheKey = dict::getCacheKey('xhInvite').$code; $params = [$cacheKey]; if(!empty($data)){ foreach($data as $key => $val){ $params[] = $key; $params[] = $val; } } Yii::$app->redis->executeCommand('HMSET',$params); } }