From 41e8fbf13b2ed595d8c1c67746588d17144f96d4 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Mon, 13 Dec 2021 11:22:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=90=E8=B4=B9=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/HomeController.php | 4 ++ app/Models/ShippingRule.php | 3 + app/Services/ShippingService.php | 72 +++++++++++++++++++ app/Traits/SkuInfo.php | 8 +-- ..._08_112027_create_shipping_rules_table.php | 2 +- 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 app/Services/ShippingService.php diff --git a/app/Admin/Controllers/HomeController.php b/app/Admin/Controllers/HomeController.php index 6f9d71c9..922e1481 100644 --- a/app/Admin/Controllers/HomeController.php +++ b/app/Admin/Controllers/HomeController.php @@ -3,12 +3,16 @@ namespace App\Admin\Controllers; use App\Http\Controllers\Controller; +use App\Services\ShippingService; use Dcat\Admin\Layout\Content; class HomeController extends Controller { public function index(Content $content) { + $shippingService = new ShippingService(); + $amount = $shippingService->countShippingAmount(3560, 2000, 1, 2225); + dd($amount); return $content ->header('Dashboard') ->description('Description...'); diff --git a/app/Models/ShippingRule.php b/app/Models/ShippingRule.php index 12213d36..00551b3e 100644 --- a/app/Models/ShippingRule.php +++ b/app/Models/ShippingRule.php @@ -12,6 +12,9 @@ class ShippingRule extends Model use HasFactory; use HasDateTimeFormatter; + public const TYPE_FREE = 1;//包邮 + public const TYPE_WEIGHT = 2;//计算重量 + // protected $casts = [ // 'info' => JsonArray::class, // ]; diff --git a/app/Services/ShippingService.php b/app/Services/ShippingService.php new file mode 100644 index 00000000..c6826795 --- /dev/null +++ b/app/Services/ShippingService.php @@ -0,0 +1,72 @@ +where('template_id', $templateId)->get(); + $canShipping = true;//是否支持寄送 + //如果该模板下无运费规则,则直接包邮 + if ($rules->count() > 0) { + //按包邮/计重 + $rules = $rules->sortBy('type'); + foreach ($rules as $rule) { + $canShipping = false; + $_ruleInfo = json_decode($rule->info, true); + + if (in_array($zoneId, $rule->zones->pluck('id')->toArray())) { + $canShipping = true; + if ($rule->type == ShippingRule::TYPE_FREE) { + if ($totalAmount >= bcmul($_ruleInfo['threshold'], 100)) {//在包邮价格范围内 + break;//跳出foreach循环, 直接包邮 + } + } elseif ($rule->type == ShippingRule::TYPE_WEIGHT) { + $_firstWeight = (int) bcmul($_ruleInfo['first_weight'], 1000); + $_firstWamount = (int) bcmul($_ruleInfo['first_w_amount'], 100); + $_contiueWeight = (int) bcmul($_ruleInfo['continue_weight'], 1000); + $_contiueWamount = (int) bcmul($_ruleInfo['continue_w_amount'], 100); + if ($weight <= $_firstWeight) { + $shipping_amount = $_firstWamount; + break; + } else {//计算续重价格 + $num = (int) bcdiv(($weight - $_firstWeight), $_contiueWeight); + $shipping_amount = (int) bcadd($_firstWamount, bcmul($num+1, $_contiueWamount)); + break; + } + } + } + } + } + if (!$canShipping) { + throw new BizException('当前选择的地址有部分商品不支持配送'); + } + + return $this->returnAmount($shipping_amount); + } + + /** + * 统一返回运费格式 + * + * @param [type] $shipping_amount + * @return void + */ + protected function returnAmount($shipping_amount) + { + return $shipping_amount; + // return bcdiv($shipping_amount, 100, 2); + } +} diff --git a/app/Traits/SkuInfo.php b/app/Traits/SkuInfo.php index f4fb0944..8ba0548b 100644 --- a/app/Traits/SkuInfo.php +++ b/app/Traits/SkuInfo.php @@ -29,7 +29,7 @@ trait SkuInfo */ protected static function createSellPrice(float $price, float $specPrice) { - return bcadd($price, $specPrice); + return bcadd($price, $specPrice, 2); } /** @@ -41,7 +41,7 @@ trait SkuInfo */ protected static function createMarketPrice(float $price, float $specPrice) { - return bcadd($price, 0); + return bcadd($price, 0, 2); } /** @@ -53,7 +53,7 @@ trait SkuInfo */ protected static function createCostPrice(float $price, float $specPrice) { - return bcadd($price, 0); + return bcadd($price, 0, 2); } /** @@ -65,7 +65,7 @@ trait SkuInfo */ protected static function createVipPrice(?float $price, float $specPrice) { - return !is_null($price) ? bcadd($price, $specPrice) : null; + return !is_null($price) ? bcadd($price, $specPrice, 2) : null; } /** diff --git a/database/migrations/2021_12_08_112027_create_shipping_rules_table.php b/database/migrations/2021_12_08_112027_create_shipping_rules_table.php index b29a816a..a017821e 100644 --- a/database/migrations/2021_12_08_112027_create_shipping_rules_table.php +++ b/database/migrations/2021_12_08_112027_create_shipping_rules_table.php @@ -16,7 +16,7 @@ class CreateShippingRulesTable extends Migration Schema::create('shipping_rules', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('template_id')->comment('模板ID'); - $table->unsignedTinyInteger('type')->default(0)->comment('规则类型:0包邮,1计重'); + $table->unsignedTinyInteger('type')->default(0)->comment('规则类型:1包邮,2计重'); $table->json('info')->nullable()->comment('规则内容'); $table->string('remarks')->nullable()->comment('规则备注'); $table->timestamps();