| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2023/1/3
- * Time: 9:18
- */
- namespace app\common\services\goods;
- use Carbon\Carbon;
- use app\common\models\Goods;
- use app\common\models\Order;
- use app\common\models\Member;
- use Illuminate\Support\Facades\DB;
- use app\common\models\GoodsOption;
- use app\common\models\MemberGroup;
- use app\common\models\MemberLevel;
- use app\common\models\goods\Privilege;
- use app\common\exceptions\AppException;
- class GoodsOptionBuyLimit
- {
- /**
- * @var Privilege
- */
- public $privilege;
- /**
- * @var GoodsOption
- */
- public $goodsOption;
- public function __construct(Privilege $privilege, GoodsOption $goodsOption)
- {
- $this->privilege = $privilege;
- $this->goodsOption = $goodsOption;
- }
- public function getGoodsTitle()
- {
- $name = '';
- if ($this->goodsOption->goods) {
- $name .= $this->goodsOption->goods->title.'|';
- } else {
- $name .= 'ID:'.$this->goodsOption->goods_id.'|';
- }
- $name .= $this->goodsOption->title;
- return $name;
- }
- public function getOrderGoodsWhere()
- {
- return app('db')->getTablePrefix()."yz_order_goods.goods_option_id = {$this->goodsOption->id}";
- }
- /**
- * @param Member $member
- * @param $num
- * @throws AppException
- */
- public function goodsValidate(Member $member, $num)
- {
- //不在限制的规格内
- if (!in_array($this->goodsOption->id, $this->privilege->option_id_array)) {
- return;
- }
- $this->validateMinBuyLimit($num);
- $this->validateDayBuyTotalLimit($num);
- $this->validateOneBuyLimit($num);
- $this->validateDayBuyLimit($member,$num);
- $this->validateWeekBuyLimit($member,$num);
- $this->validateMonthBuyLimit($member,$num);
- $this->validateTotalBuyLimit($member,$num);
- $this->validateMemberLevelLimit($member);
- $this->validateMemberGroupLimit($member);
- $this->validateBuyMultipleLimit($num);
- }
- /**
- * 商品单次购买最低数量
- * @param $num
- * @throws AppException
- */
- public function validateMinBuyLimit($num)
- {
- //只做平台商品验证,解除限制所以商品都会验证
- if (intval($this->privilege->min_buy_limit) > 0) {
- if ($num < $this->privilege->min_buy_limit) {
- throw new AppException('商品:(' . $this->getGoodsTitle() . '),未到达最低购买数量' . $this->privilege->min_buy_limit . '件');
- }
- }
- }
- /**
- * 商品每日购买限制
- * @param Member $member
- * @param int $num
- * @throws AppException
- */
- public function validateDayBuyTotalLimit($num = 1)
- {
- if ($this->privilege->day_buy_total_limit > 0) {
- $start_time = Carbon::today()->timestamp;
- $end_time = Carbon::now()->timestamp;
- $rang = [$start_time,$end_time];
- $history_num =
- \app\common\models\OrderGoods::select('yz_order_goods.*')
- ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
- ->whereRaw($this->getOrderGoodsWhere())
- ->where('yz_order.status', '!=' ,Order::CLOSE)
- ->whereBetween('yz_order_goods.created_at',$rang)
- ->sum('yz_order_goods.total');
- if ($history_num + $num > $this->privilege->day_buy_total_limit) {
- throw new AppException('商品(' . $this->getGoodsTitle() . ')每日最多售出' . $this->privilege->day_buy_total_limit . '件');
- }
- }
- }
- /**
- * 用户单次购买限制
- * @param $num
- * @throws AppException
- */
- public function validateOneBuyLimit($num = 1)
- {
- if ($this->privilege->once_buy_limit > 0) {
- if ($num > $this->privilege->once_buy_limit) {
- throw new AppException('商品(' . $this->getGoodsTitle() . ')单次最多可购买' . $this->privilege->once_buy_limit . '件');
- }
- }
- }
- /**
- * 用户每日购买限制
- * @param Member $member
- * @param int $num
- * @throws AppException
- */
- public function validateDayBuyLimit(Member $member,$num = 1)
- {
- if ($this->privilege->day_buy_limit > 0) {
- $start_time = Carbon::today()->timestamp;
- $end_time = Carbon::now()->timestamp;
- $rang = [$start_time,$end_time];
- $history_num = $member
- ->orderGoods()
- ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
- ->whereRaw($this->getOrderGoodsWhere())
- ->where('yz_order.status', '!=' ,Order::CLOSE)
- ->whereBetween('yz_order_goods.created_at',$rang)
- ->sum('yz_order_goods.total');
- if ($history_num + $num > $this->privilege->day_buy_limit) {
- throw new AppException('您今天已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),该商品每天最多可购买' . $this->privilege->day_buy_limit . '件');
- }
- }
- }
- /**
- * 用户每周购买限制
- * @param Member $member
- * @param int $num
- * @throws AppException
- */
- public function validateWeekBuyLimit(Member $member,$num = 1)
- {
- if ($this->privilege->week_buy_limit > 0) {
- $start_time = Carbon::now()->startOfWeek()->timestamp;
- $end_time = Carbon::now()->timestamp;
- $rang = [$start_time,$end_time];
- $history_num = $member
- ->orderGoods()
- ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
- ->whereRaw($this->getOrderGoodsWhere())
- ->where('yz_order.status', '!=' ,Order::CLOSE)
- ->whereBetween('yz_order_goods.created_at',$rang)
- ->sum('yz_order_goods.total');
- if ($history_num + $num > $this->privilege->week_buy_limit) {
- throw new AppException('您这周已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),该商品每周最多可购买' . $this->privilege->week_buy_limit . '件');
- }
- }
- }
- /**
- * 用户每月购买限制
- * @param Member $member
- * @param int $num
- * @throws AppException
- */
- public function validateMonthBuyLimit(Member $member,$num = 1)
- {
- if ($this->privilege->month_buy_limit > 0) {
- $start_time = Carbon::now()->startOfMonth()->timestamp;
- $end_time = Carbon::now()->timestamp;
- $range = [$start_time,$end_time];
- $history_num = $member
- ->orderGoods()
- ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
- ->whereRaw($this->getOrderGoodsWhere())
- ->where('yz_order.status', '!=' ,Order::CLOSE)
- ->whereBetween('yz_order_goods.created_at',$range)
- ->sum('yz_order_goods.total');
- if ($history_num + $num > $this->privilege->month_buy_limit) {
- throw new AppException('您这个月已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),该商品每月最多可购买' . $this->privilege->month_buy_limit . '件');
- }
- }
- }
- /**
- * 用户购买总数限制
- * @param Member $member
- * @param int $num
- * @throws AppException
- */
- public function validateTotalBuyLimit(Member $member,$num = 1)
- {
- if ($this->privilege->total_buy_limit > 0) {
- $history_num = $member->orderGoods()
- ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
- ->whereRaw($this->getOrderGoodsWhere())
- ->where('yz_order.status', '!=' ,Order::CLOSE)
- ->sum('yz_order_goods.total');
- if ($history_num + $num > $this->privilege->total_buy_limit) {
- throw new AppException('您已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),最多可购买' . $this->privilege->total_buy_limit . '件');
- }
- }
- }
- /**
- * 用户等级限制
- * @param Member $member
- * @throws AppException
- */
- public function validateMemberLevelLimit(Member $member)
- {
- if (empty($this->privilege->buy_levels) && $this->privilege->buy_levels !== '0') {
- return;
- }
- $buy_levels = explode(',', $this->privilege->buy_levels);
- if ($this->privilege->buy_levels !== '0') {
- $level_names = MemberLevel::select(DB::raw('group_concat(level_name) as level_name'))->whereIn('id', $buy_levels)->value('level_name');
- if (empty($level_names)) {
- return;
- }
- }
- if (!in_array($member->yzMember->level_id, $buy_levels)) {
- $ordinaryMember = in_array('0', $buy_levels)? '普通会员 ':'';
- throw new AppException('商品(' . $this->getGoodsTitle() . ')仅限' . $ordinaryMember.$level_names . '购买');
- }
- }
- /**
- * 用户组限购
- * @param Member $member
- * @throws AppException
- */
- public function validateMemberGroupLimit(Member $member)
- {
- if (empty($this->privilege->buy_groups)) {
- return;
- }
- $buy_groups = explode(',', $this->privilege->buy_groups);
- $group_names = MemberGroup::select(DB::raw('group_concat(group_name) as level_name'))->whereIn('id', $buy_groups)->value('level_name');
- if (empty($group_names)) {
- return;
- }
- if (!in_array($member->yzMember->group_id, $buy_groups)) {
- throw new AppException('(' . $this->getGoodsTitle() . ')该商品仅限[' . $group_names . ']购买');
- }
- }
- /**
- * 用户单次购买倍数限制
- * @param $num
- * @throws AppException
- */
- public function validateBuyMultipleLimit($num = 1)
- {
- //只做平台商品验证,解除限制所以商品都会验证
- if (intval($this->privilege->buy_multiple) > 0) {
- if ($num % $this->privilege->buy_multiple != 0) {
- throw new AppException('商品:(' . $this->getGoodsTitle() . '),购买数量需为' . $this->privilege->buy_multiple . '的倍数');
- }
- }
- }
- }
|