zjf
2023-03-08 92b359a6458fb985b78431e0a2f54b548e01b1ca
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
<!-- 菜单树 -->
<template>
  <div class="apartment-management">
  <p class="title"> 部门管理 </p>
 
  <el-card>
      <el-row>
        <el-button class="create-button"
        type="primary" @click='createModule'>新增</el-button>
        <el-button class="delete-button"
        type="primary" @click='deleteModule' v-if='isDelete'>删除</el-button>
      </el-row>
      <el-table
        ref="menusTable"
        border
        max-height="600px"
        :row-style="showRow"
        :data="menusTable"
        @selection-change="handleSelectionChange"
        v-bind="$attrs">
        <el-table-column
          show-overflow-tooltip
          type="selection"
          align="center"
          width="55"/>
        <el-table-column
          prop="Name"
          label="部门名称" width="400">
          <template slot-scope="scope">
            <span class='level'>
              <div v-show="scope.row.Children&&scope.row.Children.length>0" @click="openToggle(scope.row)"
                :class="[scope.row.open?'el-table__expand-icon el-table__expand-icon--expanded':'el-table__expand-icon']">
                <i class='el-icon-arrow-right'></i>
              </div>
            </span>
            {{scope.row.Name}}
          </template>
        </el-table-column>
        <el-table-column
          prop="Comment"
          label="备注"  width="200">
          <template slot-scope="scope">
            <div>{{scope.row.Comment}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="IsEnabled"
          label="状态">
          <template slot-scope="scope">
            <div>{{scope.row.IsEnabled ?'启用':'禁用'}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="Sort"
          label="排序">
          <template slot-scope="scope">
            <div>{{scope.row.Sort}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="IsSectorArea"
          label="是否是区域部门">
          <template slot-scope="scope">
            <div>{{scope.row.IsSectorArea ? '是' : '否'}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="operation"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click='addApart(scope.row)'>添加子部门</el-button>
            <!-- 判断下面是否有子菜单,有子菜单不能是有删除按钮 -->
            <el-button v-if="!scope.row.children" type="text" size="small"  @click='deleteApart(scope.row.Id)'>删除</el-button>
            <el-button type="text" size="small"  @click='editApart(scope.row)'>编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
     </el-card>
    <!-- 导航新增编辑弹出框 -->
    <el-dialog
      :visible.sync="depCreateDialog"
      :title="navBtnTitle"
      :before-close="cancelDialog"
      width="40%">
      <el-form
        ref="apartForm"
        :model="apartForm"
        :rules="apartRules"
        label-width="120px"
        class="login-form"
        auto-complete="on"
        label-position="left">
        <el-form-item label="上级部门" prop='fatherIntro'>
            <el-cascader
              ref="refApart"
              placeholder="请选择上级部门"
              :options="isCreate? selectOpts: rightsList"
              :show-all-levels="false"
              :props="{
                children: 'Children',
                label: 'Name',
                value: 'Id'
              }"
              v-model="apartForm.fatherIntro"
              style="width: 100%;"
              @change='getFatherOpt'
              change-on-select/>
        </el-form-item>
        <el-form-item label="部门名称"  prop='introName'>
          <el-input
            v-model="apartForm.introName"
            placeholder="请输入部门名称"/>
          </el-form-item>
        <el-form-item label="部门状态"  prop='apartStatus'>
          <el-switch v-model="apartForm.apartStatus"></el-switch>
        </el-form-item>
          <el-form-item label="是否是区域部门">
            <el-switch v-model="apartForm.apartChosen"></el-switch>
          </el-form-item>
          <el-form-item label="部门类型" prop='apartInt'>
           <el-select v-model="apartForm.apartInt" placeholder="请选择" style="width: 100%;">
              <el-option
                v-for="item in apartLists"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序数字">
            <el-input
              class='numInp'
              type='number'
              v-model="apartForm.sortNum"
              placeholder="请输入排序数字"/>
          </el-form-item>
          <el-form-item label="备注说明">
            <el-input
                type='textarea'
                v-model="apartForm.info"
                placeholder="请输入备注说明"/>
          </el-form-item>
          <el-form-item>
            <el-button @click="cancelDialog">取 消</el-button>
            <el-button
              type="primary"
              @click="submitDialog('apartForm')">确 定</el-button>
          </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
 
<script>
import Vue from 'vue'
import { GetDepartmentList, AddDepartment, DeleteDepartment, UpdateDepartment } from '@/api/systemManagement/departmentManage'
export default {
  name: 'staffManage',
  data () {
    return {
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      // 菜单表格结构数据
      menusTable: [],
      depCreateDialog: false,
      apartForm: {
        fatherIntro: '', // 上级部门
        introName: '', // 部门名称
        apartStatus: true, // 部门状态
        apartChosen: false, // 是否是区域部门
        apartInt: '', // 是否是区域部门
        sortNum: 1, // 排序数字
        info: ''
      },
      apartRules: {
        fatherIntro: [
          { required: true, trigger: 'blur', message: '请选择上级部门' }
        ],
        introName: [{ required: true, trigger: 'blur', message: '请输入部门名称' }],
        apartInt: [{ required: true, trigger: 'blur', message: '请选择部门类型' }]
      },
      navBtnTitle: '新建',
      deleteIds: [],
      isDelete: false,
      parentId: '',
      isCreate: false,
      isSon: false,
      apartLists: [{id: 1, value: '总经办'}, {id: 2, value: '人事部'}, {id: 3, value: '财务部'}, {id: 4, value: '商务部'}, {id: 5, value: '销售部'}, {id: 6, value: '技术部'}],
      rightsList: [],
      selectOpts: []
    }
  },
  // 初始化函数,赋值,menusTree =>menusTable
  created () {
    this.getList()
  },
  methods: {
    getList () {
      GetDepartmentList().then(res => {
        console.log(res)
        if (res.data.ErrorCode === 200) {
          this.menusTable = res.data.Result
          this.rightsList = res.data.Result
          this.selectOpts = JSON.parse(JSON.stringify(this.rightsList))
          this.selectOpts.unshift({ Name: '无上级部门', Id: 0 })
        }
      })
    },
    createModule () {
      this.navBtnTitle = '新增部门'
      this.depCreateDialog = true
      this.isCreate = true
      this.isSon = false
    },
    addApart (row) {
      this.isCreate = true
      this.isSon = true
      this.navBtnTitle = '添加子部门'
      this.depCreateDialog = true
      this.apartForm = {
        fatherIntro: row.Id, // 上级部门
        introName: '', // 部门名称
        apartStatus: true, // 部门状态
        apartChosen: false, // 是否是区域部门
        apartInt: '', // 是否是区域部门
        sortNum: Number(row.Sort) + 1, // 排序数字
        info: ''
      }
    },
    editApart (row) {
      console.log(row)
      this.isCreate = false
      this.isSon = false
      this.navBtnTitle = '编辑部门'
      this.depCreateDialog = true
      this.isCreate = false
      this.isSon = false
      this.apartForm = {
        fatherIntro: row.Id, // 上级部门
        introName: row.Name, // 部门名称
        apartStatus: row.IsEnabled, // 部门状态
        apartChosen: row.IsSectorArea, // 是否是区域部门
        apartInt: row.DepartmentType, // 是否是区域部门
        sortNum: row.Sort, // 排序数字
        info: row.Comment
      }
      this.parentId = row.ParentModuleId
    },
    getFatherOpt () {
      console.log(this.apartForm.fatherIntro)
      const flag = Array.isArray(this.apartForm.fatherIntro)
      if (flag) {
        this.parentId = this.apartForm.fatherIntro.length > 1 ? this.apartForm.fatherIntro[0] : ''
      }
    },
    handleSelectionChange (val) {
      console.log(val)
      if (val.length > 0) {
        let deleteId = []
        val.forEach(item => {
          deleteId.push(item.Id)
        })
        this.isDelete = true
        this.deleteIds = deleteId
      } else {
        this.isDelete = false
      }
    },
    deleteApart (id) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          DeleteDepartment({'Id': id}).then(res => {
            console.log(res)
            if (res.data.ErrorCode === 200) {
              this.$message.success(res.data.Message)
              this.getList()
            } else {
              this.$message.error(res.data.Message)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    submitDialog (formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return
        const isIntro = Array.isArray(this.apartForm.fatherIntro)
        let params = {
          'Name': this.apartForm.introName,
          'ParentId': isIntro ? (this.apartForm.fatherIntro.length > 1 ? this.apartForm.fatherIntro[this.apartForm.fatherIntro.length - 1] : (this.apartForm.fatherIntro[0] === 0 ? '' : this.apartForm.fatherIntro[0])) : this.apartForm.fatherIntro,
          'DepartmentType': this.apartForm.apartInt,
          'IsEnabled': this.apartForm.apartStatus,
          'Sort': this.apartForm.sortNum,
          'Comment': this.apartForm.info,
          // 'LeaderUserId': '',
          'IsSectorArea': this.apartForm.apartChosen
        }
        if (this.isCreate) {
          if (this.isSon) {
            params.ParentModuleId = this.parentId
          }
          AddDepartment(params).then(res => {
            console.log(res)
            if (res.data.ErrorCode === 200) {
              this.$message.success(res.data.Message)
              this.getList()
              this.depCreateDialog = false
              this.$refs.apartForm.resetFields()
              this.apartForm = {
                fatherIntro: '', // 上级部门
                introName: '', // 部门名称
                apartStatus: true, // 部门状态
                apartChosen: false, // 是否是区域部门
                apartInt: '', // 是否是区域部门
                sortNum: 1, // 排序数字
                info: ''
              }
            } else {
              this.$message.error(res.data.Message)
            }
          })
        } else {
          params.Id = isIntro ? (this.apartForm.fatherIntro.length > 1 ? this.apartForm.fatherIntro[this.apartForm.fatherIntro.length - 1] : this.apartForm.fatherIntro[0]) : this.apartForm.fatherIntro
 
          params.ParentId = this.parentId
 
          UpdateDepartment(params).then(res => {
            console.log(res)
            if (res.data.ErrorCode === 200) {
              this.$message.success(res.data.Message)
              this.getList()
              this.depCreateDialog = false
              this.$refs.apartForm.resetFields()
              this.apartForm = {
                fatherIntro: '', // 上级部门
                introName: '', // 部门名称
                apartStatus: true, // 部门状态
                apartChosen: false, // 是否是区域部门
                apartInt: '', // 是否是区域部门
                sortNum: 1, // 排序数字
                info: ''
              }
            } else {
              this.$message.error(res.data.Message)
            }
          })
        }
      })
    },
    // 关闭新增或编辑
    cancelDialog () {
      this.$refs.apartForm.resetFields()
      this.apartForm = {
        fatherIntro: '', // 上级部门
        introName: '', // 部门名称
        apartStatus: true, // 部门状态
        apartChosen: false, // 是否是区域部门
        apartInt: '', // 是否是区域部门
        sortNum: 1, // 排序数字
        info: ''
      }
      this.depCreateDialog = false
    },
    showRow (row) {
      const show = row.row.parent
        ? row.row.parent._expanded && row.row.parent._show
        : true
      row.row._show = show
      return show
        ? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;'
        : 'display:none;'
    },
 
    // 树节点开关操作
    openToggle (item) {
      console.log(item)
      // 这里只是展开和关闭样式的变换方法
      Vue.set(item, 'open', !item.open)
      // 展开的时候,显示子节点,关闭的时候隐藏子节点
      // 遍历所有的子节点,加入到menuTable中
      for (let j = 0; j < this.rightsList.length; j++) {
        // 找到父节点的id,然后依次把子节点放到数组里面父节点后面
        if (this.rightsList[j].Id !== item.Id) {
          continue
        }
        if (item.open) { // open => close
          console.log(item.Children)
          let rightsList = this.rightsList
          item.Children.forEach(function (child, index) {
            rightsList.splice(j + index + 1, 0, child) // 添加子节点
          })
        } else {
          this.rightsList.splice(j + 1, item.Children.length) // 删除子节点
        }
        break
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
  .apartment-management{
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }
  .nav-dialog-div {
    margin-bottom: 20px;
  }
  .nav-dialog-div /deep/ .el-input {
    width: auto;
  }
  .title {
    font-size: 18px;
    height: 40px;
    line-height: 40px;
    margin: 10px 0;
    color: #333;
    padding: 0 20px;
  }
  .level {
    display: inline-block;
    width: 20px;
  }
  .level1, .level2, .level3 {
    display: inline-block;
    width: 20px;
  }
 
  .level1 {
    margin-left: 5px;
  }
 
  .level2 {
    margin-left: 20px;
  }
 
  .level3 {
    margin-left: 35px;
  }
  .create-button{
    margin-bottom: 20px;
    float: right;
  }
  .delete-button{
    margin-bottom: 20px;
    float: right;
    margin-right: 20px;
  }
  .numInp{
    width:40%;
  }
</style>