Active.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 'sortPrice' => false, // 价格排序 高低
  91. 'listRows' => 15, // 每页数量
  92. ], $param);
  93. // 排序规则
  94. if ($params['sortType'] === 'all') {
  95. $this->order(['sort' => 'asc', $this->getPk() => 'desc']);
  96. } elseif ($params['sortType'] === 'sales') {
  97. $this->order(['active_sales' => 'desc']);
  98. } elseif ($params['sortType'] === 'price') {
  99. $this->order(['floor_price' => $params['sortPrice'] ? 'desc' : 'asc']);
  100. }
  101. // 砍价活动列表
  102. $list = $this->field(['*', '(actual_sales + initial_sales) as active_sales'])
  103. ->where('start_time', '<=', time())
  104. ->where('end_time', '>=', time())
  105. ->where('status', '=', 1)
  106. ->where('is_delete', '=', 0)
  107. ->order(['sort' => 'asc', $this->getPk() => 'desc'])
  108. ->paginate($params['listRows'], false, [
  109. 'query' => \request()->request()
  110. ]);
  111. // 设置商品数据
  112. $list = GoodsService::setGoodsData($list);
  113. // 整理正在砍价的助力信息
  114. $list = $this->setHelpsData($list);
  115. return $list;
  116. }
  117. /**
  118. * 整理正在砍价的助力信息
  119. * @param $list
  120. * @return mixed
  121. * @throws \think\Exception
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. */
  126. private function setHelpsData($list)
  127. {
  128. $model = new TaskHelpModel;
  129. foreach ($list as &$item) {
  130. $item['helps'] = $model->getHelpListByActiveId($item['active_id']);
  131. $item['helps_count'] = $model->getHelpCountByActiveId($item['active_id']);
  132. }
  133. return $list;
  134. }
  135. /**
  136. * 获取砍价活动详情
  137. * @param $activeId
  138. * @return Active|bool|null
  139. * @throws \think\exception\DbException
  140. */
  141. public function getDetail($activeId)
  142. {
  143. $model = static::detail($activeId);
  144. if (empty($model) || $model['is_delete'] == true || $model['status'] == false) {
  145. $this->error = '很抱歉,该砍价商品不存在或已下架';
  146. return false;
  147. }
  148. return $model;
  149. }
  150. /**
  151. * 获取用户是否正在参与改砍价活动,如果已参与则返回task_id
  152. * @param $activeId
  153. * @param bool $user
  154. * @return bool|int
  155. */
  156. public function getWhetherPartake($activeId, $user = false)
  157. {
  158. if ($user === false) {
  159. return false;
  160. }
  161. return TaskModel::getHandByUser($activeId, $user['user_id']);
  162. }
  163. /**
  164. * 累计活动销量(实际)
  165. * @return int|true
  166. * @throws \think\Exception
  167. */
  168. public function setIncSales()
  169. {
  170. return $this->setInc('actual_sales');
  171. }
  172. }