//表kucun,字段nums int
//表log,字段uid int/logtime datetime
use libraryUsualToolIncUTInc;
use libraryUsualToolRedisUTRedis;
use libraryUsualToolDataUTData;
$redis=UTRedis::GetRedis();
$redis->watch('sale');//监测锁
$sale=$redis->get('sale'); //库存
$n=UTData::QueryData("kucun","","id=1")["querydata"][0]["nums"];
if($sale>=$n){ //更新库存为0
UTData::UpdateData("kucun",array("nums"=>0),"id=1");
UTInc::GoUrl('-1','库存清零'); } //开启事务
$redis->multi(); $redis->incr('sale');//自增售卖数量
$res=$redis->exec();
if($res){ //增加销售记录
$uid=UTInc::GetRandomString(8);
UTData::InsertData("log",array("uid"=>$uid,"logtime"=>date('Y-m-d H:i:s',time())));
echo "下单成功";
}else{
UTInc::GoUrl('-1','下单失败');
}