优化查询代码

This commit is contained in:
kuaifan 2022-01-14 12:19:50 +08:00
parent 9250ef6f65
commit ac26713f86
3 changed files with 24 additions and 24 deletions

View File

@ -1170,6 +1170,9 @@ class ProjectController extends AbstractController
$name = Request::input('name'); $name = Request::input('name');
// //
$task = ProjectTask::userTask($task_id, true, true); $task = ProjectTask::userTask($task_id, true, true);
if ($task->complete_at) {
return Base::retError('主任务已完成无法添加子任务');
}
// //
$task = ProjectTask::addTask([ $task = ProjectTask::addTask([
'name' => $name, 'name' => $name,
@ -1761,6 +1764,9 @@ class ProjectController extends AbstractController
$list = $builder->orderByDesc('created_at')->paginate(Base::getPaginate(100, 20)); $list = $builder->orderByDesc('created_at')->paginate(Base::getPaginate(100, 20));
$list->transform(function (ProjectLog $log) use ($task_id) { $list->transform(function (ProjectLog $log) use ($task_id) {
$timestamp = Carbon::parse($log->created_at)->timestamp; $timestamp = Carbon::parse($log->created_at)->timestamp;
if ($task_id === 0) {
$log->projectTask?->cancelAppend();
}
$log->time = [ $log->time = [
'ymd' => date(date("Y", $timestamp) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $timestamp), 'ymd' => date(date("Y", $timestamp) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $timestamp),
'hi' => date("h:i", $timestamp) , 'hi' => date("h:i", $timestamp) ,

View File

@ -17,6 +17,7 @@ use Illuminate\Support\Facades\DB;
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel query() * @method static \Illuminate\Database\Eloquent\Builder|AbstractModel query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore() * @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue() * @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Model|object|static|null cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|static with($relations) * @method static \Illuminate\Database\Eloquent\Builder|static with($relations)
* @method static \Illuminate\Database\Query\Builder|static select($columns = []) * @method static \Illuminate\Database\Query\Builder|static select($columns = [])
* @method static \Illuminate\Database\Query\Builder|static whereNotIn($column, $values, $boolean = 'and') * @method static \Illuminate\Database\Query\Builder|static whereNotIn($column, $values, $boolean = 'and')
@ -62,6 +63,15 @@ class AbstractModel extends Model
return $this->$key; return $this->$key;
} }
/**
* 取消附加值
* @return static
*/
protected function scopeCancelAppend()
{
return $this->setAppends([]);
}
/** /**
* 为数组 / JSON 序列化准备日期。 * 为数组 / JSON 序列化准备日期。
* @param DateTimeInterface $date * @param DateTimeInterface $date

View File

@ -112,7 +112,7 @@ class ProjectTask extends AbstractModel
public function getFileNumAttribute() public function getFileNumAttribute()
{ {
if (!isset($this->appendattrs['file_num'])) { if (!isset($this->appendattrs['file_num'])) {
$this->appendattrs['file_num'] = ProjectTaskFile::whereTaskId($this->id)->count(); $this->appendattrs['file_num'] = $this->parent_id > 0 ? 0 : ProjectTaskFile::whereTaskId($this->id)->count();
} }
return $this->appendattrs['file_num']; return $this->appendattrs['file_num'];
} }
@ -494,6 +494,8 @@ class ProjectTask extends AbstractModel
if (version_compare(Base::getClientVersion(), '0.6.0', '<')) { if (version_compare(Base::getClientVersion(), '0.6.0', '<')) {
throw new ApiException('当前版本过低'); throw new ApiException('当前版本过低');
} }
// 主任务
$mainTask = $this->parent_id > 0 ? self::find($this->parent_id) : null;
// 工作流 // 工作流
if (Arr::exists($data, 'flow_item_id')) { if (Arr::exists($data, 'flow_item_id')) {
if ($this->flow_item_id == $data['flow_item_id']) { if ($this->flow_item_id == $data['flow_item_id']) {
@ -564,6 +566,10 @@ class ProjectTask extends AbstractModel
} }
// 状态 // 状态
if (Arr::exists($data, 'complete_at')) { if (Arr::exists($data, 'complete_at')) {
// 子任务:主任务已完成时无法修改
if ($mainTask?->complete_at) {
throw new ApiException('主任务已完成,无法修改子任务状态');
}
if (Base::isDate($data['complete_at'])) { if (Base::isDate($data['complete_at'])) {
// 标记已完成 // 标记已完成
if ($this->complete_at) { if ($this->complete_at) {
@ -646,7 +652,6 @@ class ProjectTask extends AbstractModel
$end_at = Carbon::parse($end); $end_at = Carbon::parse($end);
if ($this->parent_id > 0) { if ($this->parent_id > 0) {
// 判断同步主任务时间(子任务时间 超出 主任务) // 判断同步主任务时间(子任务时间 超出 主任务)
$mainTask = self::find($this->parent_id);
if ($mainTask) { if ($mainTask) {
$isUp = false; $isUp = false;
if ($start_at->lt(Carbon::parse($mainTask->start_at))) { if ($start_at->lt(Carbon::parse($mainTask->start_at))) {
@ -669,7 +674,6 @@ class ProjectTask extends AbstractModel
} else { } else {
if ($this->parent_id > 0) { if ($this->parent_id > 0) {
// 清空子任务时间(子任务时间等于主任务时间) // 清空子任务时间(子任务时间等于主任务时间)
$mainTask = self::find($this->parent_id);
$this->start_at = $mainTask->start_at; $this->start_at = $mainTask->start_at;
$this->end_at = $mainTask->end_at; $this->end_at = $mainTask->end_at;
} }
@ -798,9 +802,7 @@ class ProjectTask extends AbstractModel
{ {
if ($this->parent_id > 0) { if ($this->parent_id > 0) {
$task = self::find($this->parent_id); $task = self::find($this->parent_id);
if ($task) { $task?->syncDialogUser();
$task->syncDialogUser();
}
return; return;
} }
if (empty($this->dialog_id)) { if (empty($this->dialog_id)) {
@ -872,24 +874,6 @@ class ProjectTask extends AbstractModel
return $this->appendattrs['has_owner']; return $this->appendattrs['has_owner'];
} }
/**
* 是否负责人
* @param bool $isParent 是父级任务的负责人也算
* @return bool
*/
public function isOwner($isParent = true) {
if ($this->owner) {
return true;
}
if ($isParent && $this->parent_id > 0) {
$parentTask = self::find($this->parent_id);
if ($parentTask?->owner) {
return true;
}
}
return false;
}
/** /**
* 标记已完成、未完成 * 标记已完成、未完成
* @param Carbon|null $complete_at 完成时间 * @param Carbon|null $complete_at 完成时间