OrderRefund.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\service\User as UserService;
  14. use app\api\model\OrderGoods as OrderGoodsModel;
  15. use app\common\model\OrderRefund as OrderRefundModel;
  16. use app\common\enum\order\refund\RefundType as RefundTypeEnum;
  17. use app\common\enum\order\refund\AuditStatus as AuditStatusEnum;
  18. use app\common\enum\order\refund\RefundStatus as RefundStatusEnum;
  19. use app\common\exception\BaseException;
  20. /**
  21. * 售后单模型
  22. * Class OrderRefund
  23. * @package app\api\model
  24. */
  25. class OrderRefund extends OrderRefundModel
  26. {
  27. /**
  28. * 隐藏字段
  29. * @var array
  30. */
  31. protected $hidden = [
  32. 'store_id',
  33. 'update_time'
  34. ];
  35. /**
  36. * 追加字段
  37. * @var array
  38. */
  39. protected $append = [
  40. 'state_text', // 售后单状态文字描述
  41. ];
  42. /**
  43. * 售后单状态文字描述
  44. * @param $value
  45. * @param $data
  46. * @return string
  47. */
  48. public function getStateTextAttr($value, $data)
  49. {
  50. // 已完成
  51. if ($data['status'] == RefundStatusEnum::COMPLETED) {
  52. $text = [RefundTypeEnum::RETURN => '已同意退货并已退款', RefundTypeEnum::EXCHANGE => '已同意换货'];
  53. return $text[$data['type']];
  54. }
  55. // 已取消
  56. if ($data['status'] == RefundStatusEnum::CANCELLED) {
  57. return '已取消';
  58. }
  59. // 已拒绝
  60. if ($data['status'] == RefundStatusEnum::REJECTED) {
  61. // return '已拒绝';
  62. return $data['type'] == RefundTypeEnum::RETURN ? '已拒绝退货退款' : '已拒绝换货';
  63. }
  64. // 进行中
  65. if ($data['status'] == RefundStatusEnum::NORMAL) {
  66. if ($data['audit_status'] == AuditStatusEnum::WAIT) {
  67. return '等待审核中';
  68. }
  69. if ($data['type'] == RefundTypeEnum::RETURN) {
  70. return $data['is_user_send'] ? '已发货,待平台确认' : '已同意退货,请及时发货';
  71. }
  72. }
  73. return $value;
  74. }
  75. /**
  76. * 获取用户售后单列表
  77. * @param int|null $state 售后单状态
  78. * @return \think\Paginator
  79. * @throws \app\common\exception\BaseException
  80. * @throws \think\db\exception\DbException
  81. */
  82. public function getList(int $state = null)
  83. {
  84. // 检索查询条件
  85. $filter = [];
  86. // 售后单状态
  87. !is_null($state) && $filter[] = ['status', '=', $state];
  88. // 当前用户ID
  89. $userId = UserService::getCurrentLoginUserId();
  90. // 查询列表记录
  91. return $this->with(['orderGoods.image'])
  92. ->where('user_id', '=', $userId)
  93. ->order(['create_time' => 'desc'])
  94. ->paginate(15);
  95. }
  96. /**
  97. * 获取当前用户的售后单详情
  98. * @param int $orderRefundId 售后单ID
  99. * @param bool $isWith 是否关联
  100. * @return static|null
  101. * @throws BaseException
  102. */
  103. public static function getDetail(int $orderRefundId, $isWith = false)
  104. {
  105. // 关联查询
  106. $with = $isWith ? ['orderGoods' => ['image'], 'images.file', 'address'] : [];
  107. // 获取记录
  108. $detail = static::detail([
  109. 'user_id' => UserService::getCurrentLoginUserId(),
  110. 'order_refund_id' => $orderRefundId
  111. ], $with);
  112. if (empty($detail)) throwError('未找到该售后单');
  113. return $detail;
  114. }
  115. /**
  116. * 订单商品详情
  117. * @param int $orderGoodsId 订单商品ID
  118. * @return \app\common\model\OrderGoods|null
  119. * @throws BaseException
  120. */
  121. public function getRefundGoods(int $orderGoodsId)
  122. {
  123. $goods = OrderGoodsModel::detail($orderGoodsId);
  124. if (isset($goods['refund']) && !empty($goods['refund'])) {
  125. throwError('当前商品已申请售后');
  126. }
  127. return $goods;
  128. }
  129. /**
  130. * 用户发货
  131. * @param $data
  132. * @return false|int
  133. */
  134. public function delivery(array $data)
  135. {
  136. if (
  137. $this['type'] != RefundTypeEnum::RETURN
  138. || $this['audit_status'] != AuditStatusEnum::REVIEWED
  139. || $this['is_user_send'] != 0
  140. ) {
  141. $this->error = '当前售后单不合法,不允许该操作';
  142. return false;
  143. }
  144. if ($data['expressId'] <= 0) {
  145. $this->error = '请选择物流公司';
  146. return false;
  147. }
  148. if (empty($data['expressNo'])) {
  149. $this->error = '请填写物流单号';
  150. return false;
  151. }
  152. return $this->save([
  153. 'is_user_send' => 1,
  154. 'send_time' => time(),
  155. 'express_id' => (int)$data['expressId'],
  156. 'express_no' => $data['expressNo'],
  157. ]);
  158. }
  159. /**
  160. * 新增售后单记录
  161. * @param int $orderGoodsId 订单商品ID
  162. * @param array $data 用户提交的表单数据
  163. * @return mixed
  164. * @throws BaseException
  165. */
  166. public function apply(int $orderGoodsId, array $data)
  167. {
  168. // 订单商品详情
  169. $goods = $this->getRefundGoods($orderGoodsId);
  170. return $this->transaction(function () use ($orderGoodsId, $data, $goods) {
  171. // 新增售后单记录
  172. $this->save([
  173. 'order_goods_id' => $orderGoodsId,
  174. 'order_id' => $goods['order_id'],
  175. 'user_id' => UserService::getCurrentLoginUserId(),
  176. 'type' => $data['type'],
  177. 'apply_desc' => $data['content'],
  178. 'audit_status' => AuditStatusEnum::WAIT,
  179. 'status' => 0,
  180. 'store_id' => self::$storeId
  181. ]);
  182. // 记录凭证图片关系
  183. if (isset($data['images']) && !empty($data['images'])) {
  184. $this->saveImages((int)$this['order_refund_id'], $data['images']);
  185. }
  186. return true;
  187. });
  188. }
  189. /**
  190. * 记录售后单图片
  191. * @param int $orderRefundId 售后单ID
  192. * @param array $images 图片列表
  193. * @return bool
  194. */
  195. private function saveImages(int $orderRefundId, array $images)
  196. {
  197. // 生成评价图片数据
  198. $data = [];
  199. foreach ($images as $imageId) {
  200. $data[] = [
  201. 'order_refund_id' => $orderRefundId,
  202. 'image_id' => $imageId,
  203. 'store_id' => self::$storeId
  204. ];
  205. }
  206. return !empty($data) && (new OrderRefundImage)->addAll($data) !== false;
  207. }
  208. }