master
cody 3 months ago
parent 515219af32
commit c8ae055341

@ -2,6 +2,8 @@
namespace App\Console\Commands;
use App\Models\Course;
use App\Models\CourseSign;
use App\Models\User;
use App\Repositories\MeetRepository;
use Illuminate\Console\Command;
@ -40,13 +42,38 @@ class UpdateUserNo extends Command
*/
public function handle()
{
$users = User::whereNull('no')->get();
foreach ($users as $user) {
$no = User::updateNo($user->id);
$this->info($no . '更新成功');
// 已经开始的课程日期
$dateList = Course::whereNotNull('start_date')
->where('start_date', '<=', date('Y-m-d'))
->orderBy('start_date')
->groupBy('start_date')
->pluck('start_date')
->toArray();
foreach ($dateList as $date) {
$courses = Course::with(['courseSigns' => function ($query) {
$query->where('status', 1);
}])->where('start_date', $date)
->orderBy('start_date')
->get();
$i = 1;
// 编号前缀
$prefix = date('Ymd', strtotime($date));
foreach ($courses as $course) {
foreach ($course->courseSigns as $sign) {
$user = User::find($sign->user_id);
if ($user->no) {
continue;
}
$no = $prefix . str_pad($i, 3, '0', STR_PAD_LEFT);
// 更新用户编号
$user->no = $no;
$user->save();
$this->info($no);
$i++;
}
}
}
return $this->info('更新完成');
}
}

@ -105,7 +105,7 @@ class SupplyDemandController extends CommonController
}
$detail = SupplyDemand::with([
'user' => function ($query) {
$query->select('id', 'nickname', 'name', 'headimgurl');
$query->select('id', 'nickname', 'name', 'headimgurl', 'username');
}
])->find($all['id']);
// 增加view_count

@ -74,8 +74,6 @@ class UserController extends CommonController
$score = Config::getValueByKey('share_score');
ScoreLog::add($pid, $score, '分享获得');
}
// 更新编号
User::updateNo($user->id);
}
$token = $user->createToken("mobile-token")->plainTextToken;
return $this->success(compact('token'));

@ -182,49 +182,4 @@ class User extends Authenticatable implements Auditable
return $user->appointment_total - $useTotal >= 0 ? $user->appointment_total - $useTotal : 0;
}
/**
* 更新用户编号
*/
public static function updateNo($userId)
{
// todo::编号可能回重复,还需要详细排查
$user = self::find($userId);
if (!empty($user->no)) {
return false;
}
// 获取最早一条审核通过的报名数据
$courseSigns = CourseSign::with('course')
->where('user_id', $userId)
->where('status', 1)
->orderBy('created_at', 'asc')
->first();
if (empty($courseSigns)) {
return false;
}
if (empty($courseSigns->course->start_date)) {
return false;
}
// 编号前缀
$prefix = date('Ymd', strtotime($courseSigns->course->start_date));
// 获取同一天开始的所有课程
$course = Course::where('start_date', $courseSigns->course->start_date)->orderBy('created_at', 'asc')->get();
// 获取同一天开始所有课程的报名信息
$courseSignsList = CourseSign::whereIn('id', function ($query) use ($course) {
$query->from('course_signs')
->where('status', 1)
->whereIn('course_id', $course->pluck('id'))
->selectRaw('MIN(id)')
->groupBy('user_id');
})->orderBy('created_at', 'asc')->get();
// 获取当前用户id在$courseSigns中第几位
$index = $courseSignsList->search(function ($item) use ($user) {
return $item->user_id == $user->id;
});
$no = $prefix . str_pad($index + 1, 3, '0', STR_PAD_LEFT);
$user->no = $no;
$user->save();
return $user->no;
}
}

Loading…
Cancel
Save