|
@@ -0,0 +1,363 @@
|
|
|
+<?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 UserIntegralDao
|
|
|
+ */
|
|
|
+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 = [])
|
|
|
+ {
|
|
|
+ $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)
|
|
|
+ {
|
|
|
+ return $this->dao->sum($where, 'num', true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单条数据
|
|
|
+ * @return float
|
|
|
+ */
|
|
|
+ public function getPaySum($uid)
|
|
|
+ {
|
|
|
+ $where = ['uid' => $uid, 'valid' => 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))];
|
|
|
+ return $this->dao->sum($where, 'order_price', true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加积分
|
|
|
+ * @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)
|
|
|
+ {
|
|
|
+// $inc_integral = bcdiv($total, $price, 5);
|
|
|
+ //计算释放天数
|
|
|
+ $default_send_day = sys_config('default_send_day', 90);
|
|
|
+ $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])->max('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($total, $send_day, 2),
|
|
|
+ 'sum_price' => $total,
|
|
|
+ 'order_id' => $order_id,
|
|
|
+ 'link_id' => $link_id,
|
|
|
+ '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)
|
|
|
+ {
|
|
|
+ $inc_integral = $total;
|
|
|
+ $origin = $this->get($id);
|
|
|
+ return $this->dao->update($id, [
|
|
|
+ 'up_speed' => bcadd($origin['up_speed'], $inc_integral, 2),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //每日释放
|
|
|
+ public function daySend()
|
|
|
+ {
|
|
|
+ $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', '<', strtotime('today'))->limit(50)->select();
|
|
|
+ foreach ($list as $v) {
|
|
|
+ $day_send = $v['day_send'];
|
|
|
+ $send_day = $v['send_day'];
|
|
|
+ $up_speed = $v['up_speed'] - $v['up_speed_used'];
|
|
|
+ if ($sum < $system_check) {
|
|
|
+ $send_day = $v['send_day'] + sys_config('fail_inc_send_day', 1);
|
|
|
+ $day_send = bcdiv((string)(($v['num'] - $v['sent_num'])), (string)$send_day, 2);
|
|
|
+ }
|
|
|
+ $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 ($real_send >= $left) {
|
|
|
+ $real_send = $left;
|
|
|
+ $update['up_speed_used'] = $v['up_speed'];
|
|
|
+ $update['sent_num'] = $v['num'];
|
|
|
+ $update['status'] = 1;
|
|
|
+ }
|
|
|
+ $update['extract_num'] = bcadd($real_send, $v['extract_num'], 2);
|
|
|
+ $update['send_time'] = time();
|
|
|
+ $this->dao->update($v['id'], $update);
|
|
|
+ if (($update['status'] ?? 0) == 1) {
|
|
|
+ $this->extract($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($origin['extract_num'], $num, 2);
|
|
|
+ $update['extracted_num'] = bcadd($origin['extracted_num'], $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);
|
|
|
+ $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($user['integral'], $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', $to_integral, 'uid');
|
|
|
+ }
|
|
|
+ if ($to_integral < $num) {
|
|
|
+ $left = bcsub((string)$num, $to_integral, 2);
|
|
|
+ if ($reorder_set > $origin['reorder_count']) {
|
|
|
+ $to_now_money = sys_config('re_now_money');
|
|
|
+ $to_now_money = bcdiv(bcmul($left, (string)$to_now_money), '100', 2);
|
|
|
+ if ($to_now_money > 0) {
|
|
|
+ $balance = bcadd($user['now_money'], $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($left, (string)$to_energy), '100', 2);
|
|
|
+ if ($to_energy > 0) {
|
|
|
+ $balance = bcadd($user['energy'], $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($left, (string)$to_brokerage), '100', 2);
|
|
|
+ if ($to_brokerage > 0) {
|
|
|
+ $balance = bcadd($user['brokerage_price'], $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($user['brokerage_price'], $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;
|
|
|
+ }
|
|
|
+}
|