123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?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\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');
- }
- }
|