Express.php 9.3 KB

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