step.vue 12.2 KB
Newer Older
arvin's avatar
arvin committed
1
<template>
arvin's avatar
arvin committed
2
  <div class="TfMomWebStep-model">
arvin's avatar
arvin committed
3
    <div :id="ganttElId" style="height: 100%;width: 100%;position: relative;" />
arvin's avatar
arvin committed
4 5 6 7
  </div>
</template>

<script>
arvin's avatar
arvin committed
8 9
import config from './config'
import data from './data'
arvin's avatar
arvin committed
10
import CsvExportor from 'csv-exportor'
arvin's avatar
arvin committed
11 12
export default {
  name: 'TfMomWebStep',
arvin's avatar
arvin committed
13 14 15 16 17 18 19 20 21 22 23
  mixins: [config, data],
  props: {
    node: {
      type: Object,
      default: () => null
    },
    showFlag: {
      type: Boolean,
      default: false
    }
  },
arvin's avatar
arvin committed
24 25
  data() {
    return {
arvin's avatar
arvin committed
26 27
      // eslint-disable-next-line no-undef
      gantt: Gantt.getGanttInstance(),
arvin's avatar
arvin committed
28 29 30 31 32 33 34 35 36 37 38
      ganttElId: 'unit-gantt',
      type: 'month-week',
      ganttData: [],
      initGanttFlag: false,
      params: null,
      currentId: ''
    }
  },
  computed: {
    columns() {
      return [
arvin's avatar
arvin committed
39 40 41 42
        { name: 'index', label: '节点', width: 30, align: 'center', resize: true },
        { name: 'text', label: '站位号', width: 40, align: 'center', resize: true },
        { name: 'duration', label: '工期(天)', width: 30, align: 'center', resize: true, editor: { type: 'number', map_to: 'duration', min: 0, max: 100 }},
        { name: '', label: '前置', width: 40, align: 'center', resize: true, template: (task) => {
arvin's avatar
arvin committed
43 44 45 46 47 48 49 50 51 52
          var links = task.$target
          var labels = []
          for (var i = 0; i < links.length; i++) {
            var link = this.gantt.getLink(links[i])
            const label = this.gantt.getTask(link.source).index
            if (!labels.includes(label)) {
              labels.push(label)
            }
          }
          return '<span>' + labels.join(',') + '</span>'
arvin's avatar
arvin committed
53 54
        },
        editor: { type: 'custom_datepicker_editor' }
arvin's avatar
arvin committed
55
        }
arvin's avatar
arvin committed
56
      ]
arvin's avatar
arvin committed
57 58 59 60 61 62
    }
  },
  mounted() {

  },
  methods: {
arvin's avatar
arvin committed
63 64
    configLayout() {
      const ganttAg = this.gantt
arvin's avatar
arvin committed
65 66
      this.setColumnsConfig()
      this.usePlugins()
arvin's avatar
arvin committed
67 68 69
      const getInput = function(node) {
        return node.querySelector('input')
      }
arvin's avatar
arvin committed
70

arvin's avatar
arvin committed
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
      const zoomConfig = {
        levels: [
          [
            {
              unit: 'month',
              // step: 1,
              format: function(date) {
                var month =
                  (new Date(date).getFullYear() - 2007) * 12 +
                  new Date(date).getMonth() +
                  1
                return month + '月' // ganttAg.date.getWeek(date)
              }
            },
            {
              unit: 'week',
              format: function(date) {
                var newDate = new Date(date)
                var oldDate = new Date('2007/01/01')
                var days = (newDate - oldDate) / (1 * 24 * 60 * 60 * 7 * 1000)
                return Math.ceil(days + 1) + '周' // ganttAg.date.getWeek(date)
              }
            }
          ],
          [
            {
              unit: 'week',
              format: function(date) {
                var newDate = new Date(date)
                var oldDate = new Date('2007/01/01')
                var days = (newDate - oldDate) / (1 * 24 * 60 * 60 * 7 * 1000)
                return Math.ceil(days + 1) + '周' // ganttAg.date.getWeek(date)
              }
            },
            {
              unit: 'day',
              format: function(date) {
                var newDate = new Date(date)
                var oldDate = new Date('2007/01/01')
                var days = (newDate - oldDate) / (1 * 24 * 60 * 60 * 1000)
                return Math.ceil(days + 1) + '日' // ganttAg.date.getWeek(date)
              }
            }
          ]
        ],
        useKey: 'ctrlKey',
        trigger: 'wheel',
        element: function() {
          return ganttAg.$root.querySelector('.gantt_task')
        }
      }

      ganttAg.config.date_format = '%Y-%m-%d'
      // ganttAg.config.duration_unit = 'minute'
      ganttAg.config.branch_loading = true
      ganttAg.config.scale_height = 70
      ganttAg.config.show_task_cells = true
      ganttAg.config.autofit = false
arvin's avatar
arvin committed
129
      ganttAg.config.fit_tasks = false
arvin's avatar
arvin committed
130 131 132 133 134
      ganttAg.config.drag_move = false
      ganttAg.config.details_on_dblclick = false
      ganttAg.config.auto_scheduling = true
      // 指定在选择显示所选任务时是否滚动时间线区域
      ganttAg.config.scroll_on_click = true
arvin's avatar
arvin committed
135
      ganttAg.config.min_column_width = 40
arvin's avatar
arvin committed
136 137 138 139 140
      ganttAg.config.autoscroll = true
      ganttAg.config.drag_progress = false
      ganttAg.ext.zoom.init(zoomConfig)
      // 允许通过拖放来调整任务大小
      ganttAg.config.drag_resize = true
arvin's avatar
arvin committed
141
      ganttAg.config.highlight_critical_path = true
arvin's avatar
arvin committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      ganttAg.config.editor_types.custom_datepicker_editor = {
        show: function(id, column, config, placeholder) {
          var html =
            "<div><input type='text' id='datepicker' name='" +
            column.name +
            "'></div>"
          placeholder.innerHTML = html
        },
        hide: function() {},
        get_value: function(id, column, node) {
          return getInput(node).value || 0
        },
        is_valid: function(value, id, column, node) {},
        focus: function(node) {},
        set_value: function(value, id, column, node) {
          // eslint-disable-next-line eqeqeq
arvin's avatar
arvin committed
158
          var data = ganttAg.getTableData().data.links
arvin's avatar
arvin committed
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
            .filter((p) => p.target === id)
            .map((p) => ganttAg.getWBSCode(ganttAg.getTask(p.source)))
          ganttAg.getWBSCode(ganttAg.getTask(id))
          getInput(node).value = data.join(',')
        },
        is_changed: function(value, id, column, node) {
          var olddata = ganttAg
            .getTask(id)
            .$target.map((p) =>
              ganttAg.getWBSCode(ganttAg.getTask(ganttAg.getLink(p).source))
            )
          if (olddata.join() === getInput(node).value) return
          var newdata = getInput(node).value
            ? getInput(node).value.split(',')
            : []
          var oldLinks = ganttAg.getTask(id).$target.map((p) => {
            return ganttAg.getLink(p)
          })
          olddata
            .filter((p) => !newdata.includes(p))
            .forEach((p) => {
              var link = oldLinks.find(
                (t) => Number(t.source) === ganttAg.getTaskByWBSCode(p).id
              )
              if (link) {
                ganttAg.deleteLink(link.id)
              }
            })
          newdata
            .filter((p) => !olddata.includes(p))
            .map((p) => ganttAg.getTaskByWBSCode(p))
            .forEach((p) => {
              ganttAg.addLink({
                source: p.id,
                target: id,
                type: '0'
              })
            })
          ganttAg.render()
        }
      }
      ganttAg.templates.link_class = function(link) {
        if (link.isCritical) {
          return 'critical_path'
        }
      }
      ganttAg.templates.tooltip_text = function(start, end, task) {
        return `<b>站位: </b>${task.text}<br><b>工期: </b> ${task.duration}天`
      }
      ganttAg.templates.link_description = function(link) {
        // 处理插件首次触发传入空值
        // if (!link) return false
        var from = ganttAg.getTask(link.source) || ''
        var to = ganttAg.getTask(link.target) || ''
        var text = '<b>' + from.text + '</b> -'
        text += '<b>' + to.text + '</b>的 '
        return text
      }
      ganttAg.templates.task_class = function(start, end, task) {
        return 'workday_over'
      }
      // 删除连接后触发
      ganttAg.attachEvent('onAfterLinkDelete', function(id, item) {
        var target = ganttAg.getTask(item.target)
        var targetLength = target.$target.length
        if (!targetLength) {
          var data = {
arvin's avatar
arvin committed
226 227
            links: ganttAg.getTableData().data.links,
            data: ganttAg.getTableData().data.data.map((p) => {
arvin's avatar
arvin committed
228 229 230 231 232
              delete p.end_date
              p.start_date = new Date(2007, 0, 1)
              return p
            })
          }
arvin's avatar
arvin committed
233
          ganttAg.clearAll()
arvin's avatar
arvin committed
234 235 236 237 238 239 240 241 242 243
          ganttAg.parse(data)
        }
      })
      // 在将新链接添加到甘特图之前触发
      ganttAg.attachEvent('onBeforeLinkAdd', function(id, link) {
        if (link.type !== '0') {
          return false
        }
        return true
      })
arvin's avatar
arvin committed
244
      ganttAg.attachEvent('onAfterBatchUpdate', function(action) {
arvin's avatar
arvin committed
245
        ganttAg.autoSchedule()
arvin's avatar
arvin committed
246
      })
arvin's avatar
arvin committed
247 248 249 250
      this.$nextTick(() => {
        ganttAg.init(this.ganttElId)
      })
    },
arvin's avatar
arvin committed
251 252 253
    refreshGantt() {
      const val = this.ganttData
      if (!val || !val.length) {
254
        this.gantt.clearAll()
arvin's avatar
arvin committed
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
        return
      }
      const params = {
        data: [],
        links: []
      }
      val.forEach((item, index) => {
        const link = item.extProcessExecutorRoutes && item.extProcessExecutorRoutes[0]
        params.data.push({
          index: index + 1,
          text: item.serialNumber,
          id: item.id,
          parenId: link && link.prevNodeId || '',
          start_date: '2007-1-1',
          type: 'task',
          duration: item.workHour ? Number(item.workHour / 8) : 0
        })
        if (link) {
          params.links.push({
            cid: link.id,
            tid: link.currNodeId,
            source: link.prevNodeId,
            target: link.currNodeId,
            type: '0',
            isCritical: link.isCritical

          })
        }
      })
      this.params = params
      this.gantt.clearAll()
      this.gantt.parse(this.params)
    },
    getData() {
      this.currentId = this.node.id
      const params = {
        'searchItems': { 'items': [{ 'fieldName': 'aircraftSortiesId', 'operator': 'EQ', 'value': this.node.id }] },
        'sortItem': [{ 'fieldName': 'serialNumber', 'sortOrder': 'asc' }],
        'openProps': [
          {
            'name': 'extProcessExecutorRoutes'
          }
        ]
      }

      this.$api.searchApi('ExtPosition', params).then(res => {
        if (res.items.content) {
          this.ganttData = res.items.content
        } else {
          this.ganttData = []
        }
        this.refreshGantt()
      }).catch(() => {
        this.ganttData = []
        this.refreshGantt()
      })
    },
arvin's avatar
arvin committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    export() {
      var dd = this.gantt.getTableData()
      const tableData = dd.data.data.filter(p => !p.priority).map((p) => {
        return {
          index: p.index,
          text: p.text,
          duration: Math.round(p.duration),
          predecessors: Object.values(p)
            .filter((item) => item.toString().includes('<span>'))[0]
            .replace('<span>', '')
            .replace('</span>', '')
        }
      })
      const header = ['节点', '站位号', '工期(天)', '前置']
      CsvExportor.downloadCsv(tableData, { header }, '站位路线.csv')
arvin's avatar
arvin committed
327 328
    },
    save() {
arvin's avatar
arvin committed
329 330 331 332
      if (this.loading) {
        return
      }
      this.loading = true
arvin's avatar
arvin committed
333 334
      const tasks = this.gantt.getTableData().data.data
      const links = this.gantt.getTableData().data.links
arvin's avatar
arvin committed
335 336 337
      links.forEach(l => {
        l.isCritical = this.gantt.isCriticalLink(l)
      })
arvin's avatar
arvin committed
338
      const params = this.ganttData.map(item => {
arvin's avatar
arvin committed
339
        const routes = item.extProcessExecutorRoutes ? item.extProcessExecutorRoutes.map(r => {
arvin's avatar
arvin committed
340 341 342 343
          return {
            id: r.id,
            operator: 'REMOVE'
          }
arvin's avatar
arvin committed
344
        }) : []
arvin's avatar
arvin committed
345
        const task = tasks.find(r => r.id === item.id)
arvin's avatar
arvin committed
346 347 348 349 350 351 352 353 354 355 356
        const link = links.find(r => Number(r.target) === item.id)
        if (link) {
          routes.push({
            operator: 'ADD',
            subTypeName: 'ExtProcessExecutorRoute',
            prevNodeIdType: 'ExtPosition',
            currNodeIdType: 'ExtPosition',
            currNodeId: Number(link.target),
            prevNodeId: Number(link.source),
            isCritical: link.isCritical
          })
arvin's avatar
arvin committed
357
        }
arvin's avatar
arvin committed
358

arvin's avatar
arvin committed
359 360 361 362 363 364 365 366 367
        return {
          id: item.id,
          operator: 'MODIFY',
          subTypeName: 'ExtPosition',
          workHour: task && (task.duration * 8) || 0,
          extProcessExecutorRoutes: routes
        }
      })
      this.$api.recursion('ExtPosition', params, true).then(() => {
arvin's avatar
arvin committed
368
        this.$utils.showMessageSuccess('保存成功!')
arvin's avatar
arvin committed
369
        this.getData()
arvin's avatar
arvin committed
370
      }).finally(() => { this.loading = false })
arvin's avatar
arvin committed
371
    }
arvin's avatar
arvin committed
372 373 374 375
  }
}
</script>

arvin's avatar
arvin committed
376 377 378 379 380 381
<style lang="scss">
.TfMomWebStep-model{
    height: 100%;
    .gatt{
        height: 100%;
    }
arvin's avatar
arvin committed
382 383
    .gantt_critical_task{
      background: #f39c4f;
arvin's avatar
arvin committed
384
      border: 1px solid #f39c4f;
arvin's avatar
arvin committed
385 386 387 388
    }
    .gantt_task_line{
      &.workday_over{
        background: #f39c4f;
arvin's avatar
arvin committed
389
        border: 1px solid #f39c4f;
arvin's avatar
arvin committed
390 391
      }
    }
arvin's avatar
arvin committed
392
}
arvin's avatar
arvin committed
393
</style>