hasMany(ReportReceive::class, "rid"); } public function receivesUser(): BelongsToMany { return $this->belongsToMany(User::class, ReportReceive::class, "rid", "userid") ->withPivot("receive_time", "read"); } public function sendUser() { return $this->hasOne(User::class, "userid", "userid"); } public function getTypeAttribute($value): string { return match ($value) { Report::WEEKLY => "周报", Report::DAILY => "日报", default => "", }; } public function getContentAttribute($value): string { return htmlspecialchars_decode($value); } public function getReceivesAttribute() { if (!isset($this->appendattrs['receives'])) { $this->appendattrs['receives'] = empty( $this->receivesUser ) ? [] : array_column($this->receivesUser->toArray(), "userid"); } return $this->appendattrs['receives']; } /** * 获取单条记录 * @param $id * @param User|null $user * @return Report|Builder|Model|object|null * @throw ApiException */ public static function getOne($id, User $user = null) { $user === null && $user = User::auth(); $one = self::whereUserid($user->userid)->whereId($id)->first(); if ( empty($one) ) throw new ApiException("记录不存在"); return $one; } /** * 获取最后一条提交记录 * @param User|null $user * @return Builder|Model|\Illuminate\Database\Query\Builder|object */ public static function getLastOne(User $user = null) { $user === null && $user = User::auth(); $one = self::whereUserid($user->userid)->orderByDesc("created_at")->first(); if ( empty($one) ) throw new ApiException("记录不存在"); return $one; } /** * 生成唯一标识 * @param $type * @param $offset * @param Carbon|null $time * @return string */ public static function generateSign($type, $offset, Carbon $time = null): string { $user = User::auth(); $now_dt = $time === null ? Carbon::now() : $time; $time_s = match ($type) { Report::WEEKLY => function() use ($now_dt, $offset) { // 如果设置了周期偏移量 empty( $offset ) || $now_dt->subWeeks( abs( $offset ) ); $now_dt->startOfWeek(); // 设置为当周第一天 return $now_dt->year . $now_dt->weekOfYear . $now_dt->month . $now_dt->weekOfMonth; }, Report::DAILY => function() use ($now_dt, $offset) { // 如果设置了周期偏移量 empty( $offset ) || $now_dt->subDays( abs( $offset ) ); return $now_dt->year . $now_dt->dayOfYear . $now_dt->month . $now_dt->daysInMonth; }, default => "", }; return md5( $user->userid . ( is_callable($time_s) ? $time_s() : "" ) . $type ); } }