AfterSaleLogic.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\adminapi\logic\settings\order\TransactionSettingsLogic;
  21. use app\common\enum\AccountLogEnum;
  22. use app\common\enum\AfterSaleEnum;
  23. use app\common\enum\AfterSaleLogEnum;
  24. use app\common\enum\NoticeEnum;
  25. use app\common\enum\OrderEnum;
  26. use app\common\enum\YesNoEnum;
  27. use app\common\logic\BaseLogic;
  28. use app\common\model\AddressLibrary;
  29. use app\common\model\AfterSale;
  30. use app\common\model\AfterSaleGoods;
  31. use app\common\model\AfterSaleLog;
  32. use app\common\model\Delivery;
  33. use app\common\model\Order;
  34. use app\common\model\OrderGoods;
  35. use app\common\service\after_sale\AfterSaleService;
  36. use app\common\service\ConfigService;
  37. use app\common\service\FileService;
  38. use app\common\service\RegionService;
  39. use think\facade\Db;
  40. /**
  41. * 售后逻辑层
  42. * Class AfterSaleLogic
  43. * @package app\shopapi\logic
  44. */
  45. class AfterSaleLogic extends BaseLogic
  46. {
  47. /**
  48. * @notes 获取子订单商品信息
  49. * @param $params
  50. * @return array
  51. * @author Tab
  52. * @date 2021/7/31 18:21
  53. */
  54. public static function orderGoodsInfo($params)
  55. {
  56. $field = 'og.id as order_goods_id, og.goods_num, og.total_pay_price,og.goods_snap,og.express_price,og.order_id';
  57. $field .= ',g.name as goods_name, g.image as goods_image';
  58. $field .= ',gi.image as item_image, gi.spec_value_str';
  59. $orderGoods = OrderGoods::alias('og')
  60. ->leftJoin('goods g', 'g.id = og.goods_id')
  61. ->leftJoin('goods_item gi', 'gi.id = og.item_id')
  62. ->field($field)
  63. ->findOrEmpty($params['order_goods_id']);
  64. if($orderGoods->isEmpty()) {
  65. return [];
  66. }
  67. $orderGoods = $orderGoods->toArray();
  68. $orderGoods['image'] = $orderGoods['item_image'] ?: $orderGoods['goods_image'];
  69. $orderGoods['image'] = FileService::getFileUrl($orderGoods['image']);
  70. if(isset($params['refund_method'])) {
  71. $orderGoods['reason'] = AfterSaleEnum::getReason($params['refund_method']);
  72. }
  73. //非待发货时扣除运费
  74. $order = Order::where('id',$orderGoods['order_id'])->findOrEmpty()->toArray();
  75. if ($order['order_status'] != OrderEnum::STATUS_WAIT_DELIVERY) {
  76. $orderGoods['total_pay_price'] = $orderGoods['total_pay_price'] - $orderGoods['express_price'];
  77. }
  78. return $orderGoods;
  79. }
  80. /**
  81. * @notes 申请商品售后
  82. * @param $params
  83. * @return bool
  84. * @author Tab
  85. * @date 2021/8/2 11:54
  86. */
  87. public static function apply($params)
  88. {
  89. DB::startTrans();
  90. try {
  91. // 校验是否允许售后申请
  92. self::checkCondition($params);
  93. // 生成售后记录
  94. $data = self::createGoodsAfterSale($params);
  95. // 新的售后申请成功,删除该子订单以前售后失败的记录
  96. $ids = AfterSale::where([
  97. 'order_goods_id' => $params['order_goods_id'],
  98. 'status' => AfterSaleEnum::STATUS_FAIL
  99. ])->column('id');
  100. if (!empty($ids)) {
  101. AfterSale::destroy($ids);
  102. AfterSaleGoods::where('after_sale_id', 'in', $ids)->useSoftDelete('delete_time', time())->delete();
  103. }
  104. // 消息通知 - 通知卖家
  105. $mobile = ConfigService::get('shop', 'return_contact_mobile');
  106. event('Notice', [
  107. 'scene_id' => NoticeEnum::SELLER_REFUND_APPLY_NOTICE,
  108. 'params' => [
  109. 'mobile' => $mobile,
  110. 'after_sale_sn' => $data['after_sale']->sn
  111. ]
  112. ]);
  113. Db::commit();
  114. return [
  115. 'after_sale_id' => $data['after_sale']->id,
  116. 'after_sale_goods_id' => $data['after_sale_goods']->id,
  117. ];
  118. } catch(\Exception $e) {
  119. Db::rollback();
  120. self::setError($e->getMessage());
  121. return false;
  122. }
  123. }
  124. /**
  125. * @notes 校验是否允许售后申请
  126. * @param $params
  127. * @throws \think\Exception
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @author Tab
  132. * @date 2021/8/2 11:54
  133. */
  134. public static function checkCondition($params)
  135. {
  136. $orderGoods = OrderGoods::findOrEmpty($params['order_goods_id']);
  137. if($orderGoods->isEmpty()) {
  138. throw new \think\Exception('子订单不存在,无法发起商品售后');
  139. }
  140. $orderGoods = $orderGoods->toArray();
  141. $order = Order::findOrEmpty($orderGoods['order_id']);
  142. if ($order['user_id'] != $params['user_id']) {
  143. throw new \think\Exception('您没有权限发起该订单的售后');
  144. }
  145. if($order['pay_status'] != YesNoEnum::YES) {
  146. throw new \think\Exception('主订单未付款,不允许发起商品售后');
  147. }
  148. if ($order['order_status'] == OrderEnum::STATUS_FINISH) {
  149. $after_sales = ConfigService::get('transaction', 'after_sales');
  150. if(!$after_sales) {
  151. throw new \think\Exception('系统已关闭售后维权');
  152. }
  153. if(!is_null($order['after_sale_deadline']) && ($order['after_sale_deadline'] < time())) {
  154. throw new \think\Exception('订单已过售后时间,无法发起商品售后');
  155. }
  156. }
  157. //批量下单后,过了取消订单的时间,可对其中一件商品进行退款操作
  158. if (in_array($order['order_status'],[OrderEnum::STATUS_WAIT_DELIVERY,OrderEnum::STATUS_WAIT_RECEIVE])) {
  159. $ableCancelOrder = ConfigService::get('transaction', 'cancel_unshipped_orders');
  160. if ($ableCancelOrder == YesNoEnum::YES) {
  161. $configTime = ConfigService::get('transaction', 'cancel_unshipped_orders_times');
  162. $ableCancelTime = strtotime($order['pay_time']) + ($configTime * 60);
  163. if (time() < $ableCancelTime) {
  164. throw new \think\Exception('买家取消待发货订单未过,无法发起商品售后');
  165. }
  166. }
  167. }
  168. $aferSale = AfterSale::where([
  169. ['order_goods_id', '=', $orderGoods['id']],
  170. ['status', '=', AfterSaleEnum::STATUS_SUCCESS]
  171. ])->select()->toArray();
  172. if($aferSale) {
  173. throw new \think\Exception('该子订单已售后成功, 不能重复发起售后');
  174. }
  175. $aferSale = AfterSale::where([
  176. ['order_goods_id', '=', $orderGoods['id']],
  177. ['status', '=', AfterSaleEnum::STATUS_ING]
  178. ])->select()->toArray();
  179. if($aferSale) {
  180. throw new \think\Exception('该子订单已在售后中,请耐心等待');
  181. }
  182. $aferSale = AfterSale::where([
  183. ['order_id', '=', $order['id']],
  184. ['refund_type', '=', AfterSaleEnum::REFUND_TYPE_ORDER],
  185. ['status', '=', AfterSaleEnum::STATUS_SUCCESS]
  186. ])->select()->toArray();
  187. if($aferSale) {
  188. throw new \think\Exception('主订单已售后成功, 不能重复发起售后');
  189. }
  190. $aferSale = AfterSale::where([
  191. ['order_id', '=', $order['id']],
  192. ['refund_type', '=', AfterSaleEnum::REFUND_TYPE_ORDER],
  193. ['status', '=', AfterSaleEnum::STATUS_ING]
  194. ])->select()->toArray();
  195. if($aferSale) {
  196. throw new \think\Exception('主订单已在售后中,请耐心等待');
  197. }
  198. }
  199. /**
  200. * @notes 生成售后记录
  201. * @param $params
  202. * @author Tab
  203. * @date 2021/8/2 11:54
  204. */
  205. public static function createGoodsAfterSale($params)
  206. {
  207. $orderGoods = OrderGoods::findOrEmpty($params['order_goods_id'])->toArray();
  208. $order = Order::where('id',$orderGoods['order_id'])->findOrEmpty()->toArray();
  209. $refund_total_amount = $orderGoods['total_pay_price'];
  210. if ($order['order_status'] != OrderEnum::STATUS_WAIT_DELIVERY) {
  211. $refund_total_amount = $orderGoods['total_pay_price'] - $orderGoods['express_price'];
  212. }
  213. $params['refund_image'] = isset($params['refund_image']) ? FileService::setFileUrl($params['refund_image']) : '';
  214. // 生成售后主表记录
  215. $data = [
  216. 'sn' => generate_sn((new AfterSale()), 'sn'),
  217. 'user_id' => $params['user_id'],
  218. 'order_id' => $orderGoods['order_id'],
  219. 'order_goods_id' => $orderGoods['id'],
  220. 'refund_reason' => $params['refund_reason'],
  221. 'refund_remark' => $params['refund_remark'] ?? '',
  222. 'refund_image' => $params['refund_image'],
  223. 'refund_type' => AfterSaleEnum::REFUND_TYPE_GOODS,
  224. 'refund_method' => $params['refund_method'],
  225. 'refund_total_amount' => $refund_total_amount,
  226. 'status' => AfterSaleEnum::STATUS_ING,
  227. 'sub_status' => AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE,
  228. 'refund_status' => AfterSaleEnum::NO_REFUND,
  229. 'voucher' => $params['voucher'] ?? []
  230. ];
  231. $afterSale = AfterSale::create($data);
  232. // 生成售后商品记录
  233. $data = [
  234. 'after_sale_id' => $afterSale->id,
  235. 'order_goods_id' => $orderGoods['id'],
  236. 'goods_id' => $orderGoods['goods_id'],
  237. 'item_id' => $orderGoods['item_id'],
  238. 'goods_price' => $orderGoods['goods_price'],
  239. 'goods_num' => $orderGoods['goods_num'],
  240. 'refund_amount' => $refund_total_amount
  241. ];
  242. $afterSaleGoods = AfterSaleGoods::create($data);
  243. // 生成售后日志
  244. self::createAfterLog($afterSale->id, '买家发起商品售后,等待卖家同意', $params['user_id'], AfterSaleLogEnum::ROLE_BUYER);
  245. return [
  246. 'after_sale' => $afterSale,
  247. 'after_sale_goods' => $afterSaleGoods,
  248. ];
  249. }
  250. /**
  251. * @notes 生成售后日志
  252. * @param $afterSaleId
  253. * @param $content
  254. * @param null $operatorId
  255. * @param null $operatorRole
  256. * @author Tab
  257. * @date 2021/8/9 20:10
  258. */
  259. public static function createAfterLog($afterSaleId, $content, $operatorId = null, $operatorRole = null)
  260. {
  261. $data = [
  262. 'after_sale_id' => $afterSaleId,
  263. 'content' => $content,
  264. 'operator_id' => $operatorId,
  265. 'operator_role' => $operatorRole,
  266. ];
  267. AfterSaleLog::create($data);
  268. }
  269. /**
  270. * @notes 买家取消售后
  271. * @param $params
  272. * @return bool
  273. * @author Tab
  274. * @date 2021/8/3 10:26
  275. */
  276. public static function cancel($params)
  277. {
  278. try {
  279. $afterSale = AfterSale::findOrEmpty($params['id']);
  280. if($afterSale->isEmpty()) {
  281. throw new \think\Exception('售后订单不存在');
  282. }
  283. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  284. throw new \think\Exception('不是售后中状态,不允许撤销申请');
  285. }
  286. if(!in_array($afterSale->sub_status,AfterSaleEnum::ALLOW_CANCEL)) {
  287. throw new \think\Exception('售后处理中,不允许撤销申请');
  288. }
  289. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  290. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_BUYER_CANCEL_AFTER_SALE;
  291. $afterSale->save();
  292. // 售后日志
  293. AfterSaleService::createAfterLog($afterSale->id, '买家取消售后', $params['user_id'], AfterSaleLogEnum::ROLE_BUYER);
  294. Db::commit();
  295. return true;
  296. } catch(\Exception $e) {
  297. Db::rollback();
  298. self::setError($e->getMessage());
  299. return false;
  300. }
  301. }
  302. /**
  303. * @notes 买家确认退货
  304. * @param $params
  305. * @return bool
  306. * @author Tab
  307. * @date 2021/8/3 11:45
  308. */
  309. public static function returnGoods($params)
  310. {
  311. Db::startTrans();
  312. try {
  313. $afterSale = AfterSale::findOrEmpty($params['id']);
  314. if($afterSale->isEmpty()) {
  315. throw new \think\Exception('售后订单不存在');
  316. }
  317. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_BUYER_RETURN) {
  318. throw new \think\Exception('非等待买家退货状态,不允许进行确认退货操作');
  319. }
  320. if(isset($params['express_image']) && !empty($params['express_image'])) {
  321. $params['express_image'] = FileService::setFileUrl($params['express_image']);
  322. }
  323. $afterSale->express_name = $params['express_name'];
  324. $afterSale->invoice_no = $params['invoice_no'];
  325. $afterSale->express_remark = $params['express_remark'] ?? '';
  326. $afterSale->express_image = $params['express_image'] ?? '';
  327. $afterSale->express_time = time();
  328. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT;
  329. $afterSale->save();
  330. // 记录日志
  331. AfterSaleService::createAfterLog($afterSale->id, '买家已退货,等待卖家确认收货', $params['user_id'], AfterSaleLogEnum::ROLE_BUYER);
  332. Db::commit();
  333. return true;
  334. } catch(\Exception $e) {
  335. Db::rollback();
  336. self::setError($e->getMessage());
  337. return false;
  338. }
  339. }
  340. /**
  341. * @notes 查看售后列表
  342. * @param $params
  343. * @return array|mixed
  344. * @author Tab
  345. * @date 2021/8/10 14:20
  346. */
  347. public static function lists($params)
  348. {
  349. $lists = [];
  350. switch($params['type']) {
  351. // 未发起过售后的子订单
  352. case 'apply':
  353. $lists = self::applyList($params);
  354. break;
  355. // 售后中、售后成功、售后结束
  356. case 'status_ing';
  357. case 'status_success';
  358. case 'status_fail';
  359. case 'status_success_fail';
  360. $lists = self::statusLists($params);
  361. break;
  362. }
  363. // 统计数据
  364. $lists['extend'] = self::statistics($params);
  365. return $lists;
  366. }
  367. /**
  368. * @notes 统计数据
  369. * @author Tab
  370. * @date 2021/12/7 15:26
  371. */
  372. public static function statistics($params)
  373. {
  374. // 未发起过售后的子订单
  375. $afterSaleList = AfterSaleGoods::alias('asg')
  376. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  377. ->where('as.user_id',$params['user_id'])
  378. ->column('asg.order_goods_id');
  379. $orderGoodsWhere = [
  380. ['o.user_id', '=', $params['user_id']],
  381. ['o.pay_status', '=', YesNoEnum::YES],
  382. ['og.id', 'not in', $afterSaleList],
  383. ];
  384. $field = 'og.id,og.goods_num,og.goods_price,og.goods_snap';
  385. $apply = OrderGoods::alias('og')
  386. ->leftJoin('order o', 'o.id = og.order_id')
  387. ->field($field)
  388. ->where($orderGoodsWhere)
  389. ->count();
  390. // 处理中的售后
  391. $field = 'asg.id as sub_id,asg.create_time,as.id as master_id,as.refund_method,as.sub_status,asg.order_goods_id as goods_snap,og.goods_num,og.goods_price,og.id as order_goods_id';
  392. $ing = AfterSaleGoods::alias('asg')
  393. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  394. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  395. ->where([
  396. ['user_id', '=', $params['user_id']],
  397. ['status', 'in', [AfterSaleEnum::STATUS_ING]],
  398. ])
  399. ->field($field)
  400. ->count();
  401. // 处理完成的售后
  402. $finish = AfterSaleGoods::alias('asg')
  403. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  404. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  405. ->where([
  406. ['user_id', '=', $params['user_id']],
  407. ['status', 'in', [AfterSaleEnum::STATUS_SUCCESS, AfterSaleEnum::STATUS_FAIL]],
  408. ])
  409. ->field($field)
  410. ->count();
  411. return [
  412. 'apply' => $apply,
  413. 'ing' => $ing,
  414. 'finish' => $finish,
  415. ];
  416. }
  417. /**
  418. * @notes 未发起过售后的子订单列表
  419. * @param $params
  420. * @return mixed
  421. * @author Tab
  422. * @date 2021/8/10 11:29
  423. */
  424. public static function applyList($params)
  425. {
  426. // 未发起过售后的子订单
  427. $afterSaleList = AfterSaleGoods::alias('asg')
  428. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  429. ->where('as.user_id',$params['user_id'])
  430. ->column('asg.order_goods_id');
  431. $orderGoodsWhere = [
  432. ['o.user_id', '=', $params['user_id']],
  433. ['o.pay_status', '=', YesNoEnum::YES],
  434. ['og.id', 'not in', $afterSaleList],
  435. ];
  436. $field = 'og.id,og.goods_num,og.goods_price,og.goods_snap';
  437. $orderGoodsLists = OrderGoods::alias('og')
  438. ->leftJoin('order o', 'o.id = og.order_id')
  439. ->field($field)
  440. ->where($orderGoodsWhere)
  441. ->order('og.id', 'desc')
  442. ->page($params['page_no'], $params['page_size'])
  443. ->select()
  444. ->toArray();
  445. $count = OrderGoods::alias('og')
  446. ->leftJoin('order o', 'o.id = og.order_id')
  447. ->field($field)
  448. ->where($orderGoodsWhere)
  449. ->count();
  450. $data = [
  451. 'lists' => $orderGoodsLists,
  452. 'page' => $params['page_no'],
  453. 'size' => $params['page_size'],
  454. 'count' => $count,
  455. 'more' => is_more($count, $params['page_no'], $params['page_size'])
  456. ];
  457. return $data;
  458. }
  459. /**
  460. * @notes 售后列表
  461. * @param $params
  462. * @return mixed
  463. * @author Tab
  464. * @date 2021/8/10 14:09
  465. */
  466. public static function statusLists($params)
  467. {
  468. switch($params['type']) {
  469. case 'status_ing':
  470. $status = [AfterSaleEnum::STATUS_ING];
  471. break;
  472. case 'status_success':
  473. $status = [AfterSaleEnum::STATUS_SUCCESS];
  474. break;
  475. case 'status_fail':
  476. $status = [AfterSaleEnum::STATUS_FAIL];
  477. break;
  478. case 'status_success_fail':
  479. $status = [AfterSaleEnum::STATUS_SUCCESS, AfterSaleEnum::STATUS_FAIL];
  480. break;
  481. }
  482. $field = 'asg.id as sub_id,asg.create_time,as.id as master_id,as.refund_method,as.sub_status,asg.order_goods_id as goods_snap,og.goods_num,og.goods_price,og.id as order_goods_id';
  483. $lists = AfterSaleGoods::alias('asg')
  484. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  485. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  486. ->where([
  487. ['user_id', '=', $params['user_id']],
  488. ['status', 'in', $status],
  489. ])
  490. ->field($field)
  491. ->page($params['page_no'], $params['page_size'])
  492. ->order('asg.id', 'desc')
  493. ->select()
  494. ->toArray();
  495. $count = AfterSaleGoods::alias('asg')
  496. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  497. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  498. ->where([
  499. ['user_id', '=', $params['user_id']],
  500. ['status', 'in', $status],
  501. ])
  502. ->field($field)
  503. ->count();
  504. foreach($lists as &$item) {
  505. $item['refund_method_desc'] = AfterSaleEnum::getMethodDesc($item['refund_method']);
  506. $item['sub_status_desc'] = AfterSaleEnum::getSubStatusDesc($item['sub_status']);
  507. $item['btns'] = AfterSaleEnum::getBtns2($item['sub_status']);
  508. }
  509. $data = [
  510. 'lists' => $lists,
  511. 'page' => $params['page_no'],
  512. 'size' => $params['page_size'],
  513. 'count' => $count,
  514. 'more' => is_more($count, $params['page_no'], $params['page_size'])
  515. ];
  516. return $data;
  517. }
  518. /**
  519. * @notes 查看售后详情
  520. * @param $params
  521. * @return mixed
  522. * @author Tab
  523. * @date 2021/8/10 15:21
  524. */
  525. public static function detail($params)
  526. {
  527. $field = 'asg.id as sub_id,as.sub_status,asg.order_goods_id,asg.order_goods_id as goods_snap,asg.goods_num,asg.goods_price,as.refund_method,as.refund_reason,asg.refund_amount,as.refund_remark,as.id as master_id,as.sn,as.create_time,as.voucher,as.express_name,as.invoice_no,as.express_time,as.order_id';
  528. $detail = AfterSaleGoods::alias('asg')
  529. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  530. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  531. ->field($field)
  532. ->findOrEmpty($params['id'])
  533. ->toArray();
  534. if(empty($detail)) {
  535. return [];
  536. }
  537. $detail['refund_method_desc'] = AfterSaleEnum::getMethodDesc($detail['refund_method']);
  538. $detail['sub_status_desc'] = AfterSaleEnum::getSubStatusDesc($detail['sub_status']);
  539. $detail['btns'] = AfterSaleEnum::getBtns2($detail['sub_status']);
  540. $detail['voucher'] = empty($detail['voucher']) ? [] : json_decode($detail['voucher'], true);
  541. $detail['express_time'] = empty($detail['express_time']) ? '' : date('Y-m-d H:i:s', $detail['express_time']);
  542. foreach ($detail['voucher'] as &$item) {
  543. $item = FileService::getFileUrl($item);
  544. }
  545. // 退货地址
  546. // $detail['return_contact'] = ConfigService::get('shop', 'return_contact', '');
  547. // $detail['return_contact_mobile'] = ConfigService::get('shop', 'return_contact_mobile', '');
  548. // $detail['return_province'] = ConfigService::get('shop', 'return_province', '');
  549. // $detail['return_city'] = ConfigService::get('shop', 'return_city', '');
  550. // $detail['return_district'] = ConfigService::get('shop', 'return_district', '');
  551. // $detail['return_address'] = ConfigService::get('shop', 'return_address', '');
  552. // $detail['address'] = RegionService::getAddress([$detail['return_province'], $detail['return_city'], $detail['return_district']], $detail['return_address']);
  553. $deliveryLists = Delivery::where(['order_id'=>$detail['order_id']])->field('order_goods_info,return_address_info')->json(['return_address_info',true])->order(['id'=>'desc'])->select()->toArray();
  554. $returnAddress = [];
  555. foreach ($deliveryLists as $delivery) {
  556. if (!empty($delivery['order_goods_info'])) {
  557. $orderGoodsIds = array_column($delivery['order_goods_info'], 'order_goods_id');
  558. if (in_array($detail['order_goods_id'], $orderGoodsIds)) {
  559. $returnAddress = $delivery['return_address_info'];
  560. }
  561. break;
  562. }
  563. }
  564. if (empty($returnAddress)) {
  565. $returnAddress = AddressLibrary::order(['is_return_default'=>'desc','id'=>'asc'])->append(['province','city','district'])->findOrEmpty()->toArray();
  566. }
  567. $detail['return_address'] = $returnAddress;
  568. return $detail;
  569. }
  570. }