105 lines
3.5 KiB
PHP
105 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use DB;
|
|
use Carbon\Carbon;
|
|
use App\Models\{User, ActivityUser, ActivityGood, ActivityOption};
|
|
|
|
/**
|
|
* Class OrderService
|
|
*
|
|
* @package App\Services
|
|
*/
|
|
class GoodsService
|
|
{
|
|
/**
|
|
* 检测商品是否有效
|
|
*/
|
|
public function checkGoodsInfo(){
|
|
|
|
}
|
|
|
|
public function addGoodsSellNum(){
|
|
|
|
}
|
|
/**
|
|
* 检测是否有足够的奖品兑换
|
|
*
|
|
*/
|
|
public function canExchange($goods_id, $user_id){
|
|
$activity_goods = ActivityGood::where('goods_id',$goods_id)->first();
|
|
$activity_user = ActivityUser::where('user_id',$user_id)->first();
|
|
if($activity_goods && $activity_user){
|
|
$cancel_num = 0;
|
|
$need_exchange = [];
|
|
for($i =1; $i<10; $i++){
|
|
$key = 'award_'.$i;
|
|
if($activity_goods->$key > 0){
|
|
$need_exchange[] = $i;
|
|
if($activity_user->$key <= 0){
|
|
$cancel_num++;
|
|
}
|
|
}
|
|
}
|
|
//判断万能图数量足够不
|
|
if($cancel_num > 0){
|
|
//计算当前用户有多少万能图
|
|
$all_use = ActivityOption::where('is_all', 1)->get();
|
|
$user_all_num = 0;
|
|
foreach($all_use as $is_all){
|
|
$key = 'award_'.$is_all->id;
|
|
$user_all_num += $activity_user->$key;
|
|
}
|
|
//判断
|
|
if($cancel_num > $user_all_num){
|
|
return false;
|
|
}else{
|
|
return $need_exchange;
|
|
}
|
|
}else{
|
|
return $need_exchange;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function goodsCartPrice(){
|
|
$cart_price = 0;
|
|
$goods_cart = auth('api')->user()->getUserCart()->toArray();
|
|
|
|
$too_goods_arr = [];
|
|
foreach($goods_cart as $t_goods){
|
|
/** 处理第二杯半价商品start **/
|
|
if($t_goods['is_too'] == 1){
|
|
if(isset($too_goods_arr[$t_goods['goods_id']])){
|
|
$too_goods_arr[$t_goods['goods_id']]['goods_num'] += $t_goods['goods_num'];
|
|
}else{
|
|
$too_goods_arr[$t_goods['goods_id']]['goods_num'] = $t_goods['goods_num'];
|
|
}
|
|
if(!isset($too_goods_arr[$t_goods['goods_id']]['goods_price']) ||
|
|
$too_goods_arr[$t_goods['goods_id']]['goods_price'] > $t_goods['goods_price'])
|
|
{
|
|
$too_goods_arr[$t_goods['goods_id']]['goods_price'] = $t_goods['goods_price'];
|
|
}
|
|
}
|
|
$cart_price += $t_goods['goods_price']*$t_goods['goods_num'];
|
|
}
|
|
$order_special_value = 0;
|
|
if(count($too_goods_arr) > 0){
|
|
foreach($goods_cart as $key=> $o_goods){
|
|
if(isset($too_goods_arr[$o_goods['goods_id']]['goods_num']) && $too_goods_arr[$o_goods['goods_id']]['goods_num'] >=2){
|
|
if($o_goods['goods_price'] == $too_goods_arr[$o_goods['goods_id']]['goods_price']){
|
|
$goods_cart[$key]['special_value'] = number_format(round(intval($o_goods['goods_price']*100)/2)/100, 2);
|
|
$order_special_value += $goods_cart[$key]['special_value'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if($order_special_value){
|
|
$cart_price -= $order_special_value;
|
|
}
|
|
|
|
return $cart_price;
|
|
}
|
|
} |