123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?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\user\AwardIntegralDao;
- use app\model\system\AwardLake;
- use app\model\user\UserIntegral;
- use app\services\BaseServices;
- use crmeb\services\FormBuilder;
- use crmeb\traits\ServicesTrait;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\facade\Route;
- /**
- *
- * Class UserRechargeServices
- * @package app\services\user
- * @mixin AwardIntegralDao
- */
- class AwardIntegralServices extends BaseServices
- {
- use ServicesTrait;
- /**
- * UserRechargeServices constructor.
- * @param AwardIntegralDao $dao
- */
- public function __construct(AwardIntegralDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取充值列表
- * @param array $where
- * @param string $field
- * @param int $limit
- * @return array
- */
- public function getIntegralList(array $where, string $field = '*', int $limit = 0, array $with = ['user_order'])
- {
- $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['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
- }
- return compact('list', 'count');
- }
- /**
- * 获取单条数据
- * @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, $field = 'num')
- {
- return $this->dao->sum($where, $field, true);
- }
- /**
- * 获取单条数据
- * @return float
- */
- public function getPaySum($uid)
- {
- $where = ['uid' => $uid, 'valid' => 1];
- return $this->dao->sum($where, 'order_price', true);
- }
- /**
- * 获取单条数据
- * @return float
- */
- public function getPaySum2($uid)
- {
- $where = ['uid' => $uid, 'valids' => 1];
- return $this->dao->sum($where, 'order_price', true);
- }
- /**
- * 获取单条数据
- * @return array|\think\Model|null
- */
- public function getFirstStaticIntegral($uid)
- {
- $where = ['uid' => $uid, 'valid' => 1];
- return $this->dao->search($where)->order('id', 'asc')->find();
- }
- /**
- * 获取用户业绩
- * @param $uid
- * @return float
- */
- public function getAchievement($uid)
- {
- $where = ['uid' => array_merge([$uid], get_group_user($uid))];
- $service = app()->make(UserAwardIntegralServices::class);
- return $this->dao->sum($where, 'order_price', true) + $service->getAchievement($uid, 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 $total, float $order_price, int $order_id = 0, string $mark = '', $add_time = 0, $pay_type = 'yue')
- {
- // $inc_integral = bcdiv($total, $price, 5);
- //计算释放天数
- $default_send_day = sys_config('default_send_day', 90);
- $rest_time = sys_config('default_send_day_start_time', 0);
- $reorder_inc = sys_config('reorder_int_send_day', 10);
- $order_inc = sys_config('int_send_day', 10);
- //寻找复投机会
- $main_orders = $this->dao->search(['uid' => $uid, 'link_id' => 0, 'order_price_elt' => $order_price, 'valid' => 2])->order('id asc')->column('id');//满足复投条件的主单
- $link_id = 0;
- $reorder_count = 0;
- foreach ($main_orders as $v) {
- $can_reorder = $this->dao->search(['uid' => $uid, 'valid' => 1, 'link_id' => $v])->find();
- if (!$can_reorder) {
- $link_id = $v;
- $reorder_count = ($this->dao->search(['uid' => $uid, 'link_id' => $v])->max('reorder_count')) + 1;
- break;
- }
- }
- $last_order_send_day = $this->dao->search(['uid' => $uid, 'add_time_gt' => $rest_time])->order('id desc')->value('send_day');
- if (!$last_order_send_day) $send_day = $default_send_day;
- else {
- if ($link_id > 0) $send_day = $last_order_send_day + $reorder_inc;
- else $send_day = $last_order_send_day + $order_inc;
- }
- return $this->dao->save([
- 'uid' => $uid,
- 'num' => $total,
- 'send_day' => $send_day,
- 'day_send' => bcdiv((string)$total, (string)$send_day, 2),
- 'sum_price' => $total,
- 'order_id' => $order_id,
- 'link_id' => $link_id,
- 'pay_type' => $pay_type,
- 'reorder_count' => $reorder_count,
- 'mark' => $mark,
- 'order_price' => $order_price,
- 'add_time' => $add_time ?: time(),
- 'status' => $total > 0 ? 0 : 1,
- 'send_time' => time()
- ]);
- }
- // 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,
- // ]);
- // }
- public function upSpeedIntegral(int $id, string $total)
- {
- $origin = $this->get($id);
- $real_send = $total;
- $left = $origin['num'] - $origin['sent_num'];
- $update = [];
- if ($real_send >= $left) {
- $real_send = $left;
- $update['up_speed_used'] = $origin['up_speed'];
- $update['sent_num'] = $origin['num'];
- $update['status'] = 1;
- } else {
- $update['sent_num'] = bcadd((string)$origin['sent_num'], (string)$real_send, 2);
- }
- $update['extract_num'] = bcadd((string)$real_send, (string)$origin['extract_num'], 2);
- $res = $this->dao->update($id, $update);
- /** @var UserBillServices $billService */
- $billService = app()->make(UserBillServices::class);
- $billService->income('integral_speed_up', $origin['uid'], $real_send, $this->getIntegralSum(['uid' => $origin['uid']], 'extract_num') + $real_send, $origin['id']);
- if (($update['status'] ?? 0) == 1) {
- $res = $res && $this->extract($origin['id'], $update['extract_num'], true);
- }
- return $res;
- // $inc_integral = $total;
- // return $this->dao->update($id, [
- // 'up_speed' => bcadd((string)$origin['up_speed'], (string)$inc_integral, 2),
- // ]);
- }
- //每日释放
- public function daySend($test = false)
- {
- $sum = $this->dao->search()->whereTime('add_time', 'yesterday')->sum('order_price');
- $system_check = sys_config('system_achievement_check', 10000);
- $list = $this->dao->search()->where('status', 0)->where('sent_num<num')->where('send_time', '<', $test ? (time() + 1000) : strtotime('today'))->limit($test ? 10000 : 50)->select();
- foreach ($list as $v) {
- $update = [];
- if ($sum < $system_check) {
- $v['stop_day'] = $v['stop_day'] + sys_config('fail_inc_send_day', 1);
- }
- if ($v['stop_day'] > 0) {
- // $this->dao->search()->where('id', $v['id'])->dec('stop_day', 1)->update();
- $update['send_time'] = time();
- $update['stop_day'] = $v['stop_day'] - 1;
- $this->dao->search()->where('id', $v['id'])->update($update);
- continue;
- }
- $day_send = $v['day_send'];
- $send_day = $v['send_day'];
- $up_speed = $v['up_speed'] - $v['up_speed_used'];
- $up_speed = bcdiv((string)$up_speed, (string)($send_day - $v['sent_day']), 2);
- $real_send = $day_send + $up_speed;
- $left = $v['num'] - $v['sent_num'];
- $update['day_send'] = $day_send;
- $update['up_speed_used'] = $v['up_speed_used'] + $up_speed;
- $update['sent_day'] = $v['sent_day'] + 1;
- $update['send_day'] = $send_day;
- if ($update['sent_day'] == $update['send_day']) {
- $real_send = $left;
- }
- if ($real_send >= $left) {
- $real_send = $left;
- $update['up_speed_used'] = $v['up_speed'];
- $update['sent_num'] = $v['num'];
- $update['status'] = 1;
- } else {
- $update['sent_num'] = bcadd((string)$v['sent_num'], (string)$real_send, 2);
- }
- $update['extract_num'] = bcadd((string)$real_send, (string)$v['extract_num'], 2);
- $update['send_time'] = time();
- $this->dao->update($v['id'], $update);
- /** @var UserBillServices $billService */
- $billService = app()->make(UserBillServices::class);
- $billService->income('integral_day_send', $v['uid'], $real_send, $this->getIntegralSum(['uid' => $v['uid']], 'extract_num') + $real_send, $v['id']);
- if (($update['status'] ?? 0) == 1) {
- $this->extract($v['id'], $update['extract_num'], true);
- }
- }
- }
- //提取
- public function extract($id, $num, $system = false)
- {
- $origin = $this->get($id);
- if ($origin['extract_num'] < $num) {
- if (!$system) {
- throw new Exception('可提取参考分不足');
- }
- $num = $origin['extract_num'];
- }
- if ($num <= 0) return true;
- $update['extract_num'] = bcsub((string)$origin['extract_num'], (string)$num, 2);
- $update['extracted_num'] = bcadd((string)$origin['extracted_num'], (string)$num, 2);
- $this->update($id, $update);
- $res = true;
- /** @var UserBrokerageServices $brokerageService */
- $brokerageService = app()->make(UserBrokerageServices::class);
- /** @var UserBillServices $billService */
- $billService = app()->make(UserBillServices::class);
- /** @var UserMoneyServices $moneyService */
- $moneyService = app()->make(UserMoneyServices::class);
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $billService->income('extract_award_integral', $origin['uid'], $num, $this->getIntegralSum(['uid' => $origin['uid']], 'extract_num') - $num, $id);
- $uid = $origin['uid'];
- $user = $userService->getUserInfo($uid);
- $reorder_set = sys_config('reorder_times', 2);
- $to_integral = sys_config('shop_integral_ratio');
- $mark = '用户参考分' . ($system ? '出局' : '提取') . $num;
- $to_integral = bcdiv(bcmul((string)$num, (string)$to_integral), '100', 2);
- if ($to_integral > 0) {
- $balance = bcadd((string)$user['integral'], (string)$to_integral, 2);
- $res = $res && $billService->income('extract_integral_shop', $uid, [
- 'mark' => $mark,
- 'number' => floatval($to_integral),
- ], $balance, $id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'integral', (string)$to_integral, 'uid');
- }
- if ($to_integral < $num) {
- $left = bcsub((string)$num, (string)$to_integral, 2);
- if ($reorder_set >= $origin['reorder_count']) {
- $to_now_money = sys_config('re_now_money');
- $to_now_money = bcdiv(bcmul((string)$left, (string)$to_now_money), '100', 2);
- if ($to_now_money > 0) {
- $balance = bcadd((string)$user['now_money'], (string)$to_now_money, 2);
- $res = $res && $moneyService->income('extract_integral_static', $uid, [
- 'mark' => $mark,
- 'number' => floatval($to_now_money),
- ], $balance, $id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'now_money', $to_now_money, 'uid');
- }
- $to_energy = sys_config('re_energy');
- $to_energy = bcdiv(bcmul((string)$left, (string)$to_energy), '100', 2);
- if ($to_energy > 0) {
- $balance = bcadd((string)$user['energy'], (string)$to_energy, 2);
- $res = $res && $billService->income('extract_integral_static', $uid, [
- 'mark' => $mark,
- 'number' => floatval($to_energy),
- ], $balance, $id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'energy', $to_energy, 'uid');
- }
- $to_brokerage = 100 - $to_now_money - $to_energy;
- if ($to_brokerage < 0) $to_brokerage = 0;
- $to_brokerage = bcdiv(bcmul((string)$left, (string)$to_brokerage), '100', 2);
- if ($to_brokerage > 0) {
- $balance = bcadd((string)$user['brokerage_price'], (string)$to_brokerage, 2);
- $res = $res && $brokerageService->income('extract_integral_static', $uid, [
- 'mark' => $mark,
- 'number' => floatval($to_energy),
- ], $balance, $id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'brokerage_price', $to_brokerage, 'uid');
- }
- } else {
- //消费分,佣金
- $balance = bcadd((string)$user['brokerage_price'], (string)$left, 2);
- $res = $res && $brokerageService->income('extract_integral_static', $uid, [
- 'mark' => $mark,
- 'number' => floatval($left),
- ], $balance, $id);
- // 添加用户佣金
- $res = $res && $userService->bcInc($uid, 'brokerage_price', $left, 'uid');
- }
- }
- return $res;
- }
- }
|