AfterSaleLogic.php 25 KB

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