RefundOperation.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/12/22
  8. * Time: 13:58
  9. */
  10. namespace app\backend\modules\refund\services\operation;
  11. use app\common\events\Event;
  12. use app\common\models\OrderGoods;
  13. use app\common\models\refund\RefundGoodsLog;
  14. use app\common\modules\refund\RefundOrderFactory;
  15. use app\framework\Http\Request;
  16. use app\frontend\modules\refund\services\RefundBackWayService;
  17. use Illuminate\Support\Facades\DB;
  18. use app\common\exceptions\AppException;
  19. use app\common\models\refund\RefundApply;
  20. abstract class RefundOperation extends RefundApply
  21. {
  22. protected $transaction = false;
  23. protected $statusBeforeChange = [];
  24. //改变后状态
  25. protected $statusAfterChanged;
  26. protected $name = ''; //操作名称
  27. protected $timeField = ''; //操作时间
  28. protected $refundOrderType;//售后订单类型
  29. protected $port_type = 'frontend'; //frontend = 前端、backend = 后端
  30. public $params;//参数,暂时没用
  31. protected $backWay;
  32. /**
  33. * 表更新后
  34. */
  35. abstract protected function updateAfter();
  36. /**
  37. * 操作日志记录
  38. */
  39. abstract protected function writeLog();
  40. /**
  41. * 表更新前
  42. */
  43. abstract protected function updateBefore();
  44. /**
  45. * 发送通知
  46. */
  47. protected function sendMessage() {}
  48. /**
  49. * 退款操作事件
  50. * @return Event|array|null
  51. */
  52. protected function afterEventClass()
  53. {
  54. return null;
  55. }
  56. /**
  57. * 必须要在触发完退款操作事件,才能去操作订单。
  58. * 因为订单状态改变会触发订单事件
  59. */
  60. protected function triggerEventAfter()
  61. {
  62. }
  63. /**
  64. * 更新申请表
  65. * @return bool
  66. */
  67. protected function updateTable() {
  68. if (isset($this->statusAfterChanged)) {
  69. $this->status = $this->statusAfterChanged;
  70. }
  71. if(!empty($this->timeField)){
  72. $timeFields = $this->timeField;
  73. $this->$timeFields = time();
  74. }
  75. return $this->save();
  76. }
  77. /**
  78. * 操作验证
  79. * @throws AppException
  80. */
  81. protected function operationValidate()
  82. {
  83. if (!empty($this->statusBeforeChange) && !in_array($this->status, $this->statusBeforeChange)) {
  84. throw new AppException($this->status_name . '的退款申请,无法执行' . $this->name . '操作');
  85. }
  86. }
  87. protected function backWay()
  88. {
  89. if (!isset($this->backWay)) {
  90. $this->backWay = RefundBackWayService::getBackWayClass($this->refund_way_type);
  91. $this->backWay->setRefundApply($this);
  92. }
  93. return $this->backWay;
  94. }
  95. protected function setBackWay()
  96. {
  97. $this->backWay()->saveRelation();
  98. }
  99. /***
  100. * 执行操作操作
  101. * @return bool
  102. * @throws AppException
  103. */
  104. final public function execute()
  105. {
  106. $this->operationValidate(); //验证
  107. $this->updateBefore(); //更新表之前
  108. $result = $this->updateTable();//更新表
  109. if (!$result) {
  110. throw new AppException('信息更新失败');
  111. }
  112. $this->updateAfter();//更新表之后
  113. $this->writeLog(); //写入售后协商记录表
  114. $this->triggerRefundEvent(); //售后事件触发
  115. $this->triggerEventAfter();//事件触发后
  116. $this->sendMessage(); //发送通知
  117. return $result;
  118. }
  119. /**
  120. * 发布监听
  121. */
  122. final protected function triggerRefundEvent()
  123. {
  124. $eventClass = $this->afterEventClass();
  125. if (is_array($eventClass)) {
  126. foreach ($eventClass as $itemEvent) {
  127. if (!is_null($itemEvent) && ($itemEvent instanceof Event)) {
  128. event($itemEvent);
  129. }
  130. }
  131. } else {
  132. if (!is_null($eventClass) && ($eventClass instanceof Event)) {
  133. event($eventClass);
  134. }
  135. }
  136. }
  137. final public function setParams($request)
  138. {
  139. $this->params = $request;
  140. }
  141. final public function getParam($key)
  142. {
  143. return array_get($this->params, $key, '');
  144. }
  145. final public function getParams()
  146. {
  147. return $this->params;
  148. }
  149. final public function getRequest()
  150. {
  151. // if (!isset($this->request)) {
  152. // $this->request = request();
  153. // }
  154. return request();
  155. }
  156. //对应的订单
  157. final public function relatedPluginOrder()
  158. {
  159. if (!isset($this->refundOrderType)) {
  160. $this->refundOrderType = RefundOrderFactory::getInstance()->getRefundOrder($this->order, $this->port_type);
  161. }
  162. return $this->refundOrderType;
  163. }
  164. /**
  165. * 售后申请记录退款商品
  166. * @param $refundGoods
  167. */
  168. protected function createOrderGoodsRefundLog($refundGoods)
  169. {
  170. $order_goods_ids = array_column($refundGoods, 'id');
  171. OrderGoods::whereIn('id', $order_goods_ids)->update(['refund_id' => $this->id]);
  172. foreach ($refundGoods as $goodsItem) {
  173. RefundGoodsLog::saveData($this,$goodsItem);
  174. }
  175. }
  176. /**
  177. * 更新订单商品表售后状态
  178. */
  179. protected function updateOrderGoodsRefundStatus()
  180. {
  181. //不是换货售后商品需要标识下退过款
  182. if ($this->refund_type != self::REFUND_TYPE_EXCHANGE_GOODS) {
  183. //is_refund 字段现在作用只是标记下该商品售后过
  184. $updateData['is_refund'] = DB::raw('is_refund + 1');
  185. }
  186. $updateData['refund_id'] = 0;
  187. OrderGoods::where('refund_id', $this->id)->update($updateData);
  188. }
  189. protected function delRefundOrderGoodsLog()
  190. {
  191. RefundGoodsLog::where('refund_id', $this->id)->delete();
  192. //更新订单商品表售后字段
  193. OrderGoods::where('refund_id', $this->id)->update(['refund_id'=> 0]);
  194. }
  195. protected function cancelRefund()
  196. {
  197. return $this->order->cancelRefund();
  198. }
  199. protected function closeOrder()
  200. {
  201. return $this->order->close();
  202. }
  203. /**
  204. * @param $eventClass
  205. * @param mixed ...$parameter
  206. */
  207. protected function afterEvent($eventClass, ...$parameter)
  208. {
  209. event(new $eventClass(...$parameter));
  210. }
  211. }