// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\services\user; use app\dao\system\AwardLakeDao; use app\dao\user\UserIntegralDao; use app\model\system\AwardLake; use app\model\user\UserIntegral; use app\services\BaseServices; use crmeb\services\FormBuilder; use crmeb\traits\ServicesTrait; use app\webscoket\SocketPush; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\facade\Route; /** * * 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 array|\think\Model|null */ public function getFirstStaticIntegral($uid) { $where = ['uid' => $uid, 'status' => 0, 'type' => 0]; return $this->dao->getOne($where); } /** * 获取单条数据 * @return array|\think\Model|null */ public function getTopStaticIntegral($id) { $where = ['status' => 0, 'type' => 0]; return $this->dao->search($where)->where('id', '>', $id)->order('id', 'asc')->find(); } /** * 获取单条数据 * @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); } /** * 获取单条数据 * @return float */ public function reOrder($uid, $exceptId = 0) { $where = ['uid' => $uid, 'type' => 0, 'order_id_not' => $exceptId]; return $this->dao->search($where)->count() > 0; } /** * 获取用户业绩 * @param $uid * @return float */ public function getAchievement($uid, $status = '') { $where = ['uid' => array_merge([$uid], get_group_user($uid)), 'type' => 0]; if ($status != '') $where['status'] = $status; return $this->dao->sum($where, 'order_price', true); } /** * 获取单条数据 * @return float */ public function getLake($time = '') { if ($time == '') { return $this->lake_dao->sum([], 'num'); } else { return AwardLake::whereTime('add_time', $time)->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); } return $price; } /** * 加积分 * @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 = '', $add_time = 0) { if ($price > 0) $inc_integral = bcdiv($total, $price, 5); else $inc_integral = 0; 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' => $add_time ?: time(), 'status' => $inc_integral > 0 ? 0 : 1, ]); } public function incIntegralAdmin(int $uid, string $price, string $total, float $order_price, int $type, string $extract_sum, int $link_id = 0, string $mark = '', $add_time = 0) { $inc_integral = bcdiv($total, $price, 5); $old = UserIntegral::where('uid', $uid)->where('link_id', $link_id)->where('type', $type)->whereLike('mark', '%' . $mark . '%')->find(); if ($old) { $dump = '用户' . $uid . $mark . '补' . bcsub($inc_integral, $old['num'], 5); } else { $dump = '用户' . $uid . $mark . '补' . $inc_integral; } @file_put_contents('add', $dump . PHP_EOL, FILE_APPEND); if ($old) { UserIntegral::where('id', $old['id'])->delete(); } return UserIntegral::create([ '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' => $add_time, 'status' => $inc_integral > 0 ? 0 : 1, ]); // return true; } public function incUpdateIntegral(int $id, string $price, string $total, string $mark) { if ($price > 0) { $inc_integral = bcdiv($total, $price, 5); } else { $inc_integral = $total; } $origin = $this->get($id); return $this->dao->update($id, [ 'num' => bcadd($origin['num'], $inc_integral, 5), 'mark' => $origin['mark'] . $mark . (float)$inc_integral, ]); } /** * 加奖池 * @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(), ]); } /** * 获取修改页面数据 * @param int $id * @return array * @throws \FormBuilder\Exception\FormBuilderException */ public function editLack() { $f = array(); $f[] = FormBuilder::number('sum', '资金池', $this->getLake())->disabled(true); $f[] = FormBuilder::number('num', '操作数量', 0)->step(0.01)->min(0.01)->max($this->getLake())->required(); $f[] = FormBuilder::radio('pm', '操作类型', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 0, 'label' => '减少']]); $f[] = FormBuilder::textarea('mark', '操作原因'); return create_form('编辑', $f, Route::buildUrl('/finance/lake'), 'POST'); } }