💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
https://www.thinkphp.cn/topic/59204.html ![](https://img.kancloud.cn/30/2a/302aa9582e7533440575b5905f2a424b_742x302.png) ![](https://img.kancloud.cn/58/a8/58a8bded296d63ca8ffbe02d38d2a551_1183x644.png) ![](https://img.kancloud.cn/0f/e2/0fe200b4d5d9fc8e00e1ba88c26d2e88_491x274.png) ![](https://img.kancloud.cn/a0/1a/a01ae6da05feedaf277d949504db00db_741x671.png) ***** ***** ***** ***** ![](https://img.kancloud.cn/3e/f9/3ef9783c6f79c713fde04a90b8172c72_1305x876.png) 这个时候需要设计一个相对灵活的代码。 比如优惠的方式有多重,比如:打折,满减,又或者是其他的玩法。所以计算优惠券的方式,用[策略模式](https://so.csdn.net/so/search?q=%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F&spm=1001.2101.3001.7020)相对会灵活一点。 https://blog.csdn.net/qq_22823581/article/details/110480356 ![](https://img.kancloud.cn/15/7d/157d147e0de530fc1151634aa117145c_1140x977.png) sql导出链接 然后就是对于券的可用不可用的判断,我这边用了call_user_func 这个函数,就是需要判断的方法,做一个循环判断,比如:这个券是否是指定会员可见的,是否符合满减条件等等。每个判断条件比较独立。便于后面的扩展性。 这边粗略的提供下我的思路,具体应用需要符合自己的应用场景中去实现。 当然,还有更灵活的方式,等待着发现。 ``` <?php interface calculateInterface { public function calculate(); } /** * 满减 * Class FullReduction */ class FullReduction implements calculateInterface { public function calculate() { return '满减'; } } class Discount implements calculateInterface { public function calculate() { return '打折'; } } class ToCalculate { public $obj; public function __construct(calculateInterface $obj) { $this->obj = $obj; } public function doing() { return $this->obj->calculate(); } } class Buy { public $error; /** * @var array */ public $condition = [ 'checkLimit', 'checkMember', 'checkTime', 'checkGoods', ]; public $couponInfo = [ 'isMember' => 1, 'name' => '双十一优惠券', 'expireDate' => '1609308076', 'startingTime' => '1606716076', 'type' => 'discount' ]; public $couponInfos = [ [ 'isMember' => 1, 'name' => '双十一优惠券', 'expireDate' => '1609308076', 'startingTime' => '1606716076', 'type' => 'discount' ], [ 'isMember' => 0, 'name' => '双十一优惠券', 'expireDate' => '1609308076', 'startingTime' => '1606716076', 'type' => 'discount' ], ]; public function dobuy() { //判断是那种计算方式 $obj = new ToCalculate(new Discount()); return $obj->doing(); } public function checkCoupons() { foreach ($this->couponInfos as $k => $coupon) { if (!$this->checkCondition($coupon)) { unset($this->couponInfos[$k]); } } return $this->couponInfos; } public function checkCoupon($couponInfo) { return $this->checkCondition($couponInfo); } public function checkCondition($counpon) { try { foreach ($this->condition as $condition) { $res = call_user_func([$this, $condition], $counpon); if (!$res) { if (empty($this->error)) { $this->error = '执行' . $condition . '错误'; } return false; } } } catch (\Exception $exception) { $this->error = $exception->getMessage(); return false; } return true; } public function checkTime($counpon) { return true; } public function checkMember($counpon) { //查找是否制定会员 if ($counpon['isMember'] != 1) { $this->error = '非会员'; return false; } return true; } public function checkGoods() { //检查商品 return true; } public function checkLimit() { //门槛 return true; } } $obj = new Buy(); echo '获取该店铺的所有符合条件的优惠券' . PHP_EOL; $all = $obj->checkCoupons(); if (!$all) { echo "该店铺无你可用优惠券" . PHP_EOL; exit; } $check = $obj->checkCoupon($all[0]);//检查是否可用优惠券 if ($check) { echo '随便使用一张优惠券没有问题' . PHP_EOL; } $res =$obj->dobuy(); var_dump($res); //场景1:商品详情页中,先筛选可用时间范围内的优惠券,进行优惠券的过滤 //如果不符合的直接过滤 //过滤之后需要判断券是否已经使用,或者是否领取完成 //coupon::with('coupon_user') 如果可领数量大于已领取数量,文案显示继续领取 // ``` ![](https://img.kancloud.cn/ce/b7/ceb7d73c6863d74e2576685c3316c37b_401x93.png) ## 也可以用组合模式 ``` // 抽象商品类 abstract class Product { abstract public function getPrice(); abstract public function getProducts(); } // 商品类 class Item extends Product { public $name; public $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function getPrice() { return $this->price; } public function getProducts() { return array($this); } } // 商品组合类 class Combo extends Product { public $name; public $products = array(); public function __construct($name) { $this->name = $name; } public function addProduct(Product $product) { $this->products[] = $product; } public function removeProduct(Product $product) { $key = array_search($product, $this->products, true); if ($key !== false) { unset($this->products[$key]); } } public function getPrice() { $totalPrice = 0; foreach ($this->products as $product) { $totalPrice += $product->getPrice(); } return $totalPrice; } public function getProducts() { return $this->products; } } // 购物车类 class ShoppingCart { private $products = array(); public function addProduct(Product $product) { $this->products[] = $product; } public function removeProduct(Product $product) { $key = array_search($product, $this->products, true); if ($key !== false) { unset($this->products[$key]); } } public function getTotalPrice() { $totalPrice = 0; foreach ($this->products as $product) { $totalPrice += $product->getPrice(); } return $totalPrice; } public function getProducts() { return $this->products; } } // 示例用法 $item1 = new Item('Product A', 10); $item2 = new Item('Product B', 20); $item3 = new Item('Product C', 30); $combo1 = new Combo('Combo 1'); $combo1->addProduct($item1); $combo1->addProduct($item2); $combo2 = new Combo('Combo 2'); $combo2->addProduct($item2); $combo2->addProduct($item3); $cart = new ShoppingCart(); $cart->addProduct($item1); $cart->addProduct($combo1); $cart->addProduct($combo2); $totalPrice = $cart->getTotalPrice(); echo "Total Price: $totalPrice\n"; echo "Shopping Cart Contents:\n"; foreach ($cart->getProducts() as $product) { echo "- " . get_class($product) . " " . $product->name . " (" . $product->getPrice() . ")\n"; if ($product instanceof Combo) { foreach ($product->getProducts() as $subProduct) { echo " - " . get_class($subProduct) . " " . $subProduct->name . " (" . $subProduct->getPrice() . ")\n"; } } } ``` ## **sql** ``` CREATE TABLE `coupon` ( `coupon_id` int PRIMARY KEY AUTO_INCREMENT COMMENT '优惠券id', `store_id` int NOT NULL COMMENT '商店id', `coupon_name` varchar(50) NOT NULL COMMENT '优惠券名称', `coupon_status` varchar(50) NOT NULL COMMENT '优惠券状态', `coupon_amount` varchar(20) NOT NULL DEFAULT 0 COMMENT '优惠金额', `coupon_type` varchar(20) NOT NULL DEFAULT 0 COMMENT '打折类型 打折券,满减券', `coupon_number` int NOT NULL DEFAULT 0 COMMENT '发放数量', `remainder_number` int NOT NULL DEFAULT 0 COMMENT '剩下优惠券的数量', `describe` varchar(500) NOT NULL DEFAULT "" COMMENT '描述优惠券', `create_time` int NOT NULL DEFAULT 0 COMMENT '创建时间', `update_time` int NOT NULL DEFAULT 0 COMMENT '更新时间', `use_time_type` varchar(20) NOT NULL DEFAULT "" COMMENT '使用时间类型', `begin_time` int NOT NULL DEFAULT 0 COMMENT '开始时间', `end_time` int NOT NULL DEFAULT 0 COMMENT '结束时间', `is_threshold` tinyint(1) NOT NULL DEFAULT 0 COMMENT '使用门槛', `threshold_amount` varchar(20) NOT NULL DEFAULT 0 COMMENT '门槛值', `member_limit_type` varchar(10) NOT NULL DEFAULT "" COMMENT '成员限制类型', `receive_limit_number` int NOT NULL DEFAULT 0 COMMENT '每人限领 0 表示不限领' ); CREATE TABLE `coupon_limit_members` ( `coupon_limit_members_id` int PRIMARY KEY AUTO_INCREMENT COMMENT 'id ', `member_id` int NOT NULL DEFAULT "0" COMMENT '成员id', `user_id` int NOT NULL DEFAULT "0" COMMENT '用户id', `store_id` int NOT NULL DEFAULT "0" COMMENT '商店id', `create_time` int NOT NULL DEFAULT 0 COMMENT '创建时间', `update_time` int NOT NULL DEFAULT 0 COMMENT '更新时间', `coupon_id` int NOT NULL DEFAULT "0" COMMENT '优惠券id' ); CREATE TABLE `coupon_user` ( `coupon_user_id` int PRIMARY KEY AUTO_INCREMENT, `coupon_id` int, `user_id` int, `coupon_user_status` varchar(20) NOT NULL DEFAULT "0" COMMENT '优惠券使用的状态' ); CREATE TABLE `coupon_goods` ( `coupon_goods_id` int PRIMARY KEY AUTO_INCREMENT, `goods_id` int NOT NULL DEFAULT 0 COMMENT '商品id', `spec_id` int NOT NULL DEFAULT 0 COMMENT '规格id', `coupon_id` int NOT NULL DEFAULT 0 COMMENT '优惠券id' ); ALTER TABLE `coupon` ADD FOREIGN KEY (`coupon_id`) REFERENCES `coupon_user` (`coupon_id`); ALTER TABLE `coupon` ADD FOREIGN KEY (`coupon_id`) REFERENCES `coupon_limit_members` (`coupon_id`); ALTER TABLE `coupon` ADD FOREIGN KEY (`coupon_id`) REFERENCES `coupon_goods` (`coupon_id`); ```