AfterSaleLogic.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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,og.express_status';
  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. //
  176. // $aferSale = AfterSale::where([
  177. // ['order_goods_id', '=', $orderGoods['id']],
  178. // ['status', '=', AfterSaleEnum::STATUS_ING]
  179. // ])->select()->toArray();
  180. // if($aferSale) {
  181. // throw new \think\Exception('该子订单已在售后中,请耐心等待');
  182. // }
  183. // 查询加锁防重复提交
  184. $aferSale = AfterSale::where([
  185. ['order_goods_id', '=', $orderGoods['id']],
  186. ['status', 'in', [AfterSaleEnum::STATUS_ING, AfterSaleEnum::STATUS_SUCCESS]]
  187. ])->lock(true)->findOrEmpty();
  188. if (!$aferSale->isEmpty()) {
  189. throw new \think\Exception('该子订单已存在售后申请');
  190. }
  191. $aferSale = AfterSale::where([
  192. ['order_id', '=', $order['id']],
  193. ['refund_type', '=', AfterSaleEnum::REFUND_TYPE_ORDER],
  194. ['status', '=', AfterSaleEnum::STATUS_SUCCESS]
  195. ])->select()->toArray();
  196. if($aferSale) {
  197. throw new \think\Exception('主订单已售后成功, 不能重复发起售后');
  198. }
  199. $aferSale = AfterSale::where([
  200. ['order_id', '=', $order['id']],
  201. ['refund_type', '=', AfterSaleEnum::REFUND_TYPE_ORDER],
  202. ['status', '=', AfterSaleEnum::STATUS_ING]
  203. ])->select()->toArray();
  204. if($aferSale) {
  205. throw new \think\Exception('主订单已在售后中,请耐心等待');
  206. }
  207. }
  208. /**
  209. * @notes 生成售后记录
  210. * @param $params
  211. * @author Tab
  212. * @date 2021/8/2 11:54
  213. */
  214. public static function createGoodsAfterSale($params)
  215. {
  216. $orderGoods = OrderGoods::findOrEmpty($params['order_goods_id'])->toArray();
  217. $order = Order::where('id',$orderGoods['order_id'])->findOrEmpty()->toArray();
  218. $refund_total_amount = $orderGoods['total_pay_price'];
  219. if ($order['order_status'] != OrderEnum::STATUS_WAIT_DELIVERY) {
  220. $refund_total_amount = $orderGoods['total_pay_price'] - $orderGoods['express_price'];
  221. }
  222. $params['refund_image'] = isset($params['refund_image']) ? FileService::setFileUrl($params['refund_image']) : '';
  223. // 生成售后主表记录
  224. $data = [
  225. 'sn' => generate_sn((new AfterSale()), 'sn'),
  226. 'user_id' => $params['user_id'],
  227. 'order_id' => $orderGoods['order_id'],
  228. 'order_goods_id' => $orderGoods['id'],
  229. 'refund_reason' => $params['refund_reason'],
  230. 'refund_remark' => $params['refund_remark'] ?? '',
  231. 'refund_image' => $params['refund_image'],
  232. 'refund_type' => AfterSaleEnum::REFUND_TYPE_GOODS,
  233. 'refund_method' => $params['refund_method'],
  234. 'refund_total_amount' => $refund_total_amount,
  235. 'status' => AfterSaleEnum::STATUS_ING,
  236. 'sub_status' => AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE,
  237. 'refund_status' => AfterSaleEnum::NO_REFUND,
  238. 'voucher' => $params['voucher'] ?? []
  239. ];
  240. $afterSale = AfterSale::create($data);
  241. // 生成售后商品记录
  242. $data = [
  243. 'after_sale_id' => $afterSale->id,
  244. 'order_goods_id' => $orderGoods['id'],
  245. 'goods_id' => $orderGoods['goods_id'],
  246. 'item_id' => $orderGoods['item_id'],
  247. 'goods_price' => $orderGoods['goods_price'],
  248. 'goods_num' => $orderGoods['goods_num'],
  249. 'refund_amount' => $refund_total_amount
  250. ];
  251. $afterSaleGoods = AfterSaleGoods::create($data);
  252. // 生成售后日志
  253. self::createAfterLog($afterSale->id, '买家发起商品售后,等待卖家同意', $params['user_id'], AfterSaleLogEnum::ROLE_BUYER);
  254. return [
  255. 'after_sale' => $afterSale,
  256. 'after_sale_goods' => $afterSaleGoods,
  257. ];
  258. }
  259. /**
  260. * @notes 生成售后日志
  261. * @param $afterSaleId
  262. * @param $content
  263. * @param null $operatorId
  264. * @param null $operatorRole
  265. * @author Tab
  266. * @date 2021/8/9 20:10
  267. */
  268. public static function createAfterLog($afterSaleId, $content, $operatorId = null, $operatorRole = null)
  269. {
  270. $data = [
  271. 'after_sale_id' => $afterSaleId,
  272. 'content' => $content,
  273. 'operator_id' => $operatorId,
  274. 'operator_role' => $operatorRole,
  275. ];
  276. AfterSaleLog::create($data);
  277. }
  278. /**
  279. * @notes 买家取消售后
  280. * @param $params
  281. * @return bool
  282. * @author Tab
  283. * @date 2021/8/3 10:26
  284. */
  285. public static function cancel($params)
  286. {
  287. try {
  288. $afterSale = AfterSale::findOrEmpty($params['id']);
  289. if($afterSale->isEmpty()) {
  290. throw new \think\Exception('售后订单不存在');
  291. }
  292. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  293. throw new \think\Exception('不是售后中状态,不允许撤销申请');
  294. }
  295. if(!in_array($afterSale->sub_status,AfterSaleEnum::ALLOW_CANCEL)) {
  296. throw new \think\Exception('售后处理中,不允许撤销申请');
  297. }
  298. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  299. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_BUYER_CANCEL_AFTER_SALE;
  300. $afterSale->save();
  301. // 售后日志
  302. AfterSaleService::createAfterLog($afterSale->id, '买家取消售后', $params['user_id'], AfterSaleLogEnum::ROLE_BUYER);
  303. Db::commit();
  304. return true;
  305. } catch(\Exception $e) {
  306. Db::rollback();
  307. self::setError($e->getMessage());
  308. return false;
  309. }
  310. }
  311. /**
  312. * @notes 买家确认退货
  313. * @param $params
  314. * @return bool
  315. * @author Tab
  316. * @date 2021/8/3 11:45
  317. */
  318. public static function returnGoods($params)
  319. {
  320. Db::startTrans();
  321. try {
  322. $afterSale = AfterSale::findOrEmpty($params['id']);
  323. if($afterSale->isEmpty()) {
  324. throw new \think\Exception('售后订单不存在');
  325. }
  326. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_BUYER_RETURN) {
  327. throw new \think\Exception('非等待买家退货状态,不允许进行确认退货操作');
  328. }
  329. if(isset($params['express_image']) && !empty($params['express_image'])) {
  330. $params['express_image'] = FileService::setFileUrl($params['express_image']);
  331. }
  332. $afterSale->express_name = $params['express_name'];
  333. $afterSale->invoice_no = $params['invoice_no'];
  334. $afterSale->express_remark = $params['express_remark'] ?? '';
  335. $afterSale->express_image = $params['express_image'] ?? '';
  336. $afterSale->express_time = time();
  337. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT;
  338. $afterSale->save();
  339. // 记录日志
  340. AfterSaleService::createAfterLog($afterSale->id, '买家已退货,等待卖家确认收货', $params['user_id'], AfterSaleLogEnum::ROLE_BUYER);
  341. Db::commit();
  342. return true;
  343. } catch(\Exception $e) {
  344. Db::rollback();
  345. self::setError($e->getMessage());
  346. return false;
  347. }
  348. }
  349. /**
  350. * @notes 查看售后列表
  351. * @param $params
  352. * @return array|mixed
  353. * @author Tab
  354. * @date 2021/8/10 14:20
  355. */
  356. public static function lists($params)
  357. {
  358. $lists = [];
  359. switch($params['type']) {
  360. // 未发起过售后的子订单
  361. case 'apply':
  362. $lists = self::applyList($params);
  363. break;
  364. // 售后中、售后成功、售后结束
  365. case 'status_ing';
  366. case 'status_success';
  367. case 'status_fail';
  368. case 'status_success_fail';
  369. $lists = self::statusLists($params);
  370. break;
  371. }
  372. // 统计数据
  373. $lists['extend'] = self::statistics($params);
  374. return $lists;
  375. }
  376. /**
  377. * @notes 统计数据
  378. * @author Tab
  379. * @date 2021/12/7 15:26
  380. */
  381. public static function statistics($params)
  382. {
  383. // 未发起过售后的子订单
  384. $afterSaleList = AfterSaleGoods::alias('asg')
  385. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  386. ->where('as.user_id',$params['user_id'])
  387. ->column('asg.order_goods_id');
  388. $orderGoodsWhere = [
  389. ['o.user_id', '=', $params['user_id']],
  390. ['o.pay_status', '=', YesNoEnum::YES],
  391. ['og.id', 'not in', $afterSaleList],
  392. ];
  393. $field = 'og.id,og.goods_num,og.goods_price,og.goods_snap';
  394. $apply = OrderGoods::alias('og')
  395. ->leftJoin('order o', 'o.id = og.order_id')
  396. ->field($field)
  397. ->where($orderGoodsWhere)
  398. ->count();
  399. // 处理中的售后
  400. $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';
  401. $ing = AfterSaleGoods::alias('asg')
  402. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  403. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  404. ->where([
  405. ['user_id', '=', $params['user_id']],
  406. ['status', 'in', [AfterSaleEnum::STATUS_ING]],
  407. ])
  408. ->field($field)
  409. ->count();
  410. // 处理完成的售后
  411. $finish = AfterSaleGoods::alias('asg')
  412. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  413. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  414. ->where([
  415. ['user_id', '=', $params['user_id']],
  416. ['status', 'in', [AfterSaleEnum::STATUS_SUCCESS, AfterSaleEnum::STATUS_FAIL]],
  417. ])
  418. ->field($field)
  419. ->count();
  420. return [
  421. 'apply' => $apply,
  422. 'ing' => $ing,
  423. 'finish' => $finish,
  424. ];
  425. }
  426. /**
  427. * @notes 未发起过售后的子订单列表
  428. * @param $params
  429. * @return mixed
  430. * @author Tab
  431. * @date 2021/8/10 11:29
  432. */
  433. public static function applyList($params)
  434. {
  435. // 未发起过售后的子订单
  436. $afterSaleList = AfterSaleGoods::alias('asg')
  437. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  438. ->where('as.user_id',$params['user_id'])
  439. ->column('asg.order_goods_id');
  440. $orderGoodsWhere = [
  441. ['o.user_id', '=', $params['user_id']],
  442. ['o.pay_status', '=', YesNoEnum::YES],
  443. ['og.id', 'not in', $afterSaleList],
  444. ];
  445. $field = 'og.id,og.goods_num,og.goods_price,og.goods_snap';
  446. $orderGoodsLists = OrderGoods::alias('og')
  447. ->leftJoin('order o', 'o.id = og.order_id')
  448. ->field($field)
  449. ->where($orderGoodsWhere)
  450. ->order('og.id', 'desc')
  451. ->page($params['page_no'], $params['page_size'])
  452. ->select()
  453. ->toArray();
  454. $count = OrderGoods::alias('og')
  455. ->leftJoin('order o', 'o.id = og.order_id')
  456. ->field($field)
  457. ->where($orderGoodsWhere)
  458. ->count();
  459. $data = [
  460. 'lists' => $orderGoodsLists,
  461. 'page' => $params['page_no'],
  462. 'size' => $params['page_size'],
  463. 'count' => $count,
  464. 'more' => is_more($count, $params['page_no'], $params['page_size'])
  465. ];
  466. return $data;
  467. }
  468. /**
  469. * @notes 售后列表
  470. * @param $params
  471. * @return mixed
  472. * @author Tab
  473. * @date 2021/8/10 14:09
  474. */
  475. public static function statusLists($params)
  476. {
  477. switch($params['type']) {
  478. case 'status_ing':
  479. $status = [AfterSaleEnum::STATUS_ING];
  480. break;
  481. case 'status_success':
  482. $status = [AfterSaleEnum::STATUS_SUCCESS];
  483. break;
  484. case 'status_fail':
  485. $status = [AfterSaleEnum::STATUS_FAIL];
  486. break;
  487. case 'status_success_fail':
  488. $status = [AfterSaleEnum::STATUS_SUCCESS, AfterSaleEnum::STATUS_FAIL];
  489. break;
  490. }
  491. $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';
  492. $lists = AfterSaleGoods::alias('asg')
  493. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  494. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  495. ->where([
  496. ['user_id', '=', $params['user_id']],
  497. ['status', 'in', $status],
  498. ])
  499. ->field($field)
  500. ->page($params['page_no'], $params['page_size'])
  501. ->order('asg.id', 'desc')
  502. ->select()
  503. ->toArray();
  504. $count = AfterSaleGoods::alias('asg')
  505. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  506. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  507. ->where([
  508. ['user_id', '=', $params['user_id']],
  509. ['status', 'in', $status],
  510. ])
  511. ->field($field)
  512. ->count();
  513. foreach($lists as &$item) {
  514. $item['refund_method_desc'] = AfterSaleEnum::getMethodDesc($item['refund_method']);
  515. $item['sub_status_desc'] = AfterSaleEnum::getSubStatusDesc($item['sub_status']);
  516. $item['btns'] = AfterSaleEnum::getBtns2($item['sub_status']);
  517. }
  518. $data = [
  519. 'lists' => $lists,
  520. 'page' => $params['page_no'],
  521. 'size' => $params['page_size'],
  522. 'count' => $count,
  523. 'more' => is_more($count, $params['page_no'], $params['page_size'])
  524. ];
  525. return $data;
  526. }
  527. /**
  528. * @notes 查看售后详情
  529. * @param $params
  530. * @return mixed
  531. * @author Tab
  532. * @date 2021/8/10 15:21
  533. */
  534. public static function detail($params)
  535. {
  536. $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,as.status,as.admin_remark';
  537. $detail = AfterSaleGoods::alias('asg')
  538. ->leftJoin('after_sale as', 'as.id = asg.after_sale_id')
  539. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  540. ->field($field)
  541. ->findOrEmpty($params['id'])
  542. ->toArray();
  543. if(empty($detail)) {
  544. return [];
  545. }
  546. $detail['refund_method_desc'] = AfterSaleEnum::getMethodDesc($detail['refund_method']);
  547. $detail['sub_status_desc'] = AfterSaleEnum::getSubStatusDesc($detail['sub_status']);
  548. $detail['btns'] = AfterSaleEnum::getBtns2($detail['sub_status']);
  549. $detail['voucher'] = empty($detail['voucher']) ? [] : json_decode($detail['voucher'], true);
  550. $detail['express_time'] = empty($detail['express_time']) ? '' : date('Y-m-d H:i:s', $detail['express_time']);
  551. foreach ($detail['voucher'] as &$item) {
  552. $item = FileService::getFileUrl($item);
  553. }
  554. // 退货地址
  555. // $detail['return_contact'] = ConfigService::get('shop', 'return_contact', '');
  556. // $detail['return_contact_mobile'] = ConfigService::get('shop', 'return_contact_mobile', '');
  557. // $detail['return_province'] = ConfigService::get('shop', 'return_province', '');
  558. // $detail['return_city'] = ConfigService::get('shop', 'return_city', '');
  559. // $detail['return_district'] = ConfigService::get('shop', 'return_district', '');
  560. // $detail['return_address'] = ConfigService::get('shop', 'return_address', '');
  561. // $detail['address'] = RegionService::getAddress([$detail['return_province'], $detail['return_city'], $detail['return_district']], $detail['return_address']);
  562. $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();
  563. $returnAddress = [];
  564. foreach ($deliveryLists as $delivery) {
  565. if (!empty($delivery['order_goods_info'])) {
  566. $orderGoodsIds = array_column($delivery['order_goods_info'], 'order_goods_id');
  567. if (in_array($detail['order_goods_id'], $orderGoodsIds)) {
  568. $returnAddress = $delivery['return_address_info'];
  569. }
  570. break;
  571. }
  572. }
  573. if (empty($returnAddress)) {
  574. $returnAddress = AddressLibrary::order(['is_return_default'=>'desc','id'=>'asc'])->append(['province','city','district'])->findOrEmpty()->toArray();
  575. }
  576. $detail['return_address'] = $returnAddress;
  577. //订单编号
  578. $detail['order_sn'] = Order::where(['id'=>$detail['order_id']])->value('sn');
  579. return $detail;
  580. }
  581. /**
  582. * @notes 修改物流信息
  583. * @param array $params
  584. * @return string|true
  585. * @author ljj
  586. * @date 2025/4/23 下午3:35
  587. */
  588. public static function changeDelivery(array $params)
  589. {
  590. try{
  591. $afterSale = AfterSale::findOrEmpty($params['id']);
  592. if($afterSale->isEmpty()) {
  593. throw new \think\Exception('售后订单不存在');
  594. }
  595. $afterSale->express_name = $params['express_name'];
  596. $afterSale->invoice_no = $params['invoice_no'];
  597. $afterSale->save();
  598. return true;
  599. }catch (\Exception $e){
  600. return $e->getMessage();
  601. }
  602. }
  603. }