Task.php 11 KB

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