PointQueue.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2019/3/31
  5. * Time: 9:04 PM
  6. */
  7. namespace app\common\models\finance;
  8. use app\common\models\BaseModel;
  9. use app\common\models\Order;
  10. use app\common\services\finance\PointService;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use app\common\models\Member;
  13. class PointQueue extends BaseModel
  14. {
  15. public $table = 'yz_point_queue';
  16. public $timestamps = true;
  17. protected $guarded = [''];
  18. protected $appends = [
  19. 'status_name'
  20. ];
  21. const STATUS_FINISH = 1;
  22. const STATUS_RUNING = 0;
  23. public static function getList($search)
  24. {
  25. return self::select()
  26. ->with([
  27. 'member' => function ($member) {
  28. $member->select(['uid', 'nickname', 'realname', 'avatar', 'mobile']);
  29. },
  30. 'order' => function ($order) {
  31. $order->select(['id', 'order_sn']);
  32. }
  33. ])
  34. ->search($search);
  35. }
  36. public function scopeSearch($query, $search)
  37. {
  38. if ($search['uid']) {
  39. $query->where('uid', $search['uid']);
  40. }
  41. if ($search['member']) {
  42. $query->whereHas('member', function ($member) use ($search) {
  43. $member->select('uid', 'nickname', 'realname', 'mobile', 'avatar')
  44. ->where('realname', 'like', '%' . $search['member'] . '%')
  45. ->orWhere('mobile', 'like', '%' . $search['member'] . '%')
  46. ->orWhere('nickname', 'like', '%' . $search['member'] . '%');
  47. });
  48. }
  49. if ($search['queue_id']) {
  50. $query->where('id', $search['queue_id']);
  51. }
  52. return $query;
  53. }
  54. public function getStatusNameAttribute()
  55. {
  56. $statusName = '奖励中';
  57. if ($this->status == self::STATUS_FINISH) {
  58. $statusName = '已完成';
  59. }
  60. return $statusName;
  61. }
  62. public function member()
  63. {
  64. return $this->hasOne(Member::class, 'uid', 'uid');
  65. }
  66. public function order()
  67. {
  68. return $this->hasOne(Order::class, 'id', 'order_id');
  69. }
  70. public static function handle($orderModel, $goodsSale, $pointTotal)
  71. {
  72. if ($pointTotal <= 0) {
  73. return;
  74. }
  75. $data = [
  76. 'uniacid' => $orderModel->uniacid,
  77. 'uid' => $orderModel->uid,
  78. 'order_id' => $orderModel->id,
  79. 'goods_id' => $goodsSale->goods_id,
  80. 'point_total' => $pointTotal,
  81. 'finish_point' => 0,
  82. 'surplus_point' => $pointTotal,
  83. 'once_unit' => $goodsSale->max_once_point,
  84. 'last_point' => 0,
  85. 'status' => self::STATUS_RUNING
  86. ];
  87. self::store($data);
  88. }
  89. public static function store($data)
  90. {
  91. $model = new self();
  92. $model->fill($data);
  93. $model->save();
  94. // 消息通知 暂无
  95. }
  96. public static function returnRun($queueModel)
  97. {
  98. // 单期奖励积分数量
  99. $amount = $queueModel->once_unit;
  100. if ($queueModel->surplus_point - $amount < 0) {
  101. $amount = $queueModel->surplus_point;
  102. $queueModel->status = PointQueue::STATUS_FINISH;
  103. }
  104. $queueModel->last_point = $amount;
  105. $queueModel->surplus_point -= $amount;
  106. $queueModel->finish_point += $amount;
  107. // 修改队列
  108. $queueModel->save();
  109. // 奖励记录
  110. $log = [
  111. 'uniacid' => $queueModel->uniacid,
  112. 'uid' => $queueModel->uid,
  113. 'queue_id' => $queueModel->id,
  114. 'amount' => $amount,
  115. 'point_total' => $queueModel->point_total,
  116. 'finish_point' => $queueModel->finish_point,
  117. 'surplus_point' => $queueModel->surplus_point
  118. ];
  119. PointQueueLog::store($log);
  120. // 奖励通知 暂无
  121. // 发放奖励到会员
  122. $remark = "queue_id[{$queueModel->id}]";
  123. (new PointService([
  124. 'point_income_type' => PointService::POINT_INCOME_GET,
  125. 'point_mode' => PointService::POINT_MODE_GOODS,
  126. 'member_id' => $queueModel->uid,
  127. 'point' => $amount,
  128. 'remark' => $remark
  129. ]))->changePoint();
  130. }
  131. public static function boot()
  132. {
  133. parent::boot();
  134. static::addGlobalScope(function (Builder $builder) {
  135. $builder->uniacid();
  136. });
  137. }
  138. }