lqmac 1 year ago
parent 88a772aa19
commit 5e5cd0c62c
  1. 63
      application/api/controller/Order.php

@ -123,30 +123,30 @@ class Order extends Api
*/
public function limitBuyNum($user, $warehouse){
$is_allow_access = 0;
//return $is_allow_access;
//是否在时间范围内
$start = strtotime($warehouse['start']);
$end = strtotime($warehouse['end']);
$start_date = date("Y-m-d H:i:s",$start);
$end_date = date("Y-m-d H:i:s", $end);
$code = $start."-".$end;
//仓库限制用户总抢购次数
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->count();
if ($warehouse['limit_buy_num'] > 0 && $count > $warehouse['limit_buy_num']) {
$start_date = date("Y-m-d H:i",$start);
$end_date = date("Y-m-d H:i", $end);
$code = $start_date."-".$end_date;
//提前下单次数(买单)
$advance_count = OrderModel::where('buyer_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 1)->count();
if ($user['advance_access_num'] > 0 && $advance_count >= $user['advance_access_num']) {
$is_allow_access = 1;
return $is_allow_access;
}
//提前进入次数
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 1)->count();
if ($user['advance_access_num'] > 0 && $count > $user['advance_access_num']) {
//正常下单次数(买单)
$normal_count = OrderModel::where('buyer_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 0)->count();
// var_dump($normal_count);
// exit();
if ($user['buy_goods_num_per_day'] > 0 && $normal_count >= $user['buy_goods_num_per_day']) {
$is_allow_access = 1;
return $is_allow_access;
}
//正常进入次数
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance',0)->count();
if ($user['buy_goods_num_per_day'] > 0 && $count > $user['buy_goods_num_per_day']) {
$count = $advance_count + $normal_count;
//总下单次数
if ($warehouse['limit_buy_num'] > 0 && $count >= $warehouse['limit_buy_num']) {
$is_allow_access = 1;
return $is_allow_access;
}
@ -182,7 +182,6 @@ class Order extends Api
$this->error("区域不存在");
}
$is_allow_access = $this->limitBuyNum($user, $warehouse);
// $is_allow_access = $this->userIsAllowAccess($user, $warehouse, time());
if ($is_allow_access > 0 ) {
$this->error("抢购次数被限制");
}
@ -210,11 +209,10 @@ class Order extends Api
}
$user = $this->auth->getUserinfo();
$warehouse = Warehouse::where('id', $info['warehouse_id'])->find();
// $is_allow_access = $this->userIsAllowAccess($user, $warehouse, time());
// if ($is_allow_access == 0) {
// $this->error("不允许进入抢购");
// }
$warehouse = Warehouse::where('id', $info['warehouse_id'])->find();
$is_allow_access = $this->limitBuyNum($user, $warehouse);
if ($is_allow_access > 0 ) {
$this->error("抢购次数被限制");
}
$this->getBuyTime($warehouse, $user);
$owner = User::where('id', $info['owner_id'])->find();
$info['warehouse'] = $warehouse;
@ -222,13 +220,13 @@ class Order extends Api
$info['image'] = cdnurl($info['image'], true);
//写进入记录
$start = date("Y-m-d H:i:s", strtotime($warehouse['start']));
$end = date("Y-m-d H:i:s", strtotime($warehouse['end']));
$code = $start."-".$end;
$info1 = Db::name('user_access_record')->where('warehouse_id', $info['warehouse_id'])->where('user_id', $user['id'])->where('code', $code)->count();
if (!$info1) {
Db::name('user_access_record')->insert(['user_id' => $user['id'],'warehouse_id' => $info['warehouse_id'],'code' => $code, "createtime"=>time(), 'is_advance' => $user['advance_access_minute'] > 0 ? 1 : 0]);
}
// $start = date("Y-m-d H:i:s", strtotime($warehouse['start']));
// $end = date("Y-m-d H:i:s", strtotime($warehouse['end']));
// $code = $start."-".$end;
// $info1 = Db::name('user_access_record')->where('warehouse_id', $info['warehouse_id'])->where('user_id', $user['id'])->where('code', $code)->count();
// if (!$info1) {
// Db::name('user_access_record')->insert(['user_id' => $user['id'],'warehouse_id' => $info['warehouse_id'],'code' => $code, "createtime"=>time(), 'is_advance' => $user['advance_access_minute'] > 0 ? 1 : 0]);
// }
$this->success("succ", $info);
}
/**
@ -263,6 +261,10 @@ class Order extends Api
if (time() > strtotime($warehouse['end'])) {
$this->error("抢购时间已结束!");
}
$is_allow_access = $this->limitBuyNum($user, $warehouse);
if ($is_allow_access > 0) {
$this->error("抢购次数被限制");
}
//使用redis锁,限制下单
$lock_key = $this->request->domain()."_createOrder_".$goods_id;
$res = Cache::store('redis')->setnx($lock_key, 1);
@ -275,6 +277,9 @@ class Order extends Api
$coupon_price = 0;
$pay_amount = $order_amount - $coupon_price;
$order_sn = "ZH".date("YmdHis").rand(1000,9999);
$start = date("Y-m-d H:i", strtotime($warehouse['start']));
$end = date("Y-m-d H:i", strtotime($warehouse['end']));
$code = $start."-".$end;
//创建订单
$orderData = [
"buyer_id" => $user_id,
@ -291,6 +296,8 @@ class Order extends Api
"city" => $address['city_name'],
"district" => $address['district_name'],
"address" => $address['address'],
"is_advance" => $start > time() ? 1 : 0,
"code" => $code,
];
$order = OrderModel::create($orderData);
if ($order === false) {

Loading…
Cancel
Save