diff --git a/app/Http/Controllers/Api/ProjectController.php b/app/Http/Controllers/Api/ProjectController.php index 1cc52ebf..1f2eef3d 100755 --- a/app/Http/Controllers/Api/ProjectController.php +++ b/app/Http/Controllers/Api/ProjectController.php @@ -660,7 +660,7 @@ class ProjectController extends AbstractController { User::auth(); // - $builder = ProjectTask::with(['taskUser', 'taskTag']); + $builder = ProjectTask::select(ProjectTask::taskSelect)->with(['taskUser', 'taskTag']); // $parent_id = intval(Request::input('parent_id')); $project_id = intval(Request::input('project_id')); @@ -672,22 +672,22 @@ class ProjectController extends AbstractController // if ($parent_id > 0) { ProjectTask::userTask($parent_id); - $builder->whereParentId($parent_id); + $builder->leftData()->where('project_tasks.parent_id', $parent_id); } elseif ($project_id > 0) { Project::userProject($project_id); - $builder->whereParentId(0)->whereProjectId($project_id); + $builder->leftData()->where('project_tasks.project_id', $project_id); } else { - $builder->whereParentId(0)->authData(); + $builder->authData(); } // if ($name) { $builder->where(function($query) use ($name) { - $query->where("name", "like", "%{$name}%"); + $query->where("project_tasks.name", "like", "%{$name}%"); }); } // if (Base::isDateOrTime($time_before)) { - $builder->whereNotNull('end_at')->where('end_at', '<', Carbon::parse($time_before)); + $builder->whereNotNull('project_tasks.end_at')->where('project_tasks.end_at', '<', Carbon::parse($time_before)); } elseif (is_array($time)) { if (Base::isDateOrTime($time[0]) && Base::isDateOrTime($time[1])) { $builder->betweenTime(Carbon::parse($time[0])->startOfDay(), Carbon::parse($time[1])->endOfDay()); @@ -695,18 +695,18 @@ class ProjectController extends AbstractController } // if ($complete === 'yes') { - $builder->whereNotNull('complete_at'); + $builder->whereNotNull('project_tasks.complete_at'); } elseif ($complete === 'no') { - $builder->whereNull('complete_at'); + $builder->whereNull('project_tasks.complete_at'); } // if ($archived == 'yes') { - $builder->whereNotNull('archived_at'); + $builder->whereNotNull('project_tasks.archived_at'); } elseif ($archived == 'no') { - $builder->whereNull('archived_at'); + $builder->whereNull('project_tasks.archived_at'); } // - $list = $builder->orderByDesc('id')->paginate(Base::getPaginate(200, 100)); + $list = $builder->orderByDesc('project_tasks.id')->paginate(Base::getPaginate(200, 100)); // return Base::retSuccess('success', $list); } diff --git a/app/Models/ProjectTask.php b/app/Models/ProjectTask.php index cb92f319..ee5fdb52 100644 --- a/app/Models/ProjectTask.php +++ b/app/Models/ProjectTask.php @@ -7,6 +7,7 @@ use App\Module\Base; use App\Tasks\PushTask; use Arr; use Carbon\Carbon; +use DB; use Exception; use Hhxsv5\LaravelS\Swoole\Task\Task; use Illuminate\Database\Eloquent\SoftDeletes; @@ -54,6 +55,7 @@ use Request; * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProjectTaskUser[] $taskUser * @property-read int|null $task_user_count * @method static \Illuminate\Database\Eloquent\Builder|ProjectTask authData($userid = null, $owner = false) + * @method static \Illuminate\Database\Eloquent\Builder|ProjectTask leftData($userid = null) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTask betweenTime($start, $end) * @method static \Illuminate\Database\Eloquent\Builder|ProjectTask newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|ProjectTask newQuery() @@ -89,8 +91,12 @@ class ProjectTask extends AbstractModel { use SoftDeletes; + const taskSelect = [ + 'project_tasks.*', + 'project_task_users.owner', + ]; + protected $appends = [ - 'owner', 'file_num', 'msg_num', 'sub_num', @@ -100,22 +106,6 @@ class ProjectTask extends AbstractModel 'overdue', ]; - /** - * 是否我是负责人 - * @return bool - */ - public function getOwnerAttribute() - { - if (!isset($this->appendattrs['owner'])) { - if ($this->parent_id > 0) { - $this->appendattrs['owner'] = ProjectTaskUser::whereTaskId($this->id)->whereUserid(User::userid())->whereOwner(1)->exists(); - } else { - $this->appendattrs['owner'] = ProjectTaskUser::whereTaskPid($this->id)->whereUserid(User::userid())->whereOwner(1)->exists(); - } - } - return $this->appendattrs['owner']; - } - /** * 附件数量 * @return int @@ -275,11 +265,27 @@ class ProjectTask extends AbstractModel public function scopeAuthData($query, $userid = null, $owner = false) { $userid = $userid ?: User::userid(); - $query->whereIn('id', function ($qy) use ($owner, $userid) { - $qy->select('task_pid')->from('project_task_users')->where('userid', $userid); - if ($owner) { - $qy->where('owner', 1); - } + $query->join('project_task_users', 'project_tasks.id', '=', 'project_task_users.task_id') + ->where('project_task_users.userid', $userid); + if ($owner) { + $query->where('project_task_users.owner', 1); + } + return $query; + } + + /** + * 查询自己的任务 + * @param self $query + * @param null $userid + * @return self + */ + public function scopeLeftData($query, $userid = null) + { + $userid = $userid ?: User::userid(); + $query->leftJoin('project_task_users', function ($leftJoin) use ($userid) { + $leftJoin + ->on('project_task_users.userid', '=', DB::raw($userid)) + ->on('project_tasks.id', '=', 'project_task_users.task_id'); }); return $query; } @@ -295,11 +301,11 @@ class ProjectTask extends AbstractModel { $query->where(function ($q1) use ($start, $end) { $q1->where(function ($q2) use ($start) { - $q2->where('start_at', '<=', $start)->where('end_at', '>=', $start); + $q2->where('project_tasks.start_at', '<=', $start)->where('project_tasks.end_at', '>=', $start); })->orWhere(function ($q2) use ($end) { - $q2->where('start_at', '<=', $end)->where('end_at', '>=', $end); + $q2->where('project_tasks.start_at', '<=', $end)->where('project_tasks.end_at', '>=', $end); })->orWhere(function ($q2) use ($start, $end) { - $q2->where('start_at', '>', $start)->where('end_at', '<', $end); + $q2->where('project_tasks.start_at', '>', $start)->where('project_tasks.end_at', '<', $end); }); }); return $query; diff --git a/resources/assets/js/pages/manage/components/TaskDetail.vue b/resources/assets/js/pages/manage/components/TaskDetail.vue index f7e9d514..e808f966 100644 --- a/resources/assets/js/pages/manage/components/TaskDetail.vue +++ b/resources/assets/js/pages/manage/components/TaskDetail.vue @@ -518,7 +518,7 @@ export default { 'userId', 'projects', 'columns', - 'taskSubs', + 'tasks', 'taskContents', 'taskFiles', 'taskPriority', @@ -569,8 +569,8 @@ export default { if (!this.taskId) { return []; } - return this.taskSubs.filter(({parent_id}) => { - return parent_id == this.taskId + return this.tasks.filter(({parent_id}) => { + return parent_id > 0 && parent_id == this.taskId }).sort((a, b) => { return a.id - b.id; }); diff --git a/resources/assets/js/pages/manage/components/TaskRow.vue b/resources/assets/js/pages/manage/components/TaskRow.vue index b60dc7c5..c1e81d2d 100644 --- a/resources/assets/js/pages/manage/components/TaskRow.vue +++ b/resources/assets/js/pages/manage/components/TaskRow.vue @@ -184,12 +184,12 @@ export default { }, computed: { - ...mapState(['taskSubs', 'taskPriority', 'columns']), + ...mapState(['tasks', 'taskPriority', 'columns']), subTask() { return function(task_id) { - return this.taskSubs.filter(({parent_id}) => { - return parent_id == task_id + return this.tasks.filter(({parent_id}) => { + return parent_id > 0 && parent_id == task_id }).sort((a, b) => { return a.id - b.id; }); diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index 8d80102b..c428c4f7 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -393,7 +393,7 @@ export default { state.cacheDialogs = state.dialogs = []; state.cacheProjects = state.projects = []; state.cacheColumns = state.columns = []; - state.cacheTasks = state.tasks = state.taskSubs = []; + state.cacheTasks = state.tasks = []; // state.method.setStorage("cacheTablePanel", state.cacheTablePanel); state.method.setStorage("cacheServerUrl", state.cacheServerUrl); @@ -852,12 +852,11 @@ export default { dispatch("saveTask", task) }); } else if (state.method.isJson(data)) { - let key = data.parent_id > 0 ? 'taskSubs' : 'tasks'; - let index = state[key].findIndex(({id}) => id == data.id); + let index = state.tasks.findIndex(({id}) => id == data.id); if (index > -1) { - state[key].splice(index, 1, Object.assign(state[key][index], data)); + state.tasks.splice(index, 1, Object.assign(state.tasks[index], data)); } else { - state[key].push(data); + state.tasks.push(data); } // if (index > -1 && data.parent_id) { @@ -870,11 +869,9 @@ export default { dispatch("getTaskContent", data.id); } // - if (key == 'tasks') { - setTimeout(() => { - state.method.setStorage("cacheTasks", state.cacheTasks = state[key]); - }) - } + setTimeout(() => { + state.method.setStorage("cacheTasks", state.cacheTasks = state.tasks); + }) } }, @@ -888,28 +885,19 @@ export default { $A.execMainDispatch("forgetTask", task_id) // let index = state.tasks.findIndex(({id}) => id == task_id); - let key = 'tasks'; - if (index === -1) { - index = state.taskSubs.findIndex(({id}) => id == task_id); - key = 'taskSubs'; - } if (index > -1) { - if (state[key][index].parent_id) { - dispatch("getTaskOne", state[key][index].parent_id) + if (state.tasks[index].parent_id) { + dispatch("getTaskOne", state.tasks[index].parent_id) } - if (key == 'tasks') { - dispatch('getProjectOne', state[key][index].project_id) - } - state[key].splice(index, 1); + dispatch('getProjectOne', state.tasks[index].project_id) + state.tasks.splice(index, 1); } if (state.taskId == task_id) { state.taskId = 0; } - if (key == 'tasks') { - setTimeout(() => { - state.method.setStorage("cacheTasks", state.cacheTasks = state[key]); - }) - } + setTimeout(() => { + state.method.setStorage("cacheTasks", state.cacheTasks = state.tasks); + }) }, /** @@ -954,9 +942,6 @@ export default { if (data.project_id) { state.tasks = state.tasks.filter((item) => item.project_id != data.project_id || ids.includes(item.id)); } - if (data.parent_id) { - state.taskSubs = state.taskSubs.filter((item) => item.parent_id != data.parent_id || ids.includes(item.id)); - } } dispatch("saveTask", resData.data); // diff --git a/resources/assets/js/store/state.js b/resources/assets/js/store/state.js index cf46cbf8..c6dc0291 100644 --- a/resources/assets/js/store/state.js +++ b/resources/assets/js/store/state.js @@ -312,7 +312,6 @@ state.projectLoad = 0; state.columns = []; state.taskId = 0; state.tasks = []; -state.taskSubs = []; state.taskContents = []; state.taskFiles = []; state.taskLogs = [];