mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-11 10:33:54 +00:00
132 lines
5.4 KiB
PHP
132 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Module\Base;
|
|
|
|
/**
|
|
* App\Models\ProjectUser
|
|
*
|
|
* @property int $id
|
|
* @property int|null $project_id 项目ID
|
|
* @property int|null $userid 成员ID
|
|
* @property int|null $owner 是否负责人
|
|
* @property \Illuminate\Support\Carbon|null $top_at 置顶时间
|
|
* @property int|null $sort 排序(ASC)
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \App\Models\Project|null $project
|
|
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereOwner($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereProjectId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereSort($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereTopAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectUser whereUserid($value)
|
|
* @mixin \Eloquent
|
|
*/
|
|
class ProjectUser extends AbstractModel
|
|
{
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function project(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
{
|
|
return $this->hasOne(Project::class, 'id', 'project_id');
|
|
}
|
|
|
|
/**
|
|
* 移交项目身份
|
|
* @param $originalUserid
|
|
* @param $newUserid
|
|
* @return void
|
|
*/
|
|
public static function transfer($originalUserid, $newUserid)
|
|
{
|
|
$projectIds = [];
|
|
// 移交项目身份
|
|
self::whereUserid($originalUserid)->chunkById(100, function ($list) use ($originalUserid, $newUserid, &$projectIds) {
|
|
/** @var self $item */
|
|
foreach ($list as $item) {
|
|
$row = self::whereProjectId($item->project_id)->whereUserid($newUserid)->first();
|
|
if ($row) {
|
|
// 已存在则删除原数据,判断改变已存在的数据
|
|
$row->owner = max($row->owner, $item->owner);
|
|
$row->save();
|
|
$item->delete();
|
|
} else {
|
|
// 不存在则改变原数据
|
|
$item->userid = $newUserid;
|
|
$item->save();
|
|
}
|
|
if ($item->project) {
|
|
if ($item->project->personal) {
|
|
$name = User::userid2nickname($originalUserid) ?: ('ID:' . $originalUserid);
|
|
$item->project->name = "【{$name}】{$item->project->name}";
|
|
$item->project->save();
|
|
}
|
|
$item->project->addLog("移交项目身份", [
|
|
'change' => [
|
|
[
|
|
'type' => 'user',
|
|
'data' => $originalUserid
|
|
],
|
|
[
|
|
'type' => 'user',
|
|
'data' => $newUserid
|
|
],
|
|
],
|
|
]);
|
|
$item->project->syncDialogUser();
|
|
$projectIds[] = $item->project_id;
|
|
}
|
|
}
|
|
});
|
|
// 移交工作流状态负责人
|
|
if ($projectIds) {
|
|
ProjectFlowItem::whereIn('project_id', $projectIds)->chunkById(100, function ($list) use ($originalUserid, $newUserid) {
|
|
/** @var ProjectFlowItem $item */
|
|
foreach ($list as $item) {
|
|
if (in_array($originalUserid, $item->userids)) {
|
|
$userids = array_values(array_diff($item->userids, [$originalUserid]));
|
|
$item->userids = Base::array2json(array_merge($userids, [$newUserid]));
|
|
$item->save();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 退出项目
|
|
*/
|
|
public function exitProject()
|
|
{
|
|
ProjectTaskUser::whereProjectId($this->project_id)
|
|
->whereUserid($this->userid)
|
|
->chunk(100, function ($list) {
|
|
$tastIds = [];
|
|
/** @var ProjectTaskUser $item */
|
|
foreach ($list as $item) {
|
|
$item->delete();
|
|
if (!in_array($item->task_pid, $tastIds)) {
|
|
$tastIds[] = $item->task_pid;
|
|
$item->projectTask?->syncDialogUser();
|
|
}
|
|
}
|
|
});
|
|
$this->delete();
|
|
}
|
|
}
|