StoreDeliveryOrderServices.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreDeliveryOrderDao;
  13. use app\jobs\order\OrderTakeJob;
  14. use app\jobs\store\StoreFinanceJob;
  15. use app\services\BaseServices;
  16. use app\services\other\CityAreaServices;
  17. use app\services\store\SystemStoreServices;
  18. use app\services\supplier\SystemSupplierServices;
  19. use crmeb\services\DeliverySevices;
  20. use crmeb\services\FormBuilder as Form;
  21. use crmeb\services\SystemConfigService;
  22. use crmeb\traits\OptionTrait;
  23. use think\exception\ValidateException;
  24. use think\facade\Log;
  25. use think\facade\Route as Url;
  26. /**
  27. * 发货单
  28. * Class StoreDeliveryOrderServices
  29. * @package app\services\order
  30. * @mixin StoreDeliveryOrderDao
  31. */
  32. class StoreDeliveryOrderServices extends BaseServices
  33. {
  34. use OptionTrait;
  35. protected $statusData = [
  36. 2 => '待取货',
  37. 3 => '配送中',
  38. 4 => '已完成',
  39. -1 => '已取消',
  40. 9 => '物品返回中',
  41. 10 => '物品返回完成',
  42. 100 => '骑士到店',
  43. ];
  44. /**
  45. * 平台达达门店
  46. * @var string
  47. */
  48. public $platCityShopId = 'plat_delivery_city_shop_001';
  49. /**
  50. * 构造方法
  51. * StoreDeliveryOrderServices constructor.
  52. * @param StoreDeliveryOrderDao $dao
  53. */
  54. public function __construct(StoreDeliveryOrderDao $dao)
  55. {
  56. $this->dao = $dao;
  57. }
  58. /**
  59. * 配送信息
  60. * @return string[]
  61. */
  62. public function getStatusMsg()
  63. {
  64. return $this->statusData;
  65. }
  66. /**
  67. * @param array $where
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function systemPage(array $where)
  74. {
  75. [$page, $limit] = $this->getPageValue();
  76. $list = $this->dao->getList($where, '*', $page, $limit, ['orderInfo', 'storeInfo']);
  77. $count = $this->dao->count($where);
  78. if ($list) {
  79. foreach ($list as &$item) {
  80. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s') : '';
  81. $item['distance'] = $item['distance'] ? bcdiv($item['distance'], '1000', 3) : 0;
  82. }
  83. }
  84. return compact('count', 'list');
  85. }
  86. /**
  87. * 生成订单号
  88. * @return string
  89. * @throws \Exception
  90. */
  91. public function getOrderSn(string $key = '')
  92. {
  93. [$msec, $sec] = explode(' ', microtime());
  94. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  95. $orderId = $key . $msectime . random_int(10000, max(intval($msec * 10000) + 10000, 98369));
  96. return $orderId;
  97. }
  98. /**
  99. * 地址转经纬度
  100. * @param $region
  101. * @param $address
  102. * @return mixed|null
  103. */
  104. public function lbs_address($region, $address)
  105. {
  106. $key = sys_config('tengxun_map_key', '');
  107. if (!$key) {
  108. throw new ValidateException('请先配置地图KEY');
  109. }
  110. $locationOption = new \Joypack\Tencent\Map\Bundle\AddressOption($key);
  111. $locationOption->setAddress($address);
  112. $locationOption->setRegion($region);
  113. $location = new \Joypack\Tencent\Map\Bundle\Address($locationOption);
  114. $res = $location->request();
  115. if ($res->error) {
  116. throw new ValidateException($res->error);
  117. }
  118. if ($res->status) {
  119. throw new ValidateException($res->message);
  120. }
  121. if (!$res->result) {
  122. throw new ValidateException('获取失败');
  123. }
  124. return $res->result;
  125. }
  126. /**
  127. * 处理订单数据
  128. * @param array $station
  129. * @param array $order
  130. * @param int $type
  131. * @return array
  132. */
  133. public function getPriceParams(array $station, array $order, int $type)
  134. {
  135. $data = [];
  136. $type = (int)$type;
  137. switch ($type) {
  138. case 1:
  139. $city = DeliverySevices::init(DeliverySevices::DELIVERY_TYPE_DADA)->getCity([]);
  140. $res = [];
  141. foreach ($city as $item) {
  142. $res[$item['label']] = $item['key'];
  143. }
  144. //达达城市数据:西安
  145. $city_name = str_replace(['市','自治州','地区','区划','县'], '',$station['city_name'] ?? '');
  146. $data = [
  147. 'shop_no' => $station['origin_shop_id'],
  148. 'city_code' => $res[$city_name] ?? '西安',
  149. 'cargo_price' => $order['pay_price'],
  150. 'is_prepay' => 0,
  151. 'receiver_name' => $order['real_name'],
  152. 'receiver_address' => $order['user_address'],
  153. 'cargo_weight' => 0,
  154. 'receiver_phone' => $order['user_phone'],
  155. 'is_finish_code_needed' => 1,
  156. ];
  157. break;
  158. case 2://uu城市数据:西安市
  159. $business = DeliverySevices::init(DeliverySevices::DELIVERY_TYPE_UU)->getBusiness();
  160. $business = array_combine(array_column($business, 'key'), $business);
  161. $data = [
  162. 'from_address' => $station['station_address'],
  163. 'to_address' => $order['user_address'],
  164. 'city_name' => $station['city_name'] ?? '西安',
  165. 'goods_type' => $business[$station['business'] ?? 1]['label'],
  166. 'send_type' =>'0',
  167. 'to_lat' => $order['latitude'],
  168. 'to_lng' => $order['longitude'],
  169. 'from_lat' => $station['lat'],
  170. 'from_lng' => $station['lng'],
  171. ];
  172. break;
  173. }
  174. return $data;
  175. }
  176. /**
  177. * 创建配送单
  178. * @param int $id
  179. * @param array $data
  180. * @param int $type
  181. * @param $order
  182. * @return bool
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function create(int $id, array $data, int $type = 1, $order = [])
  188. {
  189. if (!$order) {
  190. /** @var StoreOrderServices $orderServices */
  191. $orderServices = app()->make(StoreOrderServices::class);
  192. $order = $orderServices->get($id);
  193. }
  194. if (!$order) {
  195. throw new ValidateException('订单信息获取失败');
  196. }
  197. if (isset($order['user_address']) && !$order['user_address']) {
  198. throw new ValidateException('自提订单不支持同城配送');
  199. }
  200. $order = is_object($order) ? $order->toArray() : $order;
  201. //处理地址定位
  202. if (isset($order['user_location']) && $order['user_location']) {
  203. [$longitude, $latitude] = explode(' ', $order['user_location']);
  204. $order['longitude'] = $longitude;
  205. $order['latitude'] = $latitude;
  206. if (!$order['longitude'] || !$order['latitude']) {
  207. $addressArr = $this->addressHandle($order['user_address']);
  208. $city_name = $addressArr['city'] ?? '';
  209. try {
  210. $addres = $this->lbs_address($city_name, $order['user_address']);
  211. $order['latitude'] = $addres['location']['lat'] ?? '';
  212. $order['longitude'] = $addres['location']['lng'] ?? '';
  213. } catch (\Exception $e) {
  214. throw new ValidateException('获取经纬度失败');
  215. }
  216. }
  217. }
  218. if ($order['store_id']) {//门店
  219. $resType = 1;
  220. $relationId = (int)$order['store_id'];
  221. } elseif ($order['supplier_id']) {//供应商
  222. $resType = 2;
  223. $relationId = (int)$order['supplier_id'];
  224. } else {//平台信息
  225. $resType = 0;
  226. $relationId = 0;
  227. }
  228. //获取发货信息
  229. $station = $this->syncCityShop($relationId, $resType, $type);
  230. if (!$station || !isset($station['lat']) || !$station['lat'] || !isset($station['lng']) || !$station['lng']) {
  231. throw new ValidateException('获取发货信息失败');
  232. }
  233. $getPriceParams = $this->getPriceParams($station, $order, $type);
  234. $orderSn = $this->getOrderSn($type == 1 ? 'dd' : 'uu');
  235. $getPriceParams['origin_id'] = $orderSn;
  236. $getPriceParams['cargo_weight'] = $data['cargo_weight'] ?? '';
  237. $service = DeliverySevices::init($type);
  238. try {
  239. //计算价格
  240. $priceData = $service->getOrderPrice($getPriceParams);
  241. if ($type == DeliverySevices::DELIVERY_TYPE_UU) { //uu
  242. $priceData['receiver'] = $order['real_name'];
  243. $priceData['receiver_phone'] = $order['user_phone'];
  244. $priceData['note'] = $data['delivery_remark'];
  245. $priceData['push_type'] = 2;
  246. $priceData['special_type'] = $data['special_type'] ?? 0;
  247. }
  248. $res = $service->addOrder($priceData);
  249. $ret = [
  250. 'type' => $resType,
  251. 'relation_id' => $relationId,
  252. 'oid' => $id,
  253. 'order_id' => $orderSn,
  254. 'delivery_no' => $type == 2 ? $res['ordercode'] : $priceData['deliveryNo'] ?? '',
  255. 'city_code' => $station['city_name'] ?? '西安',
  256. 'receiver_phone' => $order['user_phone'],
  257. 'user_name' => $order['real_name'],
  258. 'from_address' => $station['station_address'] ?? '',
  259. 'to_address' => $order['user_address'],
  260. 'info' => $data['delivery_remark'],
  261. 'status' => $res['status'] ?? 0,
  262. 'station_type' => $type,
  263. 'to_lat' => $order['latitude'],
  264. 'to_lng' => $order['longitude'],
  265. 'from_lat' => $station['lat'] ?? '',
  266. 'from_lng' => $station['lng'] ?? '',
  267. 'distance' => $priceData['distance'],
  268. 'fee' => $priceData['fee'] ?? $priceData['need_paymoney'] ?? 0,
  269. 'mark' => $data['delivery_remark'],
  270. 'uid' => $order['uid'],
  271. 'add_time' => time()
  272. ];
  273. //入库操作
  274. $this->dao->save($ret);
  275. //记录门店流水
  276. if ($resType == 1) {
  277. StoreFinanceJob::dispatch([$ret, 6, $ret['fee']]);
  278. }
  279. return true;
  280. } catch (\Throwable $e) {
  281. if (isset($res['status']) && $res['status'] == 'success'){
  282. $error['origin_id'] = $orderSn;
  283. $error['reason'] = $type == 1 ? 36 : '信息错误';
  284. $error['delivery_no'] = $type == 2 ? $res['ordercode'] : $priceData['deliveryNo'];
  285. $service->cancelOrder($error);
  286. }
  287. throw new ValidateException($e->getMessage());
  288. }
  289. }
  290. /**
  291. * 配送订单详情
  292. * @param int $id
  293. * @return array|\think\Model
  294. * @throws \think\db\exception\DataNotFoundException
  295. * @throws \think\db\exception\DbException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. */
  298. public function detail(int $id)
  299. {
  300. $res = $this->dao->get($id, ['orderInfo']);
  301. if (!$res) {
  302. throw new ValidateException('配送订单不存在,或已取消');
  303. }
  304. $res = $res->toArray();
  305. $order = DeliverySevices::init((int)$res['station_type'])->getOrderDetail($res);
  306. if (!$res) throw new ValidateException('订单不存在');
  307. $res['data'] = [
  308. 'order_id' => $order['order_code'],
  309. 'to_address' => $order['to_address'],
  310. 'from_address' => $order['from_address'],
  311. 'state' => $order['state'],
  312. 'note' => $order['note'],
  313. 'order_price' => $order['order_price'],
  314. 'distance' => round(($order['distance'] / 1000),2) . ' km',
  315. ];
  316. return $res;
  317. }
  318. /**
  319. * 删除
  320. * @param int $id
  321. * @return mixed
  322. */
  323. public function delete(int $id)
  324. {
  325. $res = $this->dao->get($id);
  326. if (!$res) throw new ValidateException('订单不存在');
  327. return $this->dao->delete($id);
  328. }
  329. /**
  330. * @param $id
  331. * @return mixed
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\DbException
  334. * @throws \think\db\exception\ModelNotFoundException
  335. */
  336. public function cancelForm($id)
  337. {
  338. $order = $this->dao->get($id);
  339. if (!$order) throw new ValidateException('订单不存在');
  340. if ($order['status'] == -1) throw new ValidateException('订单已取消,无法操作');
  341. $field = [];
  342. if ($order['station_type'] == 1) {
  343. $options = DeliverySevices::init(1)->reasons();
  344. $field[] = Form::select('reason', '取消原因')->setOptions(Form::setOptions($options))->filterable(1)->col(12);
  345. $field[] = Form::input('cancel_reason', '其他原因说明')->required('请输入原因');
  346. } else {
  347. $field[] = Form::input('reason', '取消原因')->required('请输入原因');
  348. }
  349. return create_form('取消同城配送订单', $field, Url::buildUrl('/order/delivery_order/cancel/'. $id), 'POST');
  350. }
  351. /**
  352. * 取消订单
  353. * @param int $id
  354. * @param array $reason
  355. * @return mixed
  356. * @throws \think\db\exception\DataNotFoundException
  357. * @throws \think\db\exception\DbException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. */
  360. public function cancel(int $id, array $reason)
  361. {
  362. $order = $this->dao->get($id);
  363. if (!$order) throw new ValidateException('配送订单不存在');
  364. if ($order['status'] == -1) throw new ValidateException('请勿重复操作');
  365. $data = [
  366. 'origin_id' => $order['order_id'],
  367. 'order_code'=> $order['delivery_no'],
  368. 'reason' => $reason['reason'],
  369. 'cancel_reason' => $reason['cancel_reason'],
  370. ];
  371. $this->transaction(function () use($id, $order, $data){
  372. $mark = $data['reason'];
  373. $delivery = DeliverySevices::init((int)$order['station_type']);
  374. if ($order['station_type'] == DeliverySevices::DELIVERY_TYPE_DADA) {
  375. $options = $delivery->reasons();
  376. if ($options) $options = array_combine(array_column($options, 'value'), $options);
  377. $mark = $options[$data['reason']]['label'] ?? '';
  378. }
  379. if ($data['cancel_reason']) $mark .= ','.$data['cancel_reason'];
  380. $res = $delivery->cancelOrder($data);
  381. $deduct_fee = $res['deduct_fee'] ?? 0;
  382. $this->doCancel($id, $order, $deduct_fee, $mark);
  383. });
  384. return true;
  385. }
  386. /**
  387. * 执行取消订单
  388. * @param int $id
  389. * @param $deliveryOrder
  390. * @param $deduct_fee
  391. * @param $mark
  392. * @return bool
  393. */
  394. public function doCancel(int $id , $deliveryOrder, $deduct_fee, $mark)
  395. {
  396. //修改配送单
  397. $this->dao->update($id, ['status' => 1, 'mark' => $mark, 'deduct_fee' => $deduct_fee]);
  398. //修改愿订单
  399. /** @var StoreOrderServices $storeOrderServices */
  400. $storeOrderServices = app()->make(StoreOrderServices::class);
  401. $storeOrderServices->update($deliveryOrder['oid'], ['status' => 0, 'delivery_type' => '', 'delivery_name' => '', 'delivery_id' => '', 'delivery_uid' => '']);
  402. /** @var StoreOrderStatusServices $statusServices */
  403. $statusServices = app()->make(StoreOrderStatusServices::class);
  404. $statusServices->save([
  405. 'oid' => $deliveryOrder['oid'],
  406. 'change_type' => 'city_delivery_cancel',
  407. 'change_message' => '同城配送取消',
  408. 'change_time' => time()
  409. ]);
  410. //记录门店流水
  411. if ($deliveryOrder['type'] == 1) {
  412. StoreFinanceJob::dispatch([$deliveryOrder, 7, $deliveryOrder['fee']]);
  413. }
  414. return true;
  415. }
  416. /**
  417. * TODO 回调
  418. * @param $data
  419. * @author Qinii
  420. * @day 2/17/22
  421. */
  422. public function notify($data)
  423. {
  424. //达达
  425. /**
  426. * 订单状态(待接单=1,待取货=2,配送中=3,已完成=4,已取消=5, 指派单=8,妥投异常之物品返回中=9, 妥投异常之物品返回完成=10, 骑士到店=100,创建达达运单失败=1000 可参考文末的状态说明)
  427. */
  428. Log::info('同城回调参数:'.var_export(['=======',$data,'======='],1));
  429. if (isset($data['data'])) {
  430. $data = json_decode($data['data'], 1);
  431. }
  432. $reason = '';
  433. $deductFee = 0;
  434. $delivery = [];
  435. if (isset($data['order_status'])){
  436. $order_id = $data['order_id'];
  437. if ($data['order_status'] == 1) {
  438. $orderData = $this->dao->getOne(['order_id' => $data['order_id']]);
  439. if (!$orderData['finish_code']) {
  440. $orderData->finish_code = $data['finish_code'];
  441. $orderData->save();
  442. }
  443. return ;
  444. } else if (in_array( $data['order_status'],[2,3,4,5,9,10,100])){
  445. $status = $data['order_status'];
  446. if ($data['order_status'] == 5){
  447. $msg = [
  448. '取消:',
  449. '达达配送员取消:',
  450. '商家主动取消:',
  451. '系统或客服取消:',
  452. ];
  453. //1:达达配送员取消;2:商家主动取消;3:系统或客服取消;0:默认值
  454. $status = -1;
  455. $reason = $msg[$data['cancel_from']].$data['cancel_reason'];
  456. }
  457. $deductFee = $data['deductFee'] ?? 0;
  458. if (isset($data['dm_name']) && $data['dm_name']) {
  459. $delivery = [
  460. 'delivery_name' => $data['dm_name'],
  461. 'delivery_id' => $data['dm_mobile'],
  462. ];
  463. }
  464. }
  465. } else if (isset($data['state'])){ //uu
  466. if (!$data['origin_id']) $deliveryOrder = $this->dao->getOne(['delivery_no' => $data['order_code']]);
  467. $order_id = $data['origin_id'] ?: $deliveryOrder['order_id'] ;
  468. //当前状态 1下单成功 3跑男抢单 4已到达 5已取件 6到达目的地 10收件人已收货 -1订单取消
  469. switch ($data['state']) {
  470. case 3:
  471. $status = 2;
  472. break;
  473. case 4:
  474. $status = 100;
  475. break;
  476. case 5:
  477. $status = 3;
  478. break;
  479. case 10:
  480. $status = 4;
  481. break;
  482. case -1:
  483. $status = -1;
  484. $reason = $data['state_text'];
  485. break;
  486. default:
  487. break;
  488. }
  489. if (isset($data['driver_name']) && $data['driver_name']) {
  490. $delivery = [
  491. 'delivery_name' => $data['driver_name'],
  492. 'delivery_id' => $data['driver_mobile'],
  493. ];
  494. }
  495. }
  496. if (isset($order_id) && isset($status)){
  497. $deliveryOrder = $this->dao->getOne(['order_id' => $order_id]);
  498. if ($deliveryOrder) {
  499. $this->notifyAfter($status, $reason, $deliveryOrder, $delivery, $deductFee);
  500. }else {
  501. Log::info('同城配送回调,未查询到订单:'.$order_id);
  502. }
  503. }
  504. }
  505. /**
  506. * @param $status
  507. * @param $reason
  508. * @param $res
  509. * @param $data
  510. * @param $deductFee
  511. * @return bool
  512. */
  513. public function notifyAfter($status, $reason, $deliveryOrder, $data, $deductFee)
  514. {
  515. if (!isset($this->statusData[$status])) return true;
  516. /** @var StoreOrderServices $orderServices */
  517. $orderServices = app()->make(StoreOrderServices::class);
  518. $orderData = $orderServices->get($deliveryOrder['oid']);
  519. /** @var StoreOrderStatusServices $statusServices */
  520. $statusServices = app()->make(StoreOrderStatusServices::class);
  521. $change_type = 'city_delivery_' . $status;
  522. $oid = (int)$deliveryOrder['oid'];
  523. if ($orderData['status'] != $status && !$statusServices->count(['oid' => $oid, 'change_type' => $change_type])) {
  524. $order = $orderServices->get($oid);
  525. //修改配送单
  526. $this->dao->update($deliveryOrder['id'], ['status' => $status, 'reason' => $reason]);
  527. //增加
  528. $message = '订单已配送【'. $this->statusData[$status].'】';
  529. $statusServices->save([
  530. 'oid' => $oid,
  531. 'change_type' => $change_type,
  532. 'change_message' => $message,
  533. 'change_time' => time()
  534. ]);
  535. switch ($status) {
  536. case 2:
  537. if (!empty($data)) $orderServices->update($oid, $data);
  538. break;
  539. case 4:
  540. //订单收货
  541. if ($order['status'] != 2) {
  542. OrderTakeJob::dispatch([$order]);
  543. }
  544. break;
  545. case -1:
  546. $this->doCancel((int)$deliveryOrder['id'], $deliveryOrder, $deductFee , $reason);
  547. break;
  548. }
  549. }
  550. return true;
  551. }
  552. /**
  553. * 同步达达门店信息
  554. * @param int $id
  555. * @param bool $is_new
  556. * @param int $type
  557. * @param int $station_type
  558. * @return array
  559. * @throws \think\db\exception\DataNotFoundException
  560. * @throws \think\db\exception\DbException
  561. * @throws \think\db\exception\ModelNotFoundException
  562. */
  563. public function syncCityShop(int $id, int $type = 1, int $station_type = 1)
  564. {
  565. $data = [];
  566. if ($station_type == 1) {
  567. $status = sys_config('dada_delivery_status');
  568. } else {
  569. $status = sys_config('uu_delivery_status');
  570. }
  571. if (!$status) {//未开启
  572. return $data;
  573. }
  574. if ($type == 1) {//门店
  575. /** @var SystemStoreServices $storeServices */
  576. $storeServices = app()->make(SystemStoreServices::class);
  577. $station = $storeServices->getStoreInfo($id);
  578. /** @var CityAreaServices $cityArea */
  579. $cityArea = app()->make(CityAreaServices::class);
  580. $station['city_name'] = $cityArea->value(['id' => $station['city']], 'name');
  581. $station['address'] = $station['detailed_address'];
  582. if (!$station['city_shop_id']) {
  583. $station['city_shop_id'] = 'deliver_store_' . $station['id'] . '_'. $this->getOrderSn();
  584. $storeServices->update($id, ['city_shop_id' => $station['city_shop_id']]);
  585. }
  586. } elseif ($type == 2){//供应商
  587. /** @var SystemSupplierServices $services */
  588. $services = app()->make(SystemSupplierServices::class);
  589. $station = $services->getSupplierInfo($id);
  590. if ($station) {
  591. $station['name'] = $station['supplier_name'] ?? '';
  592. }
  593. } else {//平台
  594. $station = SystemConfigService::more(['refund_address', 'refund_name', 'refund_phone']);
  595. $address = $station['refund_address'];
  596. if ($address) {
  597. $station['address'] = $station['refund_address'];
  598. $station['name'] = $station['refund_name'];
  599. $station['phone'] = $station['refund_phone'];
  600. $station['city_shop_id'] = $this->platCityShopId;
  601. }
  602. }
  603. if (!isset($station['latitude'])) {//地址转经纬度
  604. $addressArr = $this->addressHandle($station['address']);
  605. if (!$addressArr['province'] || !$addressArr['city']) {
  606. throw new ValidateException($type == 0 ? '请检查(设置->商城设置->交易设置->退货收货人地址)完整性' : '请检查该订单关联供应商地址信息是否正确');
  607. }
  608. $station['city_name'] = $addressArr['city'] ?? '';
  609. try {
  610. $addres = $this->lbs_address($station['city_name'], $station['address']);
  611. $station['latitude'] = $addres['location']['lat'] ?? '';
  612. $station['longitude'] = $addres['location']['lng'] ?? '';
  613. } catch (\Exception $e) {
  614. throw new ValidateException('获取经纬度失败');
  615. }
  616. }
  617. $data = [
  618. 'lng' => (float)($station['longitude'] ?? 0),
  619. 'lat' => (float)($station['latitude'] ?? 0),
  620. 'phone' => $station['phone'] ?? '',
  621. 'business' => (int)($station['business'] ?? 5),
  622. 'contact_name' => $station['name'] ?? '',
  623. 'station_name' => $station['name'],
  624. 'station_address' => $station['address'] ?? '',
  625. 'status' => 1,
  626. 'origin_shop_id' => $station['city_shop_id'],
  627. ];
  628. if ($data) {
  629. $serve = DeliverySevices::init($station_type);
  630. try {
  631. $shop = $serve->getShopDetail($data['origin_shop_id']);
  632. } catch (\Throwable $e) {
  633. $shop = [];
  634. }
  635. try {
  636. if (!$shop) {
  637. $serve->addShop($data);
  638. } else {
  639. $serve->updateShop($data);
  640. }
  641. } catch (\Throwable $e) {
  642. throw new ValidateException('创建达达门店失败,原因:' . $e->getMessage());
  643. }
  644. }
  645. $data['city_name'] = $station['city_name'];
  646. return $data;
  647. }
  648. }