PointRollbackService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/9/1 下午4:33
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\common\services\finance;
  10. use app\common\facades\Setting;
  11. use app\common\models\Order;
  12. class PointRollbackService
  13. {
  14. /**
  15. * @var Order
  16. */
  17. private $orderModel;
  18. /**
  19. * @param $event
  20. * @throws \app\common\exceptions\ShopException
  21. */
  22. public function orderCancel($event)
  23. {
  24. // 执行订单赠送积分扣除
  25. (new PointRefund())->exec($event);
  26. if (!Setting::get('point.set.point_rollback')) {
  27. return;
  28. }
  29. $this->orderModel = $event->getOrderModel();
  30. $this->rollbackCoinExchange();
  31. $pointDeduction = $this->getOrderPointDeduction();
  32. if (!$pointDeduction) {
  33. return;
  34. }
  35. $this->pointRollback($pointDeduction);
  36. /**
  37. * 返还全额抵扣的积分
  38. */
  39. return;
  40. }
  41. /**
  42. * 返还全额抵扣积分
  43. * @throws \app\common\exceptions\ShopException
  44. */
  45. private function rollbackCoinExchange()
  46. {
  47. $point = $this->orderModel->orderCoinExchanges->where('code', 'point')->first();
  48. if (!$point) {
  49. return;
  50. }
  51. $coin = $point['coin'];
  52. $data = [
  53. 'point_income_type' => PointService::POINT_INCOME_GET,
  54. 'point_mode' => PointService::POINT_MODE_ROLLBACK,
  55. 'member_id' => $this->orderModel->uid,
  56. 'order_id' => $this->orderModel->id,
  57. 'point' => $coin,
  58. 'remark' => '订单:[' . $this->orderModel->order_sn . ']关闭,返还积分抵扣积分' . $coin,
  59. ];
  60. (new PointService($data))->changePoint();
  61. }
  62. private function getOrderPointDeduction()
  63. {
  64. $point = 0;
  65. if ($this->orderModel->deductions) {
  66. foreach ($this->orderModel->deductions as $key => $deduction) {
  67. if ($deduction['code'] == 'point') {
  68. $point = $deduction['coin'];
  69. break;
  70. }
  71. }
  72. }
  73. return $point;
  74. }
  75. private function pointRollback($point)
  76. {
  77. return (new PointService($this->getChangeData($point)))->changePoint();
  78. }
  79. private function getChangeData($point)
  80. {
  81. return [
  82. 'point_income_type' => PointService::POINT_INCOME_GET,
  83. 'point_mode' => PointService::POINT_MODE_ROLLBACK,
  84. 'member_id' => $this->orderModel->uid,
  85. 'order_id' => $this->orderModel->id,
  86. 'point' => $point,
  87. 'remark' => '订单:[' . $this->orderModel->order_sn . ']关闭,返还积分抵扣积分' . $point,
  88. ];
  89. }
  90. }