UserAwardIntegralServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\user;
  13. use app\dao\system\AwardLakeDao;
  14. use app\dao\user\UserIntegralDao;
  15. use app\services\BaseServices;
  16. use crmeb\traits\ServicesTrait;
  17. use app\webscoket\SocketPush;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. *
  23. * Class UserRechargeServices
  24. * @package app\services\user
  25. * @mixin UserIntegralDao
  26. */
  27. class UserAwardIntegralServices extends BaseServices
  28. {
  29. use ServicesTrait;
  30. private $lake_dao;
  31. /**
  32. * UserRechargeServices constructor.
  33. * @param UserIntegralDao $dao
  34. */
  35. public function __construct(UserIntegralDao $dao, AwardLakeDao $awardLakeDao)
  36. {
  37. $this->dao = $dao;
  38. $this->lake_dao = $awardLakeDao;
  39. }
  40. /**
  41. * 获取单条数据
  42. * @param int $id
  43. * @param array $field
  44. */
  45. public function getIntegral(int $id, array $field = [])
  46. {
  47. return $this->dao->get($id, $field);
  48. }
  49. /**
  50. * 获取单条数据
  51. * @return float
  52. */
  53. public function getIntegralSum($where)
  54. {
  55. return $this->dao->sum($where, 'num', true);
  56. }
  57. /**
  58. * 获取单条数据
  59. * @return float
  60. */
  61. public function getPaySum($uid)
  62. {
  63. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  64. return $this->dao->sum($where, 'order_price', true);
  65. }
  66. /**
  67. * 获取单条数据
  68. * @return float
  69. */
  70. public function getHourExtractPaySum($uid, $HourLimit = 0)
  71. {
  72. $where = ['uid' => $uid, 'status' => 1, 'type' => 0];
  73. $where['extract_time_gt'] = time() - ($HourLimit * 3600);
  74. return $this->dao->sum($where, 'order_price', true);
  75. }
  76. /**
  77. * 获取用户业绩
  78. * @param $uid
  79. * @return float
  80. */
  81. public function getAchievement($uid)
  82. {
  83. $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
  84. return $this->dao->sum($where, 'order_price', true);
  85. }
  86. /**
  87. * 获取单条数据
  88. * @return float
  89. */
  90. public function getLake()
  91. {
  92. return $this->lake_dao->sum([], 'num');
  93. }
  94. /**
  95. * 获取单条数据
  96. * @param int $link_id
  97. * @return string|null
  98. * @throws DataNotFoundException
  99. * @throws DbException
  100. * @throws ModelNotFoundException
  101. */
  102. public function getPrice(int $link_id = 0)
  103. {
  104. $price = 0;
  105. if ($link_id > 0) {
  106. $info = $this->dao->getOne(['link_id' => $link_id]);
  107. if ($info) $price = (string)($info['price'] ?: 0);
  108. }
  109. if ($price <= 0) {
  110. $lake_sum = $this->getLake();
  111. $sum_integral = $this->getIntegralSum(['status' => 0]);
  112. return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
  113. }
  114. }
  115. /**
  116. * 加积分
  117. * @param int $uid 用户
  118. * @param string $price 价格
  119. * @param string $total 总额
  120. * @param float $order_price
  121. * @param int $type 类型
  122. * @param string $extract_sum
  123. * @param int $link_id 关联ID
  124. * @param string $mark
  125. * @return \crmeb\basic\BaseModel|\think\Model
  126. */
  127. public function incIntegral(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '')
  128. {
  129. $inc_integral = bcdiv($total, $price, 5);
  130. return $this->dao->save([
  131. 'uid' => $uid,
  132. 'type' => $type == 1 ? 1 : 0,
  133. 'num' => $inc_integral,
  134. 'price' => $price,
  135. 'sum_price' => $total,
  136. 'extract_sum' => $extract_sum,
  137. 'link_id' => $link_id,
  138. 'mark' => $mark,
  139. 'order_price' => $order_price,
  140. 'add_time' => time(),
  141. ]);
  142. }
  143. /**
  144. * 加奖池
  145. * @param string $total 总额
  146. * @param int $link_id 关联ID
  147. * @param string $mark
  148. * @return \crmeb\basic\BaseModel|\think\Model
  149. */
  150. public function addLake(string $total, int $link_id = 0, string $mark = '')
  151. {
  152. return $this->lake_dao->save([
  153. 'num' => $total,
  154. 'link_id' => $link_id,
  155. 'mark' => $mark,
  156. 'add_time' => time(),
  157. ]);
  158. }
  159. }