UserAwardIntegralServices.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $price = $this->getPrice();
  62. return compact('list', 'count', 'price');
  63. }
  64. /**
  65. * 获取单条数据
  66. * @param int $id
  67. * @param array $field
  68. */
  69. public function getIntegral(int $id, array $field = [])
  70. {
  71. return $this->dao->get($id, $field);
  72. }
  73. /**
  74. * 获取单条数据
  75. * @param string|float $price
  76. * @param string $field
  77. * @return array
  78. * @throws DataNotFoundException
  79. * @throws DbException
  80. * @throws ModelNotFoundException
  81. */
  82. public function getIntegralsOverExtract($price, string $field = '*')
  83. {
  84. if (!$price) $price = $this->getPrice();
  85. return $this->dao->getList(['extract_price' => $price, 'status' => 0], $field);
  86. }
  87. /**
  88. * 获取单条数据
  89. * @return float
  90. */
  91. public function getIntegralSum($where)
  92. {
  93. return $this->dao->sum($where, 'num', true);
  94. }
  95. /**
  96. * 获取单条数据
  97. * @return float
  98. */
  99. public function getPaySum($uid)
  100. {
  101. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  102. return $this->dao->sum($where, 'order_price', true);
  103. }
  104. /**
  105. * 获取单条数据
  106. * @return array|\think\Model|null
  107. */
  108. public function getFirstStaticIntegral($uid)
  109. {
  110. $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
  111. return $this->dao->getOne($where);
  112. }
  113. /**
  114. * 获取单条数据
  115. * @return float
  116. */
  117. public function getHourExtractPaySum($uid, $HourLimit = 0)
  118. {
  119. $where = ['uid' => $uid, 'status' => 1, 'type' => 0];
  120. $where['extract_time_gt'] = time() - ($HourLimit * 3600);
  121. return $this->dao->sum($where, 'order_price', true);
  122. }
  123. /**
  124. * 获取用户业绩
  125. * @param $uid
  126. * @return float
  127. */
  128. public function getAchievement($uid)
  129. {
  130. $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
  131. return $this->dao->sum($where, 'order_price', true);
  132. }
  133. /**
  134. * 获取单条数据
  135. * @return float
  136. */
  137. public function getLake()
  138. {
  139. return $this->lake_dao->sum([], 'num');
  140. }
  141. /**
  142. * 获取单条数据
  143. * @param int $link_id
  144. * @return string|null
  145. * @throws DataNotFoundException
  146. * @throws DbException
  147. * @throws ModelNotFoundException
  148. */
  149. public function getPrice(int $link_id = 0)
  150. {
  151. $price = 0;
  152. if ($link_id > 0) {
  153. $info = $this->dao->getOne(['link_id' => $link_id]);
  154. if ($info) $price = (string)($info['price'] ?: 0);
  155. }
  156. if ($price <= 0) {
  157. $lake_sum = $this->getLake();
  158. $sum_integral = $this->getIntegralSum(['status' => 0]);
  159. return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
  160. }
  161. }
  162. /**
  163. * 加积分
  164. * @param int $uid 用户
  165. * @param string $price 价格
  166. * @param string $total 总额
  167. * @param float $order_price
  168. * @param int $type 类型
  169. * @param string $extract_sum
  170. * @param int $link_id 关联ID
  171. * @param string $mark
  172. * @return \crmeb\basic\BaseModel|\think\Model
  173. */
  174. public function incIntegral(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '')
  175. {
  176. $inc_integral = bcdiv($total, $price, 5);
  177. return $this->dao->save([
  178. 'uid' => $uid,
  179. 'type' => $type == 1 ? 1 : 0,
  180. 'num' => $inc_integral,
  181. 'price' => $price,
  182. 'sum_price' => $total,
  183. 'extract_sum' => $extract_sum,
  184. 'link_id' => $link_id,
  185. 'mark' => $mark,
  186. 'order_price' => $order_price,
  187. 'add_time' => time(),
  188. 'status' => $inc_integral > 0 ? 0 : 1,
  189. ]);
  190. }
  191. public function incUpdateIntegral(int $id, string $price, string $total, string $mark)
  192. {
  193. $inc_integral = bcdiv($total, $price, 5);
  194. $origin = $this->get($id);
  195. return $this->dao->update($id, [
  196. 'num' => bcadd($origin['num'], $inc_integral, 5),
  197. 'mark' => $origin['mark'] . $mark . (float)$inc_integral,
  198. ]);
  199. }
  200. /**
  201. * 加奖池
  202. * @param string $total 总额
  203. * @param int $link_id 关联ID
  204. * @param string $mark
  205. * @return \crmeb\basic\BaseModel|\think\Model
  206. */
  207. public function addLake(string $total, int $link_id = 0, string $mark = '')
  208. {
  209. return $this->lake_dao->save([
  210. 'num' => $total,
  211. 'link_id' => $link_id,
  212. 'mark' => $mark,
  213. 'add_time' => time(),
  214. ]);
  215. }
  216. }