You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

56 lines
1.4 KiB

  1. <?php
  2. namespace addons\unishop\model;
  3. use think\Model;
  4. class Delivery extends Model
  5. {
  6. // 表名
  7. protected $name = 'unishop_delivery';
  8. const TABLE_NAME = 'unishop_delivery';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 是否上架
  15. const SWITCH_YES = 1;
  16. const SWITCH_NO = 0;
  17. /**
  18. * 关联配送模板区域及运费
  19. * @return \think\model\relation\HasMany
  20. */
  21. public function rule()
  22. {
  23. return $this->hasMany('DeliveryRule','delivery_id','id');
  24. }
  25. /**
  26. * 运费算法
  27. * @param Delivery $delivery
  28. * @param int $number
  29. * @return int
  30. */
  31. public static function algorithm($delivery, $number) {
  32. $deliveryPrice = 0;
  33. $delivery['first'] = $delivery['first'] == 0 ? 1 : $delivery['first'];
  34. $delivery['additional'] = $delivery['additional'] == 0 ? 1 : $delivery['additional'];
  35. for ($i = 0; $i < $number; ) {
  36. if ($i === 0) {
  37. $deliveryPrice = bcadd($delivery['first_fee'], $deliveryPrice, 2);
  38. $i += $delivery['first'];
  39. } else {
  40. $deliveryPrice = bcadd($delivery['additional_fee'], $deliveryPrice, 2);
  41. $i += $delivery['additional'];
  42. }
  43. }
  44. return $deliveryPrice;
  45. }
  46. }