https://www.sunzhongwei.com/using-laravel-sharedlock-and-lockforupdate-for-table-row-locks
https://www.cnblogs.com/kerrycode/p/6991502.html
https://laravel-china.org/docs/laravel/5.6/queries/1398
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use BaconQrCode\Common\Mode;
use PhpParser\Node\Expr\AssignOp\Mod;
use zm\Response\Services\Response;
use App\Models;
use Yansongda\LaravelPay\Facades\Pay;
use Illuminate\Http\Request;
//use Illuminate\Http\Response;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
use zm\Sms\Facades\Sms;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class PrizeController extends Controller
{
/**
* 抽奖列表
* @param Request $requset
*/
function get_rand($proArr) {
$result = '';
//概率数组的总概率精度
$proSum = array_sum($proArr);
//概率数组循环
foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}
public function index(Request $request,Response $response){
#https://blog.csdn.net/dongsg11200/article/details/24607877
#https://www.cnblogs.com/shiwenhu/p/5650269.html
#https://www.jb51.net/article/66065.htm
$prize_arr = Models\Prize_list::select('prize_id','prize_name','prize_rate')->get()->toArray();
foreach ($prize_arr as $key => $val) {
$arr[$val['prize_id']] = $val['prize_rate'];
}
$rid = $this->get_rand($arr); //根据概率获取奖项prize_id
$res['yes'] = $prize_arr[$rid-1]['prize_name']; //中奖项
unset($prize_arr[$rid-1]); //将中奖项从数组中剔除,剩下未中奖项
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){
$pr[] = $prize_arr[$i]['prize_name'];
}
$res['no'] = $pr;
echo "<pre>";
print_r($res);
}
public function test1(){
//echo __method__;
$result = Cache::add('pro_id','y',1);
if($result==1){
//code
}
$pro_1_num = Cache::get("pro_1_num");
if($pro_1_num == ""){
Cache::put("pro_1_num", 1, 2);
}elseif($pro_1_num<=10){
$pro_1_num+=1;
Cache::put("pro_1_num", $pro_1_num, 2);
$prize = Models\Prize_list::where('prize_id',1)->first();
//dd($prize->toArray());
if($prize->prize_num>0){
echo "ok";
Models\Prize_zhong_list::create(
[
"uid"=>1,
"mobile"=>"138****0731",
"prize_id"=>1,
"prize_name"=>"平板电脑",
"addtime"=>time()
]
);
Models\Prize_list::where('prize_id',1)->decrement('prize_num');
}
}
echo "pro_1_num=>".$pro_1_num."\r\n";
}
public function test2(){
DB::beginTransaction();
$results = DB::select('select * from prize_list where prize_id = :id for update', ['id' => 1]);
$results = $results[0];
#$sql="select storenum from `product` where 1 for update";
if($results->prize_num>0){
#echo "adfasd";
Models\Prize_zhong_list::create(
[
"uid"=>1,
"mobile"=>"138****0731",
"prize_id"=>1,
"prize_name"=>"平板电脑",
"addtime"=>time()
]
);
##Models\Prize_list::where('prize_id',1)->decrement('prize_num');
DB::table('prize_list')->where('prize_id',1)->decrement('prize_num');
DB::commit();
}
}
public function test3(){
DB::beginTransaction();
DB::table('prize_list')->where('prize_id',1)->decrement('prize_num');
Models\Prize_zhong_list::create(
[
"uid"=>1,
"mobile"=>"138****0731",
"prize_id"=>1,
"prize_name"=>"平板电脑",
"addtime"=>time()
]
);
$results = DB::select('select * from prize_list where prize_id = :id', ['id' => 1]);
$results = $results[0];
print_r($results->prize_num);
if($results->prize_num<0){
DB::rollback();
}else{
DB::commit();
}
}
public function test4(){
$fp = fopen("/tmp/lock.txt",'a');
#if(flock($fp,LOCK_EX)){
if(flock($fp, LOCK_EX | LOCK_NB)) {
$prize = Models\Prize_list::where('prize_id',1)->first();
if($prize->prize_num>0){
echo "ok";
Models\Prize_zhong_list::create(
[
"uid"=>1,
"mobile"=>"138****0731",
"prize_id"=>1,
"prize_name"=>"平板电脑",
"addtime"=>time()
]
);
Models\Prize_list::where('prize_id',1)->decrement('prize_num');
}
}else{
file_put_contents("log.txt","Couldn't lock the file!\r\n",FILE_APPEND);
}
fclose($fp);
}
public function test(){
$list = DB::table('prize_list')->where('prize_id', '=', 1)->sharedLock();
$prize = Models\Prize_list::where('prize_id',1)->first();
if($prize->prize_num>0){
Models\Prize_list::where('prize_id',1)->decrement('prize_num');
Models\Prize_zhong_list::create(
[
"uid"=>1,
"mobile"=>"138****0731",
"prize_id"=>1,
"prize_name"=>"平板电脑",
"addtime"=>time()
]
);
}
}
}