UserAwardIntegralServices.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 array $where
  43. * @param string $field
  44. * @param int $limit
  45. * @return array
  46. */
  47. public function getIntegralList(array $where, string $field = '*', int $limit = 0, array $with = [])
  48. {
  49. $whereData = $where;
  50. if ($limit) {
  51. [$page] = $this->getPageValue();
  52. } else {
  53. [$page, $limit] = $this->getPageValue();
  54. }
  55. $list = $this->dao->getList($whereData, $field, $page, $limit, $with);
  56. $count = $this->dao->count($whereData);
  57. foreach ($list as &$item) {
  58. $item['_extract_time'] = $item['extract_time'] ? date('Y-m-d H:i:s', $item['extract_time']) : '暂无';
  59. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  60. }
  61. return compact('list', 'count');
  62. }
  63. /**
  64. * 获取单条数据
  65. * @param int $id
  66. * @param array $field
  67. */
  68. public function getIntegral(int $id, array $field = [])
  69. {
  70. return $this->dao->get($id, $field);
  71. }
  72. /**
  73. * 获取单条数据
  74. * @return float
  75. */
  76. public function getIntegralSum($where)
  77. {
  78. return $this->dao->sum($where, 'num', true);
  79. }
  80. /**
  81. * 获取单条数据
  82. * @return float
  83. */
  84. public function getPaySum($uid)
  85. {
  86. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  87. return $this->dao->sum($where, 'order_price', true);
  88. }
  89. /**
  90. * 获取单条数据
  91. * @return float
  92. */
  93. public function getHourExtractPaySum($uid, $HourLimit = 0)
  94. {
  95. $where = ['uid' => $uid, 'status' => 1, 'type' => 0];
  96. $where['extract_time_gt'] = time() - ($HourLimit * 3600);
  97. return $this->dao->sum($where, 'order_price', true);
  98. }
  99. /**
  100. * 获取用户业绩
  101. * @param $uid
  102. * @return float
  103. */
  104. public function getAchievement($uid)
  105. {
  106. $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
  107. return $this->dao->sum($where, 'order_price', true);
  108. }
  109. /**
  110. * 获取单条数据
  111. * @return float
  112. */
  113. public function getLake()
  114. {
  115. return $this->lake_dao->sum([], 'num');
  116. }
  117. /**
  118. * 获取单条数据
  119. * @param int $link_id
  120. * @return string|null
  121. * @throws DataNotFoundException
  122. * @throws DbException
  123. * @throws ModelNotFoundException
  124. */
  125. public function getPrice(int $link_id = 0)
  126. {
  127. $price = 0;
  128. if ($link_id > 0) {
  129. $info = $this->dao->getOne(['link_id' => $link_id]);
  130. if ($info) $price = (string)($info['price'] ?: 0);
  131. }
  132. if ($price <= 0) {
  133. $lake_sum = $this->getLake();
  134. $sum_integral = $this->getIntegralSum(['status' => 0]);
  135. return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
  136. }
  137. }
  138. /**
  139. * 加积分
  140. * @param int $uid 用户
  141. * @param string $price 价格
  142. * @param string $total 总额
  143. * @param float $order_price
  144. * @param int $type 类型
  145. * @param string $extract_sum
  146. * @param int $link_id 关联ID
  147. * @param string $mark
  148. * @return \crmeb\basic\BaseModel|\think\Model
  149. */
  150. public function incIntegral(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '')
  151. {
  152. $inc_integral = bcdiv($total, $price, 5);
  153. return $this->dao->save([
  154. 'uid' => $uid,
  155. 'type' => $type == 1 ? 1 : 0,
  156. 'num' => $inc_integral,
  157. 'price' => $price,
  158. 'sum_price' => $total,
  159. 'extract_sum' => $extract_sum,
  160. 'link_id' => $link_id,
  161. 'mark' => $mark,
  162. 'order_price' => $order_price,
  163. 'add_time' => time(),
  164. ]);
  165. }
  166. /**
  167. * 加奖池
  168. * @param string $total 总额
  169. * @param int $link_id 关联ID
  170. * @param string $mark
  171. * @return \crmeb\basic\BaseModel|\think\Model
  172. */
  173. public function addLake(string $total, int $link_id = 0, string $mark = '')
  174. {
  175. return $this->lake_dao->save([
  176. 'num' => $total,
  177. 'link_id' => $link_id,
  178. 'mark' => $mark,
  179. 'add_time' => time(),
  180. ]);
  181. }
  182. }