<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                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`); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看