Active.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\api\model\bargain;
  3. use app\common\model\bargain\Active as ActiveModel;
  4. use app\api\model\bargain\Task as TaskModel;
  5. use app\api\model\bargain\TaskHelp as TaskHelpModel;
  6. use app\common\service\Goods as GoodsService;
  7. /**
  8. * 砍价活动模型
  9. * Class Active
  10. * @package app\api\model\bargain
  11. */
  12. class Active extends ActiveModel
  13. {
  14. /**
  15. * 隐藏的字段
  16. * @var array
  17. */
  18. protected $hidden = [
  19. 'peoples',
  20. 'is_self_cut',
  21. 'initial_sales',
  22. 'actual_sales',
  23. 'sort',
  24. 'create_time',
  25. 'update_time',
  26. 'wxapp_id',
  27. 'is_delete',
  28. ];
  29. /**
  30. * 获取器:分享标题
  31. * @param $value
  32. * @return mixed
  33. */
  34. public function getShareTitleAttr($value)
  35. {
  36. return htmlspecialchars_decode($value);
  37. }
  38. /**
  39. * 获取器:砍价助力语
  40. * @param $value
  41. * @return mixed
  42. */
  43. public function getPromptWordsAttr($value)
  44. {
  45. return htmlspecialchars_decode($value);
  46. }
  47. /**
  48. * 活动会场列表
  49. * @param array $param
  50. * @return mixed|\think\Paginator
  51. * @throws \think\Exception
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @throws \think\exception\DbException
  55. */
  56. public function getHallList($param = [])
  57. {
  58. return $this->getList($param);
  59. }
  60. /**
  61. * 获取砍价活动列表(根据活动id集)
  62. * @param $activeIds
  63. * @param array $param
  64. * @return mixed|\think\Paginator
  65. * @throws \think\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public function getListByIds($activeIds, $param = [])
  71. {
  72. $this->where('active_id', 'in', $activeIds);
  73. return $this->getList($param);
  74. }
  75. /**
  76. * 获取砍价活动列表
  77. * @param $param
  78. * @return mixed|\think\Paginator
  79. * @throws \think\Exception
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. private function getList($param)
  85. {
  86. // 商品列表获取条件
  87. $params = array_merge([
  88. 'status' => 1, // 商品状态
  89. 'sortType' => 'all', // 排序类型
  90. 'listRows' => 15, // 每页数量
  91. ], $param);
  92. // 排序规则
  93. if ($params['sortType'] === 'all') {
  94. $this->order(['sort' => 'asc']);
  95. } elseif ($params['sortType'] === 'sales') {
  96. $this->order(['active_sales' => 'desc']);
  97. } elseif ($params['sortType'] === 'price') {
  98. $this->order(['floor_price' => $params['sortPrice'] ? 'desc' : 'asc']);
  99. }
  100. // 砍价活动列表
  101. $list = $this->field(['*', '(actual_sales + initial_sales) as active_sales'])
  102. ->where('start_time', '<=', time())
  103. ->where('end_time', '>=', time())
  104. ->where('status', '=', 1)
  105. ->where('is_delete', '=', 0)
  106. ->order(['sort' => 'asc'])
  107. ->paginate($params['listRows'], false, [
  108. 'query' => \request()->request()
  109. ]);
  110. // 设置商品数据
  111. $list = GoodsService::setGoodsData($list);
  112. // 整理正在砍价的助力信息
  113. $list = $this->setHelpsData($list);
  114. return $list;
  115. }
  116. /**
  117. * 整理正在砍价的助力信息
  118. * @param $list
  119. * @return mixed
  120. * @throws \think\Exception
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @throws \think\exception\DbException
  124. */
  125. private function setHelpsData($list)
  126. {
  127. $model = new TaskHelpModel;
  128. foreach ($list as &$item) {
  129. $item['helps'] = $model->getHelpListByActiveId($item['active_id']);
  130. $item['helps_count'] = $model->getHelpCountByActiveId($item['active_id']);
  131. }
  132. return $list;
  133. }
  134. /**
  135. * 获取砍价活动详情
  136. * @param $activeId
  137. * @return Active|bool|null
  138. * @throws \think\exception\DbException
  139. */
  140. public function getDetail($activeId)
  141. {
  142. $model = static::detail($activeId);
  143. if (empty($model) || $model['is_delete'] == true || $model['status'] == false) {
  144. $this->error = '很抱歉,该砍价商品不存在或已下架';
  145. return false;
  146. }
  147. return $model;
  148. }
  149. /**
  150. * 获取用户是否正在参与改砍价活动,如果已参与则返回task_id
  151. * @param $activeId
  152. * @param bool $user
  153. * @return bool|int
  154. */
  155. public function getWhetherPartake($activeId, $user = false)
  156. {
  157. if ($user === false) {
  158. return false;
  159. }
  160. return TaskModel::getHandByUser($activeId, $user['user_id']);
  161. }
  162. /**
  163. * 累计活动销量(实际)
  164. * @return int|true
  165. * @throws \think\Exception
  166. */
  167. public function setIncSales()
  168. {
  169. return $this->setInc('actual_sales');
  170. }
  171. }