OrderRefund.php 7.5 KB

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