Express.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service\delivery;
  13. use app\common\enum\Setting as SettingEnum;
  14. use app\common\library\helper;
  15. use app\common\model\store\Setting as SettingModel;
  16. use app\common\model\Delivery as DeliveryModel;
  17. use app\common\enum\OrderType as OrderTypeEnum;
  18. use app\common\service\BaseService;
  19. /**
  20. * 快递配送服务类
  21. * Class Delivery
  22. * @package app\common\service
  23. */
  24. class Express extends BaseService
  25. {
  26. // 用户收货城市id
  27. private int $cityId;
  28. // 订单商品列表
  29. private iterable $goodsList;
  30. // 不在配送范围的商品ID
  31. private ?int $notInRuleGoodsId = null;
  32. // 运费模板数据集
  33. private array $data = [];
  34. /**
  35. * 构造方法
  36. * Express constructor.
  37. * @param int $cityId 城市ID
  38. * @param $goodsList
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function __construct(int $cityId, $goodsList)
  44. {
  45. parent::__construct();
  46. // 赋值传参
  47. $this->cityId = $cityId;
  48. $this->goodsList = $goodsList;
  49. // 整合运费模板
  50. $this->initDeliveryTemplate();
  51. }
  52. /**
  53. * 验证用户收货地址是否在配送范围
  54. * @return bool
  55. */
  56. public function isIntraRegion(): bool
  57. {
  58. if (!$this->cityId) return false;
  59. foreach ($this->data as $item) {
  60. $cityIds = [];
  61. foreach ($item['delivery']['rule'] as $ruleItem) {
  62. $cityIds = array_merge($cityIds, $ruleItem['region']);
  63. }
  64. if (!in_array($this->cityId, $cityIds)) {
  65. $this->notInRuleGoodsId = current($item['goodsList'])['goods_id'];
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * 获取不在配送范围的商品名称
  73. * @return null
  74. */
  75. public function getNotInRuleGoodsName()
  76. {
  77. $item = helper::getArrayItemByColumn($this->goodsList, 'goods_id', $this->notInRuleGoodsId);
  78. return !empty($item) ? $item['goods_name'] : null;
  79. }
  80. /**
  81. * 获取订单的配送费用
  82. * @return string
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function getDeliveryFee()
  88. {
  89. if (empty($this->cityId) || empty($this->goodsList) || $this->notInRuleGoodsId > 0) {
  90. return helper::number2(0.00);
  91. }
  92. // 处理商品包邮
  93. $this->freeshipping();
  94. // 计算配送金额
  95. foreach ($this->data as &$item) {
  96. // 计算当前配送模板的运费
  97. $item['delivery_fee'] = $this->calcDeliveryAmount($item);
  98. }
  99. // 根据运费组合策略获取最终运费金额
  100. return helper::number2($this->getFinalFreight());
  101. }
  102. /**
  103. * 根据运费组合策略 计算最终运费
  104. * @return float|int|mixed
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. private function getFinalFreight()
  110. {
  111. // 运费合集
  112. $expressPriceArr = helper::getArrayColumn($this->data, 'delivery_fee');
  113. // 最终运费金额
  114. $expressPrice = 0.00;
  115. // 判断运费组合策略
  116. switch (SettingModel::getItem('trade')['freight_rule']) {
  117. case '10': // 策略1: 叠加
  118. $expressPrice = array_sum($expressPriceArr);
  119. break;
  120. case '20': // 策略2: 以最低运费结算
  121. $expressPrice = min($expressPriceArr);
  122. break;
  123. case '30': // 策略3: 以最高运费结算
  124. $expressPrice = max($expressPriceArr);
  125. break;
  126. }
  127. return $expressPrice;
  128. }
  129. /**
  130. * 商品满额包邮
  131. * @return bool
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. private function freeshipping(): bool
  137. {
  138. // 订单商品总金额
  139. $orderTotalPrice = helper::getArrayColumnSum($this->goodsList, 'total_price');
  140. // 获取满额包邮设置
  141. $options = SettingModel::getItem(SettingEnum::FULL_FREE);
  142. foreach ($this->data as &$item) {
  143. $item['free_goods_list'] = [];
  144. foreach ($item['goodsList'] as $goodsItem) {
  145. if (
  146. $options['is_open'] == true
  147. && $orderTotalPrice >= $options['money']
  148. && !in_array($goodsItem['goods_id'], $options['excludedGoodsIds'])
  149. && !in_array($this->cityId, $options['excludedRegions']['cityIds'])
  150. ) {
  151. $item['free_goods_list'][] = $goodsItem['goods_id'];
  152. }
  153. }
  154. }
  155. return true;
  156. }
  157. /**
  158. * 计算当前配送模板的运费
  159. * @param $item
  160. * @return float|mixed|string
  161. */
  162. private function calcDeliveryAmount($item)
  163. {
  164. // 获取运费模板下商品总数量or总重量
  165. if (!$totality = $this->getItemGoodsTotal($item)) {
  166. return 0.00;
  167. }
  168. // 当前收货城市配送规则
  169. $deliveryRule = $this->getCityDeliveryRule($item['delivery']);
  170. if ($totality <= $deliveryRule['first']) {
  171. return $deliveryRule['first_fee'];
  172. }
  173. // 续件or续重 数量
  174. $additional = $totality - $deliveryRule['first'];
  175. if ($additional <= $deliveryRule['additional']) {
  176. return helper::bcadd($deliveryRule['first_fee'], $deliveryRule['additional_fee']);
  177. }
  178. // 计算续重/件金额
  179. if ($deliveryRule['additional'] < 1) {
  180. // 配送规则中续件为0
  181. $additionalFee = 0.00;
  182. } else {
  183. $additionalFee = helper::bcdiv($deliveryRule['additional_fee'], $deliveryRule['additional']) * $additional;
  184. }
  185. return helper::bcadd($deliveryRule['first_fee'], $additionalFee);
  186. }
  187. /**
  188. * 获取运费模板下商品总数量or总重量
  189. * @param $item
  190. * @return int|string
  191. */
  192. private function getItemGoodsTotal($item)
  193. {
  194. $totalWeight = 0; // 总重量
  195. $totalNum = 0; // 总数量
  196. foreach ($item['goodsList'] as $goodsItem) {
  197. // 如果商品为包邮,则不计算总量中
  198. if (!in_array($goodsItem['goods_id'], $item['free_goods_list'])) {
  199. $goodsWeight = helper::bcmul($goodsItem['skuInfo']['goods_weight'], $goodsItem['total_num']);
  200. $totalWeight = helper::bcadd($totalWeight, $goodsWeight);
  201. $totalNum = helper::bcadd($totalNum, $goodsItem['total_num']);
  202. }
  203. }
  204. return $item['delivery']['method'] == 10 ? $totalNum : $totalWeight;
  205. }
  206. /**
  207. * 根据城市id获取规则信息
  208. * @param
  209. * @return array|false
  210. */
  211. private function getCityDeliveryRule($delivery)
  212. {
  213. foreach ($delivery['rule'] as $item) {
  214. if (in_array($this->cityId, $item['region'])) {
  215. return $item;
  216. }
  217. }
  218. return false;
  219. }
  220. /**
  221. * 整合运费模板
  222. * @return bool
  223. * @throws \think\db\exception\DataNotFoundException
  224. * @throws \think\db\exception\DbException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. */
  227. private function initDeliveryTemplate()
  228. {
  229. // 运费模板ID集
  230. $deliveryIds = helper::getArrayColumn($this->goodsList, 'delivery_id');
  231. // 运费模板列表
  232. $deliveryList = (new DeliveryModel)->getListByIds(array_values(array_unique($deliveryIds)));
  233. // 整理数据集
  234. foreach ($deliveryList as $item) {
  235. $this->data[$item['delivery_id']]['delivery'] = $item;
  236. $this->data[$item['delivery_id']]['goodsList'] = $this->getGoodsListByDeliveryId($item['delivery_id']);
  237. }
  238. return true;
  239. }
  240. /**
  241. * 根据运费模板id整理商品集
  242. * @param $deliveryId
  243. * @return array
  244. */
  245. private function getGoodsListByDeliveryId($deliveryId)
  246. {
  247. $data = [];
  248. foreach ($this->goodsList as $item) {
  249. $item['delivery_id'] == $deliveryId && $data[] = $item;
  250. }
  251. return $data;
  252. }
  253. }