MemberPointRepository.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\MembersPoint;
  4. use Prettus\Repository\Eloquent\BaseRepository;
  5. use Prettus\Repository\Criteria\RequestCriteria;
  6. /**
  7. * Class MemberRepositoryEloquent.
  8. *
  9. * @package namespace App\Repositories;
  10. */
  11. class MemberPointRepository extends BaseRepository
  12. {
  13. /**
  14. * Specify Model class name
  15. *
  16. * @return string
  17. */
  18. public function model()
  19. {
  20. return MembersPoint::class;
  21. }
  22. /**
  23. * Boot up the repository, pushing criteria
  24. */
  25. public function boot()
  26. {
  27. $this->pushCriteria(app(RequestCriteria::class));
  28. }
  29. public function getPointsById($uid, $utype)
  30. {
  31. return $this->model->where(['uid'=>$uid,'utype'=>$utype])->first();
  32. }
  33. public function getTaskById($uid, $utype)
  34. {
  35. return $this->with('taskLogs')->model->where(['uid'=>$uid,'utype'=>$utype])->first()->toArray();
  36. }
  37. /**企业会员会员积分增减记录
  38. * @param $uid
  39. * @param $utype
  40. * @return mixed
  41. */
  42. public function getComPointsById($uid, $utype)
  43. {
  44. return $this->with('getMembersHandsel')->model->where(['uid'=>$uid,'utype'=>$utype])->first();
  45. }
  46. /**企业会员points
  47. * @param $uid
  48. * @return mixed
  49. */
  50. public function getComTaskById($uid)
  51. {
  52. return $this->with('taskLogs')->model->where(['uid'=>$uid])->first()->toArray();
  53. }
  54. public function getPointsOne($uid, $utype)
  55. {
  56. return $this->model->where(['uid'=>$uid, 'utype'=>$utype])->first();
  57. }
  58. public function addNew($data)
  59. {
  60. return $this->model->create($data);
  61. }
  62. public function updateNew($uid, $utype, $data)
  63. {
  64. return $this->model->where(['uid'=>$uid, 'utype'=>$utype])->update($data);
  65. }
  66. /**
  67. * @param $uid
  68. * @param int $i_type 1增加 2减少
  69. * @param int $points
  70. * @return mixed
  71. */
  72. public function reportDeal($uid, $utype, $i_type = 1, $points = 0)
  73. {
  74. $userpoints = $this->getPointsOne($uid, $utype);
  75. if (!$userpoints) {
  76. $userpoints = 0;
  77. $this->addNew(['uid'=>$uid,'utype'=>$utype, 'points'=>0]);
  78. }
  79. if ($i_type==1) {
  80. return $this->model->where(['uid'=>$uid,'utype'=>$utype])->increment('points', $points);
  81. }
  82. if ($i_type==2) {
  83. if ($userpoints['points']>$points) {
  84. return $this->model->where(['uid'=>$uid,'utype'=>$utype])->decrement('points', $points);
  85. } else {
  86. return $this->model->where(['uid'=>$uid,'utype'=>$utype])->update(['points'=>0]);
  87. }
  88. }
  89. }
  90. }