StoreBargainUserHelpServices.php 7.3 KB

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