Distribution.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use mall\utils\BC;
  11. use think\facade\Db;
  12. class Distribution {
  13. private static $_delivery = [];
  14. public static function get(&$data,$address=[]){
  15. if(empty($address)){
  16. return $data;
  17. }
  18. foreach($data["item"] as $key=>$value){
  19. $delivery = self::compute($value["distribution_id"],$address["province"],$value["goods_weight"] * $value["goods_nums"]);
  20. $data["item"][$key]["delivery"] = $delivery["delivery"];
  21. $data["real_freight"] = BC::add($delivery["real_freight"],$data["real_freight"]);
  22. $data["payable_freight"] = BC::add($delivery["payable_freight"],$data["payable_freight"]);
  23. }
  24. $data = Promotion::run($data);
  25. $data["payable_amount"] = BC::add($data["real_freight"],$data["real_amount"]);
  26. $data["order_amount"] = $data["payable_amount"];
  27. return $data;
  28. }
  29. private static function compute($delivery_id, $province, $weight){
  30. $data = [
  31. "real_freight" => 0, // 实付运费
  32. "payable_freight" => 0 // 总运费金额
  33. ];
  34. $delivery = Db::name("distribution")->where(["status" => 0, "id" => $delivery_id])->find();
  35. self::$_delivery = $delivery;
  36. if (empty($delivery)) {
  37. throw new \Exception("配送方式不存在", 0);
  38. }
  39. if ($delivery["type"] == 0) {
  40. $delivery["weight_price"] = self::calculate($weight, $delivery["first_price"], $delivery["second_price"]);
  41. }else {
  42. $index = 0;
  43. $flag = false;
  44. $area_groupid = json_decode($delivery["area_group"], true);
  45. foreach ($area_groupid as $key => $val) {
  46. $arr = explode(",", $val);
  47. //foreach ($province as $area_id) {
  48. if (in_array($province, $arr)) {
  49. $index = $key;
  50. $flag = true;
  51. break;
  52. }
  53. //}
  54. if ($flag) {
  55. break;
  56. }
  57. }
  58. if ($flag) {
  59. // 如果查找到所设置的省份
  60. $firstprice = json_decode($delivery["first_price_group"], true);
  61. $secondprice = json_decode($delivery["second_price_group"], true);
  62. $delivery["weight_price"] = self::calculate($weight, $firstprice[$index], $secondprice[$index]);
  63. } else {
  64. //如果该地区未设置,使用默认费用
  65. $delivery["weight_price"] = self::calculate($weight, $delivery["first_price"], $delivery["second_price"]);
  66. }
  67. }
  68. $data["real_freight"] = $delivery["weight_price"];
  69. $data["payable_freight"] = $delivery["weight_price"];
  70. $data["delivery"] = ["title" => $delivery["title"], "description" => isset($delivery["description"]) ? $delivery["description"] : ""];
  71. return $data;
  72. }
  73. /*
  74. * 根据重量计算给定价格
  75. */
  76. private static function calculate($weight, $first_price, $second_price) {
  77. $first_weight = self::$_delivery["first_weight"];
  78. $second_weight = self::$_delivery["second_weight"];
  79. //当商品重量小于或等于首重的时候
  80. if ($weight <= $first_weight) {
  81. return $first_price;
  82. }
  83. //当商品重量大于首重时,根据次重进行累加计算
  84. $num = ceil(($weight - $first_weight) / $second_weight);
  85. return $first_price + $second_price * $num;
  86. }
  87. }