GoodsOptionBuyLimit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2023/1/3
  8. * Time: 9:18
  9. */
  10. namespace app\common\services\goods;
  11. use Carbon\Carbon;
  12. use app\common\models\Goods;
  13. use app\common\models\Order;
  14. use app\common\models\Member;
  15. use Illuminate\Support\Facades\DB;
  16. use app\common\models\GoodsOption;
  17. use app\common\models\MemberGroup;
  18. use app\common\models\MemberLevel;
  19. use app\common\models\goods\Privilege;
  20. use app\common\exceptions\AppException;
  21. class GoodsOptionBuyLimit
  22. {
  23. /**
  24. * @var Privilege
  25. */
  26. public $privilege;
  27. /**
  28. * @var GoodsOption
  29. */
  30. public $goodsOption;
  31. public function __construct(Privilege $privilege, GoodsOption $goodsOption)
  32. {
  33. $this->privilege = $privilege;
  34. $this->goodsOption = $goodsOption;
  35. }
  36. public function getGoodsTitle()
  37. {
  38. $name = '';
  39. if ($this->goodsOption->goods) {
  40. $name .= $this->goodsOption->goods->title.'|';
  41. } else {
  42. $name .= 'ID:'.$this->goodsOption->goods_id.'|';
  43. }
  44. $name .= $this->goodsOption->title;
  45. return $name;
  46. }
  47. public function getOrderGoodsWhere()
  48. {
  49. return app('db')->getTablePrefix()."yz_order_goods.goods_option_id = {$this->goodsOption->id}";
  50. }
  51. /**
  52. * @param Member $member
  53. * @param $num
  54. * @throws AppException
  55. */
  56. public function goodsValidate(Member $member, $num)
  57. {
  58. //不在限制的规格内
  59. if (!in_array($this->goodsOption->id, $this->privilege->option_id_array)) {
  60. return;
  61. }
  62. $this->validateMinBuyLimit($num);
  63. $this->validateDayBuyTotalLimit($num);
  64. $this->validateOneBuyLimit($num);
  65. $this->validateDayBuyLimit($member,$num);
  66. $this->validateWeekBuyLimit($member,$num);
  67. $this->validateMonthBuyLimit($member,$num);
  68. $this->validateTotalBuyLimit($member,$num);
  69. $this->validateMemberLevelLimit($member);
  70. $this->validateMemberGroupLimit($member);
  71. $this->validateBuyMultipleLimit($num);
  72. }
  73. /**
  74. * 商品单次购买最低数量
  75. * @param $num
  76. * @throws AppException
  77. */
  78. public function validateMinBuyLimit($num)
  79. {
  80. //只做平台商品验证,解除限制所以商品都会验证
  81. if (intval($this->privilege->min_buy_limit) > 0) {
  82. if ($num < $this->privilege->min_buy_limit) {
  83. throw new AppException('商品:(' . $this->getGoodsTitle() . '),未到达最低购买数量' . $this->privilege->min_buy_limit . '件');
  84. }
  85. }
  86. }
  87. /**
  88. * 商品每日购买限制
  89. * @param Member $member
  90. * @param int $num
  91. * @throws AppException
  92. */
  93. public function validateDayBuyTotalLimit($num = 1)
  94. {
  95. if ($this->privilege->day_buy_total_limit > 0) {
  96. $start_time = Carbon::today()->timestamp;
  97. $end_time = Carbon::now()->timestamp;
  98. $rang = [$start_time,$end_time];
  99. $history_num =
  100. \app\common\models\OrderGoods::select('yz_order_goods.*')
  101. ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
  102. ->whereRaw($this->getOrderGoodsWhere())
  103. ->where('yz_order.status', '!=' ,Order::CLOSE)
  104. ->whereBetween('yz_order_goods.created_at',$rang)
  105. ->sum('yz_order_goods.total');
  106. if ($history_num + $num > $this->privilege->day_buy_total_limit) {
  107. throw new AppException('商品(' . $this->getGoodsTitle() . ')每日最多售出' . $this->privilege->day_buy_total_limit . '件');
  108. }
  109. }
  110. }
  111. /**
  112. * 用户单次购买限制
  113. * @param $num
  114. * @throws AppException
  115. */
  116. public function validateOneBuyLimit($num = 1)
  117. {
  118. if ($this->privilege->once_buy_limit > 0) {
  119. if ($num > $this->privilege->once_buy_limit) {
  120. throw new AppException('商品(' . $this->getGoodsTitle() . ')单次最多可购买' . $this->privilege->once_buy_limit . '件');
  121. }
  122. }
  123. }
  124. /**
  125. * 用户每日购买限制
  126. * @param Member $member
  127. * @param int $num
  128. * @throws AppException
  129. */
  130. public function validateDayBuyLimit(Member $member,$num = 1)
  131. {
  132. if ($this->privilege->day_buy_limit > 0) {
  133. $start_time = Carbon::today()->timestamp;
  134. $end_time = Carbon::now()->timestamp;
  135. $rang = [$start_time,$end_time];
  136. $history_num = $member
  137. ->orderGoods()
  138. ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
  139. ->whereRaw($this->getOrderGoodsWhere())
  140. ->where('yz_order.status', '!=' ,Order::CLOSE)
  141. ->whereBetween('yz_order_goods.created_at',$rang)
  142. ->sum('yz_order_goods.total');
  143. if ($history_num + $num > $this->privilege->day_buy_limit) {
  144. throw new AppException('您今天已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),该商品每天最多可购买' . $this->privilege->day_buy_limit . '件');
  145. }
  146. }
  147. }
  148. /**
  149. * 用户每周购买限制
  150. * @param Member $member
  151. * @param int $num
  152. * @throws AppException
  153. */
  154. public function validateWeekBuyLimit(Member $member,$num = 1)
  155. {
  156. if ($this->privilege->week_buy_limit > 0) {
  157. $start_time = Carbon::now()->startOfWeek()->timestamp;
  158. $end_time = Carbon::now()->timestamp;
  159. $rang = [$start_time,$end_time];
  160. $history_num = $member
  161. ->orderGoods()
  162. ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
  163. ->whereRaw($this->getOrderGoodsWhere())
  164. ->where('yz_order.status', '!=' ,Order::CLOSE)
  165. ->whereBetween('yz_order_goods.created_at',$rang)
  166. ->sum('yz_order_goods.total');
  167. if ($history_num + $num > $this->privilege->week_buy_limit) {
  168. throw new AppException('您这周已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),该商品每周最多可购买' . $this->privilege->week_buy_limit . '件');
  169. }
  170. }
  171. }
  172. /**
  173. * 用户每月购买限制
  174. * @param Member $member
  175. * @param int $num
  176. * @throws AppException
  177. */
  178. public function validateMonthBuyLimit(Member $member,$num = 1)
  179. {
  180. if ($this->privilege->month_buy_limit > 0) {
  181. $start_time = Carbon::now()->startOfMonth()->timestamp;
  182. $end_time = Carbon::now()->timestamp;
  183. $range = [$start_time,$end_time];
  184. $history_num = $member
  185. ->orderGoods()
  186. ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
  187. ->whereRaw($this->getOrderGoodsWhere())
  188. ->where('yz_order.status', '!=' ,Order::CLOSE)
  189. ->whereBetween('yz_order_goods.created_at',$range)
  190. ->sum('yz_order_goods.total');
  191. if ($history_num + $num > $this->privilege->month_buy_limit) {
  192. throw new AppException('您这个月已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),该商品每月最多可购买' . $this->privilege->month_buy_limit . '件');
  193. }
  194. }
  195. }
  196. /**
  197. * 用户购买总数限制
  198. * @param Member $member
  199. * @param int $num
  200. * @throws AppException
  201. */
  202. public function validateTotalBuyLimit(Member $member,$num = 1)
  203. {
  204. if ($this->privilege->total_buy_limit > 0) {
  205. $history_num = $member->orderGoods()
  206. ->join('yz_order', 'yz_order_goods.order_id', '=', 'yz_order.id')
  207. ->whereRaw($this->getOrderGoodsWhere())
  208. ->where('yz_order.status', '!=' ,Order::CLOSE)
  209. ->sum('yz_order_goods.total');
  210. if ($history_num + $num > $this->privilege->total_buy_limit) {
  211. throw new AppException('您已购买' . $history_num . '件商品(' . $this->getGoodsTitle() . '),最多可购买' . $this->privilege->total_buy_limit . '件');
  212. }
  213. }
  214. }
  215. /**
  216. * 用户等级限制
  217. * @param Member $member
  218. * @throws AppException
  219. */
  220. public function validateMemberLevelLimit(Member $member)
  221. {
  222. if (empty($this->privilege->buy_levels) && $this->privilege->buy_levels !== '0') {
  223. return;
  224. }
  225. $buy_levels = explode(',', $this->privilege->buy_levels);
  226. if ($this->privilege->buy_levels !== '0') {
  227. $level_names = MemberLevel::select(DB::raw('group_concat(level_name) as level_name'))->whereIn('id', $buy_levels)->value('level_name');
  228. if (empty($level_names)) {
  229. return;
  230. }
  231. }
  232. if (!in_array($member->yzMember->level_id, $buy_levels)) {
  233. $ordinaryMember = in_array('0', $buy_levels)? '普通会员 ':'';
  234. throw new AppException('商品(' . $this->getGoodsTitle() . ')仅限' . $ordinaryMember.$level_names . '购买');
  235. }
  236. }
  237. /**
  238. * 用户组限购
  239. * @param Member $member
  240. * @throws AppException
  241. */
  242. public function validateMemberGroupLimit(Member $member)
  243. {
  244. if (empty($this->privilege->buy_groups)) {
  245. return;
  246. }
  247. $buy_groups = explode(',', $this->privilege->buy_groups);
  248. $group_names = MemberGroup::select(DB::raw('group_concat(group_name) as level_name'))->whereIn('id', $buy_groups)->value('level_name');
  249. if (empty($group_names)) {
  250. return;
  251. }
  252. if (!in_array($member->yzMember->group_id, $buy_groups)) {
  253. throw new AppException('(' . $this->getGoodsTitle() . ')该商品仅限[' . $group_names . ']购买');
  254. }
  255. }
  256. /**
  257. * 用户单次购买倍数限制
  258. * @param $num
  259. * @throws AppException
  260. */
  261. public function validateBuyMultipleLimit($num = 1)
  262. {
  263. //只做平台商品验证,解除限制所以商品都会验证
  264. if (intval($this->privilege->buy_multiple) > 0) {
  265. if ($num % $this->privilege->buy_multiple != 0) {
  266. throw new AppException('商品:(' . $this->getGoodsTitle() . '),购买数量需为' . $this->privilege->buy_multiple . '的倍数');
  267. }
  268. }
  269. }
  270. }