You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
crmeb_php/app/command/dayValue.php

184 lines
7.7 KiB

<?php
declare (strict_types=1);
namespace app\command;
use app\common\model\community\Community;
use app\common\model\user\User;
use app\common\model\user\ValueContributionLog;
use Swoole\Coroutine\MySQL\Exception;
use think\console\Command;
use app\common\model\store\order\StoreOrderContribute;
use think\console\Input;
use think\console\input\Argument;
use app\common\model\user\CommunityAccrue;
use think\console\input\Option;
use app\common\model\user\User as userModel;
use think\console\Output;
use think\event\RouteLoaded;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Route;
use app\common\repositories\system\auth\MenuRepository;
// /www/server/php/74/bin/php /server/wwwroot/crmeb/think dayValue
class dayValue extends Command
{
protected function configure()
{
// 指令配置
$this->setName('dayValue')
->addArgument('dayValue', Argument::OPTIONAL, 'php think menu [1] / [2]')
->setDescription('社区等级结算');
}
protected function execute(Input $input, Output $output)
{
$yesterdayStart = date('Y-m-d H:i:s', strtotime('yesterday'));
$yesterdayEnd = date('Y-m-d H:i:s', strtotime('tomorrow') - 1);
$level = config('partner.community_level') ?? [];
$back_level = User::getDB()->where('community_level', '>', '0')->column('uid');
$userOrder = StoreOrderContribute::getDB()->field('uid,sum(order_profit) as user_profit')
->where('created_time', '>', $yesterdayStart)
->where('created_time', '<', $yesterdayEnd)
->whereNotIn('uid', $back_level)
->group('uid')->select()->toArray();
$newOrder = array_column($userOrder, null, 'uid');
$day_total = 0;
//查询社区分组,最高三级
foreach ($userOrder as $k => $v) {
$newOrder[$v['uid']]['child_user_profit'] = 0;
$day_total += $v['user_profit'];
//查询用户
$user = User::getDB()->where('uid', $v['uid'])->find();
if (!empty($user->spread_uid)) {
//查询二级
$f_user = User::getDB()->where('uid', $user->spread_uid)->find();
if (isset($newOrder[$user->spread_uid])) {
if (isset($newOrder[$user->spread_uid]['child_user_profit'])) {
$newOrder[$user->spread_uid]['child_user_profit'] += $v['user_profit'];
} else {
$newOrder[$user->spread_uid]['child_user_profit'] = $v['user_profit'];
}
} else {
$newOrder[$user->spread_uid] = [
'uid' => $user->spread_uid,
'user_profit' => 0,
'child_user_profit' => $v['user_profit']
];
}
if (!empty($f_user->spread_uid)) {
if (isset($newOrder[$f_user->spread_uid])) {
if (isset($newOrder[$f_user->spread_uid]['child_user_profit'])) {
$newOrder[$f_user->spread_uid]['child_user_profit'] += $v['user_profit'];
} else {
$newOrder[$f_user->spread_uid]['child_user_profit'] = $v['user_profit'];
}
} else {
$newOrder[$f_user->spread_uid] = [
'uid' => $f_user->spread_uid,
'user_profit' => 0,
'child_user_profit' => $v['user_profit']
];
}
}
}
}
try {
if (!empty($newOrder)) {
$user_log_data = [];
foreach ($newOrder as $ok => $ov) {
if ($ov['user_profit'] >= $ov['child_user_profit']) {
$use_value = $ov['user_profit'];
} else {
$use_value = $ov['child_user_profit'];
}
foreach ($level as $lk => $lv) {
if ($use_value >= $lv['min_price']) {
$level[$lk]['uid'][] = $ov['uid'];
continue 1;
}
}
}
foreach ($level as $nlk => $nlv) {
$temp_value = $order_value = bcmul((string)$day_total, $nlv['pay_community'], 2);
$old_value = Community::getDB()->where('community_id', $nlv['id'])->find();
if ($old_value && $old_value->value > 0) {
$temp_value = $temp_value + $old_value->value;
}
$back_uids = User::getDB()->where('community_level', $nlv['id'])->column('uid');
if (!empty($nlv['uid'])) {
$nlv['uid'] = array_merge($nlv['uid'], $back_uids);
} else {
$nlv['uid'] = $back_uids;
}
if (!empty($nlv['uid'])) {
$num = count($nlv['uid']);
$user_temp = bcdiv($temp_value, (string)$num, 2);
foreach ($nlv['uid'] as $uk => $uv) {
$user = User::getDB()->where('uid', $uv)->find();
//if($user->pay_price > 0){
$user->all_value = bcadd($user->all_value, $user_temp, 2);
$user->save();
$user_log_data[] = [
'type' => 8,
'types_of' => 1,
'num' => $user_temp,
'ctime' => date('Y-m-d H:i:s'),
'memo' => $nlv['alias'] . '贡献值',
'user_id' => $uv,
];
//}
}
} else {
$has = Community::getDB()->where('community_id', $nlv['id'])->find();
if (!$has) {
$has = new Community();
$has->community_id = $nlv['id'];
$has->value = $order_value;
} else {
$has->value = $has->value + $order_value;
}
$has->save();
}
}
//批量增加用户贡献值日志
ValueContributionLog::getDB()->insertAll($user_log_data);
}
} catch (\Exception $e) {
print_r($e->getLine() . "-" . $e->getMessage());
}
echo '执行结束';
}
public function getLastMonth()
{
// 获取当前日期
$currentDate = date('Y-m-d');
// 提取当前日期的年、月和日
$currentYear = date('Y', strtotime($currentDate));
$currentMonth = date('m', strtotime($currentDate));
// 计算上个月的年份和月份
$lastMonthYear = $currentYear - 1;
$lastMonth = $currentMonth - 1;
// 处理特殊情况:如果上个月的月份小于1,则年份减1,月份设为12
if ($lastMonth < 1) {
$lastMonthYear--;
$lastMonth = 12;
}
// 获取上个月的第一天和最后一天
$firstDayOfLastMonth = date('Y-m-01', strtotime("$lastMonthYear-$lastMonth-01"));
$lastDayOfLastMonth = date('Y-m-t', strtotime("$lastMonthYear-$lastMonth-01"));
return [$firstDayOfLastMonth, $lastDayOfLastMonth];
}
}