StoreBargainUserHelpServices.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\bargain;
  13. use app\services\BaseServices;
  14. use app\dao\activity\bargain\StoreBargainUserHelpDao;
  15. use app\services\user\UserServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. /**
  19. *
  20. * Class StoreBargainUserHelpServices
  21. * @package app\services\activity
  22. * @method getHelpAllCount(array $where)
  23. * @method count(array $where)
  24. */
  25. class StoreBargainUserHelpServices extends BaseServices
  26. {
  27. /**
  28. * StoreBargainUserHelpServices constructor.
  29. * @param StoreBargainUserHelpDao $dao
  30. */
  31. public function __construct(StoreBargainUserHelpDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. // /**
  36. // * TODO 获取用户还剩余的砍价金额
  37. // * @param int $bargainId $bargainId 砍价商品编号
  38. // * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  39. // * @return float
  40. // * @throws \think\db\exception\DataNotFoundException
  41. // * @throws \think\db\exception\ModelNotFoundException
  42. // * @throws \think\exception\DbException
  43. // */
  44. // public function getSurplusPrice($bargainId = 0, $bargainUserUid = 0)
  45. // {
  46. // /** @var StoreBargainServices $bargainUserService */
  47. // $bargainUserService = app()->make(StoreBargainServices::class);
  48. // $bargainUserTableId = $bargainUserService->getBargainUserTableId($bargainId, $bargainUserUid);// TODO 获取用户参与砍价表编号
  49. // $coverPrice = $bargainUserService->getBargainUserDiffPriceFloat($bargainUserTableId);//TODO 获取用户可以砍掉的金额 好友砍价之后获取砍价金额
  50. // $alreadyPrice = $bargainUserService->getBargainUserPrice($bargainUserTableId);//TODO 用户已经砍掉的价格 好友砍价之后获取用户已经砍掉的价格
  51. // $surplusPrice = (float)bcsub((string)$coverPrice, (string)$alreadyPrice, 2);//TODO 用户剩余要砍掉的价格
  52. // return $surplusPrice;
  53. // }
  54. /**
  55. * 获取砍价帮列表
  56. * @param int $bid
  57. * @param int $page
  58. * @param int $limit
  59. * @return array
  60. */
  61. public function getHelpList(int $bid, int $page = 0, int $limit = 0)
  62. {
  63. $list = $this->dao->getHelpList($bid, $page, $limit);
  64. if ($list) {
  65. $ids = array_unique(array_column($list, 'uid'));
  66. /** @var UserServices $userService */
  67. $userService = app()->make(UserServices::class);
  68. $userInfos = $userService->getColumn([['uid', 'in', $ids]], 'nickname,avatar', 'uid');
  69. foreach ($list as $key => &$value) {
  70. $userInfo = $userInfos[$value['uid']] ?? [];
  71. if ($userInfo) {
  72. $value['nickname'] = $userInfo['nickname'];
  73. $value['avatar'] = $userInfo['avatar'];
  74. } else {
  75. $value['nickname'] = '此用户已失效';
  76. $value['avatar'] = '';
  77. }
  78. unset($value['id']);
  79. }
  80. }
  81. return array_values($list);
  82. }
  83. /**
  84. * 判断是否能砍价
  85. * @param $bargainId
  86. * @param $bargainUserTableId
  87. * @param $uid
  88. * @return bool
  89. */
  90. public function isBargainUserHelpCount($bargainId, $bargainUserTableId, $uid)
  91. {
  92. $count = $this->dao->count(['bargain_id' => $bargainId, 'bargain_user_id' => $bargainUserTableId, 'uid' => $uid]);
  93. if (!$count) return true;
  94. else return false;
  95. }
  96. /**
  97. * 用户砍价,写入砍价记录
  98. * @param $uid
  99. * @param $bargainUserInfo
  100. * @param $bargainInfo
  101. * @return false|string
  102. */
  103. public function setBargainRecord($uid, $bargainUserInfo, $bargainInfo)
  104. {
  105. //已经参与砍价的人数
  106. $people = $this->dao->count(['bargain_user_id' => $bargainUserInfo['id']]);
  107. //剩余砍价金额
  108. $coverPrice = bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['bargain_price_min'], 2);
  109. $surplusPrice = bcsub((string)$coverPrice, (string)$bargainUserInfo['price'], 2);//TODO 用户剩余要砍掉的价格
  110. if (0.00 === (float)$surplusPrice) throw new ApiException(410299);
  111. if (($bargainInfo['people_num'] - $people) == 1) {
  112. $price = $surplusPrice;
  113. } else {
  114. /** @var UserServices $userServices */
  115. $userServices = app()->make(UserServices::class);
  116. $userInfo = $userServices->get($uid);
  117. $price = $this->randomFloat($surplusPrice, $bargainInfo['people_num'] - $people, $userInfo->add_time == $userInfo->last_time && !$this->dao->count(['uid' => $uid]));
  118. }
  119. $allPrice = bcadd((string)$bargainUserInfo['price'], (string)$price, 2);
  120. if ($bargainUserInfo['uid'] == $uid) {
  121. $type = 1;
  122. } else {
  123. //帮砍次数限制
  124. $count = $this->dao->count(['uid' => $uid, 'bargain_id' => $bargainInfo['id'], 'type' => 0]);
  125. if ($count >= $bargainInfo['bargain_num']) throw new ApiException(410310);
  126. $type = 0;
  127. }
  128. /** @var StoreBargainUserServices $bargainUserService */
  129. $bargainUserService = app()->make(StoreBargainUserServices::class);
  130. $res1 = $bargainUserService->update($bargainUserInfo['id'], ['price' => $allPrice]);
  131. $res2 = $this->dao->save([
  132. 'uid' => $uid,
  133. 'bargain_id' => $bargainInfo['id'],
  134. 'bargain_user_id' => $bargainUserInfo['id'],
  135. 'price' => $price,
  136. 'add_time' => time(),
  137. 'type' => $type,
  138. ]);
  139. $res = $res1 && $res2;
  140. if (!$res) throw new AdminException(410307);
  141. return $price;
  142. }
  143. /**
  144. * 随机金额
  145. * @param $price
  146. * @param $people
  147. * @param $type
  148. * @return string
  149. */
  150. public function randomFloat($price, $people, $type = false)
  151. {
  152. //按照人数计算保留金额
  153. $retainPrice = bcmul((string)$people, '0.01', 2);
  154. //实际剩余金额
  155. $price = bcsub((string)$price, $retainPrice, 2);
  156. //计算比例
  157. if ($type) {
  158. $percent = '0.5';
  159. } else {
  160. $percent = bcdiv((string)mt_rand(20, 50), '100', 2);
  161. }
  162. //实际砍掉金额
  163. $cutPrice = bcmul($price, $percent, 2);
  164. //如果计算出来为0,默认砍掉0.01
  165. return $cutPrice != '0.00' ? $cutPrice : '0.01';
  166. }
  167. /**
  168. * 获取砍价商品已砍人数
  169. * @return array
  170. */
  171. public function getNums()
  172. {
  173. $nums = $this->dao->getNums();
  174. $dat = [];
  175. foreach ($nums as $item) {
  176. $dat[$item['bargain_user_id']] = $item['num'];
  177. }
  178. return $dat;
  179. }
  180. }