TaskHelp.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\api\model\bargain;
  3. use app\common\model\bargain\TaskHelp as TaskHelpModel;
  4. /**
  5. * 砍价任务助力记录模型
  6. * Class TaskHelp
  7. * @package app\api\model\bargain
  8. */
  9. class TaskHelp extends TaskHelpModel
  10. {
  11. /**
  12. * 隐藏的字段
  13. * @var array
  14. */
  15. protected $hidden = [
  16. 'wxapp_id',
  17. 'create_time',
  18. ];
  19. /**
  20. * 获取助力列表记录
  21. * @param $taskId
  22. * @return false|\PDOStatement|string|\think\Collection
  23. */
  24. public static function getListByTaskId($taskId)
  25. {
  26. // 获取列表数据
  27. $list = (new static)->with(['user'])
  28. ->where('task_id', '=', $taskId)
  29. ->order(['create_time' => 'desc'])
  30. ->select();
  31. // 隐藏会员昵称
  32. foreach ($list as &$item) {
  33. $item['user']['nickName'] = \substr_cut($item['user']['nickName']);
  34. }
  35. return $list;
  36. }
  37. /**
  38. * 新增记录
  39. * @param $task
  40. * @param $userId
  41. * @param $cutMoney
  42. * @param $isCreater
  43. * @return false|int
  44. */
  45. public function add($task, $userId, $cutMoney, $isCreater = false)
  46. {
  47. return $this->save([
  48. 'task_id' => $task['task_id'],
  49. 'active_id' => $task['active_id'],
  50. 'user_id' => $userId,
  51. 'cut_money' => $cutMoney,
  52. 'is_creater' => $isCreater,
  53. 'wxapp_id' => static::$wxapp_id,
  54. ]);
  55. }
  56. /**
  57. * 根据砍价活动id获取正在砍价的助力信息列表
  58. * @param $activeId
  59. * @return false|\PDOStatement|string|\think\Collection
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @throws \think\exception\DbException
  63. */
  64. public function getHelpListByActiveId($activeId)
  65. {
  66. return $this
  67. ->with('user') // todo: 废弃
  68. ->alias('help')
  69. ->field(['help.user_id', 'user.nickName', 'user.avatarUrl'])
  70. ->join('user', 'user.user_id = help.user_id')
  71. ->join('bargain_task task', 'task.task_id = help.task_id')
  72. ->where('help.active_id', '=', $activeId)
  73. // is_creater 只统计发起人
  74. // ->where('help.is_creater', '=', 1)
  75. ->where('task.status', '=', 1)
  76. ->where('task.is_delete', '=', 0)
  77. ->group('help.user_id')
  78. ->limit(5)
  79. ->select();
  80. }
  81. /**
  82. * 根据砍价活动id获取正在砍价的助力人数
  83. * @param $activeId
  84. * @return int|string
  85. * @throws \think\Exception
  86. */
  87. public function getHelpCountByActiveId($activeId)
  88. {
  89. return $this->alias('help')
  90. ->join('user', 'user.user_id = help.user_id')
  91. ->join('bargain_task task', 'task.task_id = help.task_id')
  92. ->where('help.active_id', '=', $activeId)
  93. // is_creater 只统计发起人
  94. // ->where('help.is_creater', '=', 1)
  95. ->where('task.status', '=', 1)
  96. ->where('task.is_delete', '=', 0)
  97. ->group('help.user_id')
  98. ->count();
  99. }
  100. }