LocalConfigService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace addons\TinyShop\services\common;
  3. use yii\web\UnprocessableEntityHttpException;
  4. use common\helpers\ArrayHelper;
  5. use addons\TinyShop\common\models\common\LocalConfig;
  6. /**
  7. * Class LocalConfigService
  8. * @package addons\TinyShop\services\common
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class LocalConfigService
  12. {
  13. /**
  14. * 获取配送费
  15. *
  16. * @param $merchant_id
  17. * @return int
  18. */
  19. public function getShippingFeeByMerchantId($merchant_id, $order_money = 0)
  20. {
  21. if (empty($model = $this->findByMerchantId($merchant_id)) || empty($model->shipping_fee)) {
  22. return 0;
  23. }
  24. if ($order_money < $model->order_money) {
  25. throw new UnprocessableEntityHttpException('最低配送金额为:' . $model->order_money);
  26. }
  27. // 运费
  28. $model->shipping_fee = ArrayHelper::arraySort($model->shipping_fee, 'order_money', 'desc');
  29. foreach ($model->shipping_fee as $item) {
  30. if ($order_money > $item['order_money']) {
  31. return $item['freight'];
  32. }
  33. }
  34. return $model->freight;
  35. }
  36. /**
  37. * @param $merchant_id
  38. * @return array|\yii\db\ActiveRecord|null
  39. */
  40. public function findByMerchantId($merchant_id = 0)
  41. {
  42. return LocalConfig::find()
  43. ->where(['merchant_id' => $merchant_id])
  44. ->one();
  45. }
  46. }
粤ICP备19079148号