Task.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace app\api\model\bargain;
  3. use app\api\model\Goods as GoodsModel;
  4. use app\api\model\bargain\Active as ActiveModel;
  5. use app\common\model\bargain\Task as TaskModel;
  6. use app\api\model\bargain\TaskHelp as TaskHelpModel;
  7. use app\api\service\bargain\Amount as AmountService;
  8. use app\common\service\Goods as GoodsService;
  9. use app\common\library\helper;
  10. /**
  11. * 砍价任务模型
  12. * Class Task
  13. * @package app\api\model\bargain
  14. */
  15. class Task extends TaskModel
  16. {
  17. /**
  18. * 隐藏的字段
  19. * @var array
  20. */
  21. protected $hidden = [
  22. 'peoples',
  23. 'section',
  24. 'is_delete',
  25. 'wxapp_id',
  26. 'create_time',
  27. 'update_time',
  28. ];
  29. /**
  30. * 我的砍价列表
  31. * @param $userId
  32. * @return \think\Paginator
  33. * @throws \think\exception\DbException
  34. */
  35. public function getMyList($userId)
  36. {
  37. // 砍价活动列表
  38. $list = $this->where('user_id', '=', $userId)
  39. ->where('is_delete', '=', 0)
  40. ->order(['create_time' => 'desc'])
  41. ->paginate(5, false, [
  42. 'query' => \request()->request()
  43. ]);
  44. // 设置商品数据
  45. $list = GoodsService::setGoodsData($list);
  46. return $list;
  47. }
  48. /**
  49. * 获取砍价任务详情
  50. * @param $taskId
  51. * @param bool $user
  52. * @return array|bool
  53. * @throws \think\exception\DbException
  54. */
  55. public function getTaskDetail($taskId, $user = false)
  56. {
  57. // 砍价任务详情
  58. $task = static::detail($taskId, ['user']);
  59. if (empty($task)) {
  60. $this->error = '砍价任务不存在';
  61. return false;
  62. }
  63. // 砍价活动详情
  64. $active = ActiveModel::detail($task['active_id']);
  65. // 砍价商品详情
  66. $goods = GoodsModel::detail($task['goods_id']);
  67. // 商品sku信息
  68. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $task['spec_sku_id']);
  69. // 好友助力榜
  70. $help_list = TaskHelpModel::getListByTaskId($taskId);
  71. // 当前是否为发起人
  72. $is_creater = $this->isCreater($task, $user);
  73. // 当前是否已砍
  74. $is_cut = $this->isCut($help_list, $user);
  75. return compact('task', 'is_creater', 'is_cut', 'active', 'goods', 'help_list');
  76. }
  77. /**
  78. * 获取砍价任务的商品列表(用于订单结算)
  79. * @param $taskId
  80. * @return array|bool
  81. * @throws \think\Exception
  82. * @throws \think\exception\DbException
  83. */
  84. public function getTaskGoods($taskId)
  85. {
  86. // 砍价任务详情
  87. $task = static::detail($taskId);
  88. if (empty($task) || $task['is_delete'] || $task['status'] == false) {
  89. $this->error = '砍价任务不存在或已结束';
  90. return false;
  91. }
  92. if ($task['is_buy'] == true) {
  93. $this->error = '该砍价商品已购买';
  94. return false;
  95. }
  96. // 砍价商品详情
  97. $goods = GoodsModel::detail($task['goods_id']);
  98. // 商品sku信息
  99. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $task['spec_sku_id']);
  100. // 商品列表
  101. $goodsList = [$goods->hidden(['category', 'content', 'image', 'sku'])];
  102. foreach ($goodsList as &$item) {
  103. // 商品单价
  104. $item['goods_price'] = $task['actual_price'];
  105. // 商品购买数量
  106. $item['total_num'] = 1;
  107. $item['spec_sku_id'] = $item['goods_sku']['spec_sku_id'];
  108. // 商品购买总金额
  109. $item['total_price'] = $task['actual_price'];
  110. }
  111. return $goodsList;
  112. }
  113. /**
  114. * 订单创建后将砍价任务结束
  115. * @return false|int
  116. */
  117. public function setTaskEnd()
  118. {
  119. return $this->save(['status' => 0]);
  120. }
  121. /**
  122. * 获取用户是否正在参与改砍价活动,如果已参与则返回task_id
  123. * @param $activeId
  124. * @param $userId
  125. * @return bool|int
  126. */
  127. public static function getHandByUser($activeId, $userId)
  128. {
  129. $taskId = (new static)->where('active_id', '=', $activeId)
  130. ->where('user_id', '=', $userId)
  131. ->where('end_time', '>', time())
  132. ->where('status', '=', 1)
  133. ->where('is_delete', '=', 0)
  134. ->value('task_id');
  135. return $taskId ?: false;
  136. }
  137. /**
  138. * 新增砍价任务
  139. * @param $userId
  140. * @param $activeId
  141. * @param $goodsSkuId
  142. * @return bool
  143. * @throws \think\exception\DbException
  144. * @throws \Exception
  145. */
  146. public function partake($userId, $activeId, $goodsSkuId)
  147. {
  148. // 获取活动详情
  149. if (!$active = $this->getActiveDetail($activeId)) {
  150. return false;
  151. }
  152. // 验证能否创建砍价任务
  153. if (!$this->onVerify($active, $userId)) {
  154. return false;
  155. }
  156. // 获取商品详情
  157. $goods = GoodsModel::detail($active['goods_id']);
  158. // 商品sku信息
  159. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $goodsSkuId);
  160. // 事务处理
  161. return $this->transaction(function () use ($userId, $active, $goodsSkuId, $goods) {
  162. // 创建砍价任务
  163. $this->add($userId, $active, $goodsSkuId, $goods);
  164. // 发起人自砍一刀
  165. $active['is_self_cut'] && $this->onCutEvent($userId, true);
  166. return true;
  167. });
  168. }
  169. /**
  170. * 帮砍一刀
  171. * @param $user
  172. * @return bool|false|int
  173. */
  174. public function helpCut($user)
  175. {
  176. // 好友助力榜
  177. $helpList = TaskHelpModel::getListByTaskId($this['task_id']);
  178. // 当前是否已砍
  179. if ($this->isCut($helpList, $user)) {
  180. $this->error = '您已参与砍价,请不要重复操作';
  181. return false;
  182. }
  183. // 帮砍一刀事件
  184. return $this->transaction(function () use ($user) {
  185. return $this->onCutEvent($user['user_id'], $this->isCreater($this, $user));
  186. });
  187. }
  188. /**
  189. * 砍一刀的金额
  190. * @return mixed
  191. */
  192. public function getCutMoney()
  193. {
  194. return $this['section'][$this['cut_people']];
  195. }
  196. /**
  197. * 帮砍一刀事件
  198. * @param $userId
  199. * @param bool $isCreater
  200. * @return false|int
  201. */
  202. private function onCutEvent($userId, $isCreater = false)
  203. {
  204. // 砍价金额
  205. $cutMoney = $this->getCutMoney();
  206. // 砍价助力记录
  207. $model = new TaskHelpModel;
  208. $model->add($this, $userId, $cutMoney, $isCreater);
  209. // 实际购买金额
  210. $actualPrice = helper::bcsub($this['actual_price'], $cutMoney);
  211. // 更新砍价任务信息
  212. $this->save([
  213. 'cut_people' => ['inc', 1],
  214. 'cut_money' => ['inc', $cutMoney],
  215. 'actual_price' => $actualPrice,
  216. 'is_floor' => helper::bcequal($actualPrice, $this['floor_price']),
  217. ]);
  218. return true;
  219. }
  220. /**
  221. * 创建砍价任务记录
  222. * @param $userId
  223. * @param $active
  224. * @param $goodsSkuId
  225. * @param $goods
  226. * @return false|int
  227. * @throws \Exception
  228. */
  229. private function add($userId, $active, $goodsSkuId, $goods)
  230. {
  231. // 分配砍价金额区间
  232. $section = $this->calcBargainSection(
  233. $goods['goods_sku']['goods_price'],
  234. $active['floor_price'],
  235. $active['peoples']
  236. );
  237. // 新增记录
  238. return $this->save([
  239. 'active_id' => $active['active_id'],
  240. 'user_id' => $userId,
  241. 'goods_id' => $active['goods_id'],
  242. 'spec_sku_id' => $goodsSkuId,
  243. 'goods_price' => $goods['goods_sku']['goods_price'],
  244. 'floor_price' => $active['floor_price'],
  245. 'peoples' => $active['peoples'],
  246. 'cut_people' => 0,
  247. 'section' => $section,
  248. 'cut_money' => 0.00,
  249. 'actual_price' => $goods['goods_sku']['goods_price'],
  250. 'end_time' => time() + ($active['expiryt_time'] * 3600),
  251. 'is_buy' => 0,
  252. 'status' => 1,
  253. 'wxapp_id' => static::$wxapp_id,
  254. ]);
  255. }
  256. /**
  257. * 砍价任务标记为已购买
  258. * @return false|int
  259. */
  260. public function setIsBuy()
  261. {
  262. return $this->save(['is_buy' => 1]);
  263. }
  264. /**
  265. * 分配砍价金额区间
  266. * @param $goodsPrice
  267. * @param $floorPrice
  268. * @param $peoples
  269. * @return mixed
  270. * @throws \Exception
  271. */
  272. private function calcBargainSection($goodsPrice, $floorPrice, $peoples)
  273. {
  274. $AmountService = new AmountService(helper::bcsub($goodsPrice, $floorPrice), $peoples);
  275. return $AmountService->handle()['items'];
  276. }
  277. /**
  278. * 当前是否为发起人
  279. * @param $task
  280. * @param $user
  281. * @return bool
  282. */
  283. private function isCreater($task, $user)
  284. {
  285. if ($user === false) return false;
  286. return $user['user_id'] == $task['user_id'];
  287. }
  288. /**
  289. * 当前是否已砍
  290. * @param $helpList
  291. * @param $user
  292. * @return bool
  293. */
  294. private function isCut($helpList, $user)
  295. {
  296. if ($user === false) return false;
  297. foreach ($helpList as $item) {
  298. if ($item['user_id'] == $user['user_id']) return true;
  299. }
  300. return false;
  301. }
  302. /**
  303. * 获取活动详情
  304. * @param $activeId
  305. * @return Active|bool|null
  306. * @throws \think\exception\DbException
  307. */
  308. private function getActiveDetail($activeId)
  309. {
  310. // 获取活动详情
  311. $ActiveModel = new ActiveModel;
  312. $detail = $ActiveModel->getDetail($activeId);
  313. // 活动详情不存在
  314. if ($detail === false) {
  315. $this->error = $ActiveModel->getError();
  316. return false;
  317. }
  318. return $detail;
  319. }
  320. /**
  321. * 验证能否创建砍价任务
  322. * @param $active
  323. * @param $userId
  324. * @return bool
  325. */
  326. private function onVerify($active, $userId)
  327. {
  328. // 活动是否开始
  329. if (!$active['is_start']) {
  330. $this->error = '很抱歉,当前砍价活动未开始';
  331. return false;
  332. }
  333. // 活动是否到期合法
  334. if ($active['is_end']) {
  335. $this->error = '很抱歉,当前砍价活动已结束';
  336. return false;
  337. }
  338. // 判断当前用户是否已参加
  339. $taskId = static::getHandByUser($active['active_id'], $userId);
  340. if ($taskId !== false && $taskId > 0) {
  341. $this->error = '很抱歉,当前砍价活动您已参加,无需重复参与';
  342. return false;
  343. }
  344. return true;
  345. }
  346. }