Active.php 5.4 KB

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