123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\user;
- use app\dao\system\AwardLakeDao;
- use app\dao\user\UserIntegralDao;
- use app\services\BaseServices;
- use crmeb\traits\ServicesTrait;
- use app\webscoket\SocketPush;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- *
- * Class UserRechargeServices
- * @package app\services\user
- * @mixin UserIntegralDao
- */
- class UserAwardIntegralServices extends BaseServices
- {
- use ServicesTrait;
- private $lake_dao;
- /**
- * UserRechargeServices constructor.
- * @param UserIntegralDao $dao
- */
- public function __construct(UserIntegralDao $dao, AwardLakeDao $awardLakeDao)
- {
- $this->dao = $dao;
- $this->lake_dao = $awardLakeDao;
- }
- /**
- * 获取充值列表
- * @param array $where
- * @param string $field
- * @param int $limit
- * @return array
- */
- public function getIntegralList(array $where, string $field = '*', int $limit = 0, array $with = [])
- {
- $whereData = $where;
- if ($limit) {
- [$page] = $this->getPageValue();
- } else {
- [$page, $limit] = $this->getPageValue();
- }
- $list = $this->dao->getList($whereData, $field, $page, $limit, $with);
- $count = $this->dao->count($whereData);
- foreach ($list as &$item) {
- $item['_extract_time'] = $item['extract_time'] ? date('Y-m-d H:i:s', $item['extract_time']) : '暂无';
- $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
- }
- $price = $this->getPrice();
- return compact('list', 'count', 'price');
- }
- /**
- * 获取单条数据
- * @param int $id
- * @param array $field
- */
- public function getIntegral(int $id, array $field = [])
- {
- return $this->dao->get($id, $field);
- }
- /**
- * 获取单条数据
- * @param string|float $price
- * @param string $field
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getIntegralsOverExtract($price, string $field = '*')
- {
- if (!$price) $price = $this->getPrice();
- return $this->dao->getList(['extract_price' => $price, 'status' => 0], $field);
- }
- /**
- * 获取单条数据
- * @return float
- */
- public function getIntegralSum($where)
- {
- return $this->dao->sum($where, 'num', true);
- }
- /**
- * 获取单条数据
- * @return float
- */
- public function getPaySum($uid)
- {
- $where = ['uid' => $uid, 'status' => 0, 'type' => 0];
- return $this->dao->sum($where, 'order_price', true);
- }
- /**
- * 获取单条数据
- * @return float
- */
- public function getHourExtractPaySum($uid, $HourLimit = 0)
- {
- $where = ['uid' => $uid, 'status' => 1, 'type' => 0];
- $where['extract_time_gt'] = time() - ($HourLimit * 3600);
- return $this->dao->sum($where, 'order_price', true);
- }
- /**
- * 获取用户业绩
- * @param $uid
- * @return float
- */
- public function getAchievement($uid)
- {
- $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0];
- return $this->dao->sum($where, 'order_price', true);
- }
- /**
- * 获取单条数据
- * @return float
- */
- public function getLake()
- {
- return $this->lake_dao->sum([], 'num');
- }
- /**
- * 获取单条数据
- * @param int $link_id
- * @return string|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getPrice(int $link_id = 0)
- {
- $price = 0;
- if ($link_id > 0) {
- $info = $this->dao->getOne(['link_id' => $link_id]);
- if ($info) $price = (string)($info['price'] ?: 0);
- }
- if ($price <= 0) {
- $lake_sum = $this->getLake();
- $sum_integral = $this->getIntegralSum(['status' => 0]);
- return (string)($sum_integral > 0 ? bcdiv((string)$lake_sum, (string)$sum_integral, 8) : 1);
- }
- }
- /**
- * 加积分
- * @param int $uid 用户
- * @param string $price 价格
- * @param string $total 总额
- * @param float $order_price
- * @param int $type 类型
- * @param string $extract_sum
- * @param int $link_id 关联ID
- * @param string $mark
- * @return \crmeb\basic\BaseModel|\think\Model
- */
- public function incIntegral(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '')
- {
- $inc_integral = bcdiv($total, $price, 5);
- return $this->dao->save([
- 'uid' => $uid,
- 'type' => $type == 1 ? 1 : 0,
- 'num' => $inc_integral,
- 'price' => $price,
- 'sum_price' => $total,
- 'extract_sum' => $extract_sum,
- 'link_id' => $link_id,
- 'mark' => $mark,
- 'order_price' => $order_price,
- 'add_time' => time(),
- 'status' => $inc_integral > 0 ? 0 : 1,
- ]);
- }
- /**
- * 加奖池
- * @param string $total 总额
- * @param int $link_id 关联ID
- * @param string $mark
- * @return \crmeb\basic\BaseModel|\think\Model
- */
- public function addLake(string $total, int $link_id = 0, string $mark = '')
- {
- return $this->lake_dao->save([
- 'num' => $total,
- 'link_id' => $link_id,
- 'mark' => $mark,
- 'add_time' => time(),
- ]);
- }
- }
|