@ -48,7 +48,10 @@ class Order extends Api
continue;
}
$value['is_allow_access'] = $this->userIsAllowAccess($user, $value, $time) > 0 ? 1 : 0;
$value['is_allow_access'] = 0;
if ($user['pid'] > 0) {
$value['is_allow_access'] = 1;
}
}
@ -61,15 +64,20 @@ class Order extends Api
* @param [type] $warehouse [description]
* @return [type] [description]
*/
public function userIsAllowAccess($user, $warehouse, $time){
public function userIsAllowAccess($user, $warehouse, $goods, $ time){
$advance_access_minute = $user['advance_access_minute'] * 60;
$is_allow_access = 0;
//是否在时间范围内
$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;
//仓库限制用户总抢购次数
if ($warehouse['limit_buy_num'] > 0) {
$count = Db::name('user_access_record')->where('user_id', $user['id'])->count();
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->count();
// var_dump($count);
// exit();
if ($count > $warehouse['limit_buy_num']) {
$is_allow_access = 0;
return $is_allow_access;
@ -81,7 +89,7 @@ class Order extends Api
if (($time + $advance_access_minute) >= $start & & ($time + $advance_access_minute) < = $end) {
//提前进入次数
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('is_advance', 1)->count();
$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']) {
$is_allow_access = 1;
return $is_allow_access;
@ -92,7 +100,7 @@ class Order extends Api
//正常进场
if ($time >= $start & & $time < = $end) {
//正常进入次数
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('is_advance',0)->count();
$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']) {
$is_allow_access = 2;
return $is_allow_access;
@ -103,6 +111,59 @@ class Order extends Api
return $is_allow_access;
}
/**
* 限制购买次数
* [limitBuyNum description]
* @param [type] $user [description]
* @param [type] $warehouse [description]
* @return [type] [description]
*/
public function limitBuyNum($user, $warehouse){
$is_allow_access = 0;
//是否在时间范围内
$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']) {
$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']) {
$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']) {
$is_allow_access = 1;
return $is_allow_access;
}
return $is_allow_access;
}
/**
* 获取购买时间
* [getBuyTime description]
* @param [type] & $warehouse [description]
* @param [type] $user [description]
* @return [type] [description]
*/
public function getBuyTime(& $warehouse, $user){
$advance_access_minute = $user['advance_access_minute'] * 60;
$time = strtotime($warehouse['start']) - $advance_access_minute;
$warehouse['start'] = date("H:i", $time);
}
/**
* 商品列表
* [getGoodsList description]
@ -117,11 +178,12 @@ class Order extends Api
if (!$warehouse) {
$this->error("区域不存在");
}
$is_allow_access = $this->userIsAllowAccess($user, $warehouse, time());
if ($is_allow_access == 0) {
$this->error("不允许进入抢购");
$is_allow_access = $this->limitBuyNum($user, $warehouse);
// $is_allow_access = $this->userIsAllowAccess($user, $warehouse, time());
if ($is_allow_access > 0 ) {
$this->error("抢购次数被限制");
}
$list = Goods::where('warehouse_id', $warehouse_id)->where('goods_type', 1)->order('id', 'desc')->paginate($this->request->param('list_rows', $limit ))->each(function ($item, $key){
$list = Goods::where('warehouse_id', $warehouse_id)->where('goods_type', 1)->where('owner_id', "< >", $user['id'])->order('status', 'desc')-> order('id', 'desc')->paginate($this->request->param('list_rows', 20 ))->each(function ($item, $key){
$warehouse = Warehouse::where('id', $item['warehouse_id'])->find();
$owner = User::where('id', $item['owner_id'])->find();
$item['warehouse'] = $warehouse;
@ -145,11 +207,12 @@ 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("不允许进入抢购");
}
// $is_allow_access = $this->userIsAllowAccess($user, $warehouse, time());
// if ($is_allow_access == 0) {
// $this->error("不允许进入抢购");
// }
$warehouse = Warehouse::where('id', $info['warehouse_id'])->find();
$this->getBuyTime($warehouse, $user);
$owner = User::where('id', $info['owner_id'])->find();
$info['warehouse'] = $warehouse;
$info['owner'] = $owner;
@ -161,7 +224,7 @@ class Order extends Api
$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' => $is_allow_access == 1 ? 1 : 2 ]);
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);
}
@ -186,19 +249,7 @@ class Order extends Api
}
$order_amount = $goods['price'] * $num;
$coupon_price = 0;
if ($coupon_id) {
// $coupon = UserCouponModel::with('coupon')->where('id', $coupon_id)->where('user_id', $user_id)->find();
// if($order_amount < $coupon['enough']) {
// $this->error("不能使用优惠卷");
// }
// if($coupon['use_time'] > 0) {
// $this->error("优惠卷已使用");
// }
// if(strtotime($coupon['user_end_time']) < time ( ) ) {
// $this->error("优惠卷已过期");
// }
// $coupon_price = $coupon['amount'] ?? 0;
}
$pay_amount = $order_amount - $coupon_price;
@ -265,6 +316,8 @@ class Order extends Api
$where = [];
if ($status != "") {
$where['status'] = ['in', explode(",", $status)];
} else {
$where['status'] = ['in', [0,1,3,4,5,6]];
}
$list = OrderModel::where('seller_id', $user_id)->where($where)->order('id', 'desc')->paginate($this->request->param('list_rows', $limit))->each(function ($item, $key){
$order_goods = Detail::where('order_id', $item['id'])->select();
@ -294,6 +347,8 @@ class Order extends Api
$where = [];
if ($status != "") {
$where['status'] = ['in', explode(",", $status)];
} else {
$where['status'] = ['in', [0,1,2,3,4,5,6,7]];
}
$list = OrderModel::where('buyer_id', $user_id)->where($where)->order('id', 'desc')->paginate($this->request->param('list_rows', $limit))->each(function ($item, $key){
$order_goods = Detail::where('order_id', $item['id'])->select();
@ -337,6 +392,7 @@ class Order extends Api
$info['entrustment_ratio'] = Config::get("site.entrustment_ratio");
$seller = User::where('id', $info['seller_id'])->field(['id', 'username', 'nickname', 'mobile', 'avatar', 'score','sign_image','sign_status','alipay_image','wechat_image','buy_goods_num_per_day','advance_access_num','bank_realname','bank_mobile','bank_no','bank_address','reason','warehouse_id','advance_access_minute','deadline','pid','total_commission_amount','available_commission_amount'])->find();
$info['seller'] = $seller;
$info['pay_voucher'] = cdnurl($info['pay_voucher'], true);
$this->success("succ", $info);
}
/**
@ -447,7 +503,7 @@ class Order extends Api
if($coupon['use_time'] > 0) {
$this->error("优惠卷已使用");
}
if(strtotime($coupon['user _end_time']) < time ( ) ) {
if(strtotime($coupon['use_end_time']) < time ( ) ) {
$this->error("优惠卷已过期");
}
$coupon_price = $coupon['amount'] ?? 0;
@ -488,10 +544,11 @@ class Order extends Api
$order_data['status'] = 1;//佣金支付
Db::name("pay_record")->insert($order_data);
//修改商品价格和归属人
Goods::where('id', $goods['goods_id'])->update(['price' => $goods_price, 'owner_id' => $user_id,"updatetime" => time(),'status'=>'normal']);
Goods::where('id', $goods['goods_id'])->update(['price' => intval( $goods_price) , 'owner_id' => $user_id,"updatetime" => time(),'status'=>'normal']);
//写入用户收益
$profit = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") - Config::get("site.entrustment_ratio")) * 0.01, 2);
$profit = $profit + $coupon_price + $pay_amount;
Record::create(["user_id" => $user_id, "type" => 2, "item_id" => $order_id,"amount"=> $profit]);
//修改用户可用佣金
User::where('id', $user_id)->update(['available_commission_amount' => $user['available_commission_amount'] - $pay_amount,"profit_amount" =>$profit, "updatetime" => time()]);
@ -568,18 +625,20 @@ class Order extends Api
Db::name("pay_record")->where('order_id', $order_id)->update(['status'=>1, "update_time"=>time()]);
//修改商品价格和归属人
$goods_price = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") * 0.01 + 1), 2);
Goods::where('id', $goods['goods_id'])->update(['price' => $goods_price, 'owner_id' => $user_id,"updatetime" => time(),"status"=>'normal']);
Goods::where('id', $goods['goods_id'])->update(['price' => intval( $goods_price) , 'owner_id' => $user_id,"updatetime" => time(),"status"=>'normal']);
$order_info = OrderModel::where("id", $order_id)->find();
//写入用户收益
$profit = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") - Config::get("site.entrustment_ratio")) * 0.01, 2);
Record::create(["user_id" => $user_id, "type" => 2, "item_id" => $order_id,"amount"=> $profit]);
$profit = $profit + $order_info['coupon_price'] + $goods['dikou_price'];
Record::create(["user_id" => $user_id, "type" => 2, "item_id" => $order_id,"amount"=> $profit ]);
//修改用户可用佣金
User::where('id', $user_id)->update(['available_commission_amount' => $user['available_commission_amount'] - $goods['dikou_price'],"profit_amount" => $user['profit_amount'] + $profit, "updatetime" => time()]);
//更新订单状态
OrderModel::where("id", $order_id)->update(['status' => 7]);
//使用优惠卷
$order_info = OrderModel::where("id", $order_id)->find();
if ($order_info['coupon_id']) {
UserCouponModel::where('id', $order_info['coupon_id'])->update(['use_order_id'=>$order_id, "use_time"=>time()]);
}