churf
2023-08-21 bdbce4e615d15f4f8423cc8ac5ee7fc66bb311ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-select v-model="curProject" value-key="id" filterable placeholder="所属项目" style="width: 400px;" class="filter-item" @change="queryByProjectId">
          <el-option
            v-for="item in projects"
            :key="item.id"
            :label="item.name"
            :value="item"
          >
            <span style="float: left">{{ item.name }}</span>
          </el-option>
        </el-select>
      </div>
      <div style="margin-bottom: 20px;">
        <el-card class="box-card">
          <el-descriptions title="项目信息" :column="3">
            <el-descriptions-item label="项目开始时间">{{ curProject === null ? '' : curProject.planStartDate }}</el-descriptions-item>
            <el-descriptions-item label="项目结束时间">{{ curProject === null ? '' : curProject.planEndDate }}</el-descriptions-item>
            <el-descriptions-item label="剩余天数">{{ curProject === null ? '0' : '0' }}</el-descriptions-item>
            <el-descriptions-item label="总任务数">{{ curProject === null ? '0' : curProject.totalTask }}</el-descriptions-item>
            <el-descriptions-item label="完成任务数">{{ curProject === null ? '0' : curProject.completeTask }}</el-descriptions-item>
            <el-descriptions-item label="任务完成比">{{ curProject === null ? '0%' : (parseFloat((curProject.completeTask / curProject.totalTask) * 100) ).toFixed(2)+ '%' }}</el-descriptions-item>
            <el-descriptions-item label="总工时数">{{ curProject === null ? '0.0' : curProject.totalHours }}</el-descriptions-item>
            <el-descriptions-item label="完成工时数">{{ curProject === null ? '0.0' : curProject.completeHours }}</el-descriptions-item>
            <el-descriptions-item label="工时完成比">{{ curProject === null ? '0%' : (parseFloat((curProject.completeHours / curProject.totalHours) * 100)).toFixed(2) + '%' }}</el-descriptions-item>
          </el-descriptions>
        </el-card>
      </div>
      <div>
        <!-- 搜索 -->
        <el-autocomplete v-model="query.name" class="filter-item" :fetch-suggestions="querySearch" placeholder="名称" style="width: 185px;" />
        <el-select v-model="query.taskStatuses" multiple collapse-tags filterable clearable placeholder="任务状态" style="width: 185px;" class="filter-item">
          <el-option
            v-for="item in dict[taskStatus]"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="query.performanceType" filterable clearable placeholder="绩效类型" style="width: 185px;" class="filter-item">
          <el-option
            v-for="item in dict[performanceType]"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="query.tagList" multiple collapse-tags filterable clearable placeholder="任务分类" style="width: 185px;" class="filter-item">
          <el-option
            v-for="item in tags"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <el-select v-model="query.userIds" multiple collapse-tags filterable clearable placeholder="所属员工" style="width: 185px;" class="filter-item">
          <el-option
            v-for="item in users"
            :key="item.id"
            :label="item.nickName"
            :value="item.id"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮,可以使用插槽方式, slot = 'left' or 'right'-->
      <crudOperation v-if="this.crud.query.flag" :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="110px">
          <el-form-item label="所属项目" prop="projectId">
            <el-select v-model="form.projectId" disabled placeholder="请选择" style="width: 340px;">
              <el-option
                v-for="item in projects"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="任务分类" prop="taskClassification" placeholder="请选择">
            <el-tag
              v-for="(tag, id) in form.tagList"
              :key="id"
              closable
              :disable-transitions="false"
              @close="handleClose(id)"
            >
              {{ tag }}
            </el-tag>
            <el-select
              v-if="inputVisible"
              ref="saveTagInput"
              v-model="inputValue"
              class="input-new-tag"
              filterable
              clearable
              allow-create
              default-first-option
              @keyup.enter.native="handleInputConfirm"
              @change="handleInputConfirm"
            >
              <el-option
                v-for="(item, id) in tags"
                :key="id"
                :label="item"
                :value="item"
              />
            </el-select>
            <el-button v-else class="button-new-tag" size="small" @click="showInput">请选择标签</el-button>
          </el-form-item>
          <el-form-item label="绩效类型" prop="performanceType">
            <el-select v-model="form.performanceType" filterable clearable placeholder="请选择" style="width: 340px;">
              <el-option
                v-for="item in dict[performanceType]"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description" :rows="3" type="textarea" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="计划开始时间" prop="planStartTime">
            <el-date-picker v-model="form.planStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" :default-time="$store.state.user.user.sysRostering.workStartTime + ':00'" :picker-options="planStartTimePickerOptions" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="计划结束时间" prop="planEndTime">
            <el-date-picker v-model="form.planEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" :default-time="$store.state.user.user.sysRostering.workEndTime + ':00'" :picker-options="planEndTimePickerOptions" style="width: 340px;" />
          </el-form-item>
          <el-form-item label="计划工时" prop="planHours">
            <el-input v-model="form.planHours" style="width: 340px;">
              <template slot="append">小时</template>
            </el-input>
          </el-form-item>
          <el-form-item label="任务成员" prop="containUserList">
            <el-select v-model="form.containUserList" multiple filterable clearable placeholder="请选择" style="width: 340px;" @change="getContainUserList">
              <el-option
                v-for="item in users"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              >
                <span style="float: left">{{ item.nickName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.levelName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="任务负责人" prop="userId">
            <el-select v-model="form.userIdList" :no-data-text=" '没有任务成员' " multiple filterable clearable placeholder="请选择" style="width: 340px;">
              <el-option
                v-for="item in users"
                v-show="form.containUserList != undefined && form.containUserList.some(animal => animal === item.id)"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              >
                <span style="float: left">{{ item.nickName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.levelName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        @selection-change="crud.selectionChangeHandler"
        @row-click="getTaskUser"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column :show-overflow-tooltip="true" prop="name" label="名称" fixed="left" width="180" />
        <el-table-column :show-overflow-tooltip="true" label="计划工时" width="80">
          <template slot-scope="scope">
            {{ scope.row.sumPlanHours }}
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="planStartTime" label="计划开始时间" width="140" />
        <el-table-column :show-overflow-tooltip="true" prop="planEndTime" label="计划结束时间" width="140" />
        <el-table-column :show-overflow-tooltip="true" prop="taskStatus" label="任务状态" width="80">
          <template slot-scope="scope">
            <el-tag>{{ dict.label[taskStatus][scope.row.taskStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="performanceType" label="绩效类型" width="80">
          <template slot-scope="scope">
            <el-tag>{{ dict.label[performanceType][scope.row.performanceType] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="tagList" label="任务分类" width="240">
          <template slot-scope="scope">
            <template>
              <el-tag v-for="tag in scope.row.tagList" :key="tag">{{ tag }}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="tagList" label="任务负责人" width="240">
          <template slot-scope="scope">
            <el-tag v-for="userId in scope.row.userIdList" :key="userId">{{ getUserNameById(userId) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="description" label="描述" />
        <el-table-column v-if="checkPer(['admin','task:divide','task:edit','task:del']) && this.crud.query.flag === true" label="操作" width="180px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>
 
<script>
import crudTask from '@/api/task/task'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import constant from '@/utils/constant'
import crudUser from '@/api/system/user'
import crudProject from '@/api/project/project'
 
const defaultForm = { id: null, name: null, projectId: null, userIdList: [], parent: { id: null }, priority: null, tagList: [], performanceType: null, description: null, planStartTime: null, planEndTime: null, planHours: null, pieceRate: 0, planWorkload: 0, planWorkloadUnit: null, taskStatus: null, remarks: null, version: null }
export default {
  name: 'Task',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: [constant.PRIORITY, constant.TAST_CLASSIFICATION, constant.PERFORMANCE_TYPE, constant.STANDARD_WORKLOAD_UNIT, constant.TASK_STATUS],
  cruds() {
    return CRUD({ title: '任务', url: 'api/task', idField: 'id', sort: 'id,desc', crudMethod: { ...crudTask }})
  },
  data() {
    // 指针重指向
    const _this = this
    // 时长(万内)验证
    var durationValidate = (rule, value, callback) => {
      const reg = /^([0-9]|[1-9]\d{1,3})(\.\d{1,2})?$/
      if (value != null && value !== '' && !reg.test(value)) {
        callback(new Error('请输入 一万 以内的正整数或两位小数'))
      } else {
        callback()
      }
    }
    return {
      permission: {
        add: ['admin', 'task:add'],
        edit: ['admin', 'task:edit'],
        del: ['admin', 'task:del'],
        download: ['admin', 'task:download'],
        divide: ['admin', 'task:divide'],
        seeDivide: ['admin', 'task:seeDivide'],
        seeDaliy: ['admin', 'task:seeDaliy']
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { max: 100, message: '最大长度 100 个字符', trigger: 'blur' }
        ],
        projectId: [
          { required: true, message: '所属项目不能为空', trigger: 'change' }
        ],
        performanceType: [
          { required: true, message: '绩效类型不能为空', trigger: 'change' }
        ],
        pieceRate: [
          { validator: durationValidate, trigger: 'blur' }
        ],
        planStartTime: [
          { required: true, message: '计划开始时间不能为空', trigger: 'change' }
        ],
        planEndTime: [
          { required: true, message: '计划结束时间不能为空', trigger: 'change' }
        ],
        planHours: [
          { required: true, message: '计划工时不能为空', trigger: 'blur' },
          { validator: durationValidate, trigger: 'blur' }
        ],
        planWorkload: [
          { validator: durationValidate, trigger: 'blur' }
        ],
        taskStatus: [
          { required: true, message: '任务状态不能为空', trigger: 'change' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        { key: 'projectId', display_name: '所属项目' },
        { key: 'priority', display_name: '优先度' },
        { key: 'taskStatus', display_name: '任务状态' }
      ],
      // 计划开始时间选项
      planStartTimePickerOptions: {
        // 禁用日期
        disabledDate(time) {
          return _this.form.planEndTime != null && _this.form.planEndTime !== '' ? time.getTime() > new Date(_this.form.planEndTime).getTime() : false
        }
      },
      // 计划结束时间选项
      planEndTimePickerOptions: {
        // 禁用日期
        disabledDate(time) {
          return _this.form.planStartTime != null && _this.form.planStartTime !== '' ? time.getTime() < new Date(_this.form.planStartTime).getTime() - (86400000 - 1) : false
        }
      },
      // 字典:优先度
      priority: constant.PRIORITY,
      // 字典:任务分类
      taskClassification: constant.TAST_CLASSIFICATION,
      // 字典: 绩效类型
      performanceType: constant.PERFORMANCE_TYPE,
      // 字典:标准工作量单位
      standardWorkloadUnit: constant.STANDARD_WORKLOAD_UNIT,
      // 字典:任务状态
      taskStatus: constant.TASK_STATUS,
      // 字典:批准状态-未承认
      approveStatusUnrecognized: constant.APPROVE_STATUS_UNRECOGNIZED,
      // 角色级别:超级管理员
      roleLevelSuper: constant.ROLE_LEVEL_SUPER,
      // 角色级别:总经理
      roleLevelGeneral: constant.ROLE_LEVEL_GENERAL,
      // CRUD状态
      crudStatusPrepared: CRUD.STATUS.PREPARED,
      // 用户最大角色级别
      userRolesLevelMin: null,
      // 项目列表
      projects: [],
      // 当前项目
      curProject: null,
      // 父级任务列表
      pidTasks: [],
      // 员工列表
      users: [],
      // 用户列表
      tags: [],
      // tag标签
      inputVisible: false,
      inputValue: null,
      containUserList: []
    }
  },
  created() {
    this.crud.optShow.reset = false
  },
  mounted() {
    // 获取项目
    this.getProject()
    // 获取员工
    this.getUser()
    this.getTags()
  },
  methods: {
    getContainUserList(e) {
      console.log(e)
      this.form.containUserList = e
    },
    getTags() {
      crudTask.getTags().then(res => {
        this.tags = res
      })
    },
    // 钩子:在获取表格数据之前执行,false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      if (!this.crud.query.projectId) {
        return false
      }
      // 用户角色级别数组
      const userRolesLevel = this.$store.state.user.user.roles.map(({ level }) => level)
      // 用户最大角色级别
      this.userRolesLevelMin = Math.min(...userRolesLevel)
      return true
    },
    [CRUD.HOOK.beforeSubmit]() {
      if (!this.form.taskStatus) {
        this.form.taskStatus = 'task_status_not_started'
      }
      if (this.inputValue !== null) {
        this.form.tagList.push(this.inputValue)
      }
      return true
    },
    [CRUD.HOOK.afterSubmit]() {
      this.inputValue = null
      this.getTags()
    },
    // 钩子:新建 - 之前
    [CRUD.HOOK.beforeToCU]() {
      this.form.projectId = this.crud.query.projectId
      // 父级任务列表
      this.pidTasks = []
      crudTask.getAll({ projectId: this.form.projectId }).then(res => {
        // 父级任务列表
        this.pidTasks = res
      })
    },
    queryByProjectId(project) {
      this.crud.resetQuery(false)
      this.crud.query.projectId = project.id
      this.crud.query.flag = project.flag
      this.crud.toQuery()
      this.getProjectTotal()
    },
    getTaskUser(even) {
      this.containUserList = []
      var containList = even.containUserList
      for (var i = 0; i < this.users.length; i++) {
        for (var j = 0; j < containList.length; j++) {
          if (containList[j] === this.users[i].id) {
            this.containUserList.push(this.users[i])
          }
        }
      }
    },
    handleClose(id) {
      this.form.tagList.splice(id, 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.focus() // .$refs.input
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue !== '') {
        this.form.tagList.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = null
    },
    handleSelect(item) {
      this.inputValue = item.value
      this.$refs.saveTagInput.$refs.input.focus()
    },
    querySearchAsync(queryString, cb) {
      crudTask.getTags(queryString ? { tagName: queryString } : {}).then(res => {
        const tagMap = []
        res.forEach(tag => {
          tagMap.push({ value: tag })
        })
        cb(tagMap)
      })
    },
    // 获取用户
    getUser() {
      // 获取用户
      crudUser.getAll().then(res => {
        // 用户列表
        this.users = res
      })
    },
    getUserNameById(userId) {
      const userList = this.users.filter(user => user.id === userId)
      return userList.length === 0 ? '' : userList[0].nickName
    },
    // 获取项目
    getProject() {
      // 获取项目
      crudProject.getRelevant({
        'projectStatusIn': ['project_status_not_started', 'project_status_under_way'],
        'approve_status': 'approve_status_acknowledged'
      }).then(res => {
        // 项目列表
        this.projects = res
      })
    },
    // 树形表数据加载
    load(tree, treeNode, resolve) {
      resolve(tree.children)
    },
    getProjectTotal() {
      this.curProject.totalTask = 0
      this.curProject.totalHours = 0.0
      this.curProject.completeTask = 0
      this.curProject.completeHours = 0.0
      crudTask.getProjectTotal({ 'projectId': this.crud.query.projectId }).then(res => {
        let totalTask = 0
        let totalHours = 0.0
        let completeTask = 0
        let completeHours = 0.0
        res.forEach(item => {
          totalTask += item.count
          totalHours += item.planHours
          if (item._id === 'task_status_completed') {
            completeTask += item.count
            completeHours += item.planHours
          }
        })
        this.curProject.totalTask = totalTask
        this.curProject.totalHours = totalHours
        this.curProject.completeTask = completeTask
        this.curProject.completeHours = completeHours
      })
    },
    // 项目变更
    projectChange() {
      // 表单赋值
      this.form.parent.id = null
      // 判断表单项目是否为空
      if (this.form.projectId == null || this.form.projectId === '') {
        // 父级任务列表
        this.pidTasks = []
      } else {
        // 获取父级任务
        crudTask.getAll({ projectId: this.form.projectId }).then(res => {
          // 父级任务列表
          this.pidTasks = res
        })
      }
    },
    // 输入框获取焦点时调用的方法
    async querySearch(queryString, cb) {
      let results = []
      if (queryString === null || queryString === '') {
        cb(results)
      } else {
        results = await crudTask.getNames({ projectId: this.crud.query.projectId, name: queryString })
        cb(results)
      }
    }
  }
}
</script>
 
<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>