// +---------------------------------------------------------------------- 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_numwhere('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; } }