123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\model;
-
- use app\common\basics\Models;
- use app\common\enum\FreightEnum;
- use think\facade\Db;
-
-
- class Freight extends Models
- {
- protected $name = 'freight';
-
- protected $autoWriteTimestamp = true;
-
- public static function getChargeWay($type)
- {
- $data = [
- FreightEnum::CHARGE_WAY_WEIGHT => '按重量计费',
- FreightEnum::CHARGE_WAY_VOLUME => '按体积计费',
- FreightEnum::CHARGE_WAY_PIECE => '按件计费',
- ];
-
- if ($type === true) {
- return $data;
- }
-
- return $data[$type] ?? '未知';
- }
-
- public function getChargeWayTextAttr($value, $data)
- {
- return self::getChargeWay($data['charge_way']);
- }
-
- public static function getNameColumn($shop_id)
- {
- $lists = self::where(['shop_id'=>$shop_id])->column('id,name', 'id');
-
- return empty($lists) ? [] : $lists;
- }
-
- public function configs()
- {
- return $this->hasMany('freight_config', 'freight_id', 'id');
- }
-
-
-
- public function sumFreight($address, $freight_id, $nums)
- {
-
- $freight_config = FreightConfig::where('freight_id', $freight_id)->select()->toArray();
- $freight_config_item = [];
- foreach ($freight_config as $key => $value) {
- $district_check = strpos($value['region'], (string)$address['district_id']);
- $city_check = strpos($value['region'], (string)$address['city_id']);
- $province_check = strpos($value['region'], (string)$address['province_id']);
-
- if ($district_check !== false) {
- $freight_config_item = $value;
- }
- if ($city_check !== false) {
- $freight_config_item = $value;
- }
- if ($province_check !== false) {
- $freight_config_item = $value;
- }
- if ($district_check == false && $city_check == false && $province_check == false) {
- if ($value['region'] == 'all') {
- $freight_config_item = $value;
- }
- }
- }
-
- $price = 0;
-
- $unified_num = $nums;
-
- if ($unified_num <= $freight_config_item['first_unit']) {
- $price = $freight_config_item['first_money'];
- }
-
- if ($unified_num > $freight_config_item['first_unit']) {
- $continue_unit = ceil(($unified_num - $freight_config_item['first_unit']) / $freight_config_item['continue_unit']);
- $price = ($continue_unit * $freight_config_item['continue_money']) + $freight_config_item['first_money'];
- }
-
- return $price;
- }
- }
|