// +---------------------------------------------------------------------- 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); } /** * 获取单条数据 * @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(), ]); } }