Express.php 9.0 KB

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