Freight.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\model\FreightItems;
  5. /**
  6. * 模型
  7. */
  8. class Freight extends Model
  9. {
  10. // 表名
  11. protected $name = 'shop_freight';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. // 追加属性
  18. protected $append = [];
  19. public function getFirstNumAttr($value, $data)
  20. {
  21. return $data['first_num'] ?? $data['num'];
  22. }
  23. public function getFirstPriceAttr($value, $data)
  24. {
  25. return $data['first_price'] ?? $data['price'];
  26. }
  27. /**
  28. * @ DateTime 2021-05-28
  29. * @ 计算邮费
  30. * @param $freight_id
  31. * @param $area_id
  32. * @param $nums
  33. * @param $weight
  34. * @param $amount
  35. * @return int
  36. */
  37. public static function calculate($freight_id, $area_id, $nums, $weight, $amount)
  38. {
  39. //模板id
  40. $freight = self::where('id', $freight_id)->where('switch', 1)->find();
  41. $shippingfee = 0;
  42. if (empty($freight)) {
  43. return $shippingfee;
  44. }
  45. //当前模板 计费类型 ,1=计件 ,2=计重
  46. $FreightItems = new FreightItems();
  47. switch ($freight['type']) {
  48. case 1:
  49. $shippingfee = $FreightItems->numPostage($freight, $area_id, $nums, $amount);
  50. break;
  51. case 2:
  52. $shippingfee = $FreightItems->weightPostage($freight, $area_id, $nums, $weight, $amount);
  53. break;
  54. }
  55. return $shippingfee;
  56. }
  57. }