OrderRefund.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 cores\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): string
  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 $state 售后单状态
  78. * @return \think\Paginator
  79. * @throws BaseException
  80. * @throws \think\db\exception\DbException
  81. */
  82. public function getList(int $state = -1): \think\Paginator
  83. {
  84. // 检索查询条件
  85. $filter = [];
  86. // 售后单状态
  87. $state > -1 && $filter[] = ['status', '=', $state];
  88. // 当前用户ID
  89. $userId = UserService::getCurrentLoginUserId();
  90. // 查询列表记录
  91. return $this->with(['orderGoods.image'])
  92. ->where($filter)
  93. ->where('user_id', '=', $userId)
  94. ->order(['create_time' => 'desc'])
  95. ->paginate(15);
  96. }
  97. /**
  98. * 获取当前用户的售后单详情
  99. * @param int $orderRefundId 售后单ID
  100. * @param bool $isWith 是否关联
  101. * @return static|null
  102. * @throws BaseException
  103. */
  104. public static function getDetail(int $orderRefundId, bool $isWith = false): ?self
  105. {
  106. // 关联查询
  107. $with = $isWith ? ['orderGoods' => ['image'], 'images.file', 'address', 'express'] : [];
  108. // 获取记录
  109. $detail = static::detail([
  110. 'user_id' => UserService::getCurrentLoginUserId(),
  111. 'order_refund_id' => $orderRefundId
  112. ], $with);
  113. empty($detail) && throwError('未找到该售后单');
  114. return $detail;
  115. }
  116. /**
  117. * 获取当前用户的售后单数量(进行中的)
  118. * @return int
  119. * @throws BaseException
  120. */
  121. public static function getCountByUnderway(): int
  122. {
  123. $userId = UserService::getCurrentLoginUserId();
  124. return (new static)->where('user_id', '=', $userId)
  125. ->where('status', '=', 0)
  126. ->count();
  127. }
  128. /**
  129. * 订单商品详情
  130. * @param int $orderGoodsId
  131. * @return OrderGoods|array|null
  132. * @throws BaseException
  133. */
  134. public function getRefundGoods(int $orderGoodsId)
  135. {
  136. $goods = OrderGoodsModel::detail($orderGoodsId);
  137. if (isset($goods['refund']) && !empty($goods['refund'])) {
  138. throwError('当前商品已申请售后');
  139. }
  140. return $goods;
  141. }
  142. /**
  143. * 用户发货
  144. * @param array $data
  145. * @return bool|false
  146. */
  147. public function delivery(array $data): bool
  148. {
  149. if (
  150. $this['type'] != RefundTypeEnum::RETURN
  151. || $this['audit_status'] != AuditStatusEnum::REVIEWED
  152. || $this['is_user_send'] != 0
  153. ) {
  154. $this->error = '当前售后单不合法,不允许该操作';
  155. return false;
  156. }
  157. if ($data['expressId'] <= 0) {
  158. $this->error = '请选择物流公司';
  159. return false;
  160. }
  161. if (empty($data['expressNo'])) {
  162. $this->error = '请填写物流单号';
  163. return false;
  164. }
  165. return $this->save([
  166. 'is_user_send' => 1,
  167. 'send_time' => time(),
  168. 'express_id' => (int)$data['expressId'],
  169. 'express_no' => $data['expressNo'],
  170. ]);
  171. }
  172. /**
  173. * 新增售后单记录
  174. * @param int $orderGoodsId 订单商品ID
  175. * @param array $data 用户提交的表单数据
  176. * @return mixed
  177. * @throws BaseException
  178. */
  179. public function apply(int $orderGoodsId, array $data)
  180. {
  181. // 订单商品详情
  182. $goods = $this->getRefundGoods($orderGoodsId);
  183. return $this->transaction(function () use ($orderGoodsId, $data, $goods) {
  184. // 新增售后单记录
  185. $this->save([
  186. 'order_goods_id' => $orderGoodsId,
  187. 'order_id' => $goods['order_id'],
  188. 'user_id' => UserService::getCurrentLoginUserId(),
  189. 'type' => $data['type'],
  190. 'apply_desc' => $data['content'],
  191. 'audit_status' => AuditStatusEnum::WAIT,
  192. 'status' => 0,
  193. 'store_id' => self::$storeId
  194. ]);
  195. // 记录凭证图片关系
  196. if (isset($data['images']) && !empty($data['images'])) {
  197. $this->saveImages((int)$this['order_refund_id'], $data['images']);
  198. }
  199. return true;
  200. });
  201. }
  202. /**
  203. * 记录售后单图片
  204. * @param int $orderRefundId 售后单ID
  205. * @param array $images 图片列表
  206. * @return void
  207. */
  208. private function saveImages(int $orderRefundId, array $images): void
  209. {
  210. // 生成评价图片数据
  211. $data = [];
  212. foreach ($images as $imageId) {
  213. $data[] = [
  214. 'order_refund_id' => $orderRefundId,
  215. 'image_id' => $imageId,
  216. 'store_id' => self::$storeId
  217. ];
  218. }
  219. !empty($data) && (new OrderRefundImage)->addAll($data) !== false;
  220. }
  221. }