Newer
Older
baseResourceFront / src / views / strategy / components / editStrategy.vue
yangqianqian on 6 Apr 2021 17 KB 按照新UI进行修改
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="strategyInfo.strategyInfo" size="small" label-well-code="right" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="策略名称" prop="strategyName">
            <el-input v-model.trim="strategyInfo.strategyInfo.strategyName" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" type="text"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="priority">
            <el-select v-model.trim="strategyInfo.strategyInfo.priority" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'必填'" clearable>
              <el-option v-for="item in prioritylist" :key="item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <el-select v-model.trim="strategyInfo.strategyInfo.deviceType" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'绑定分组设备类型'" clearable @change="changelist">
              <el-option v-for="item in lampDeviceType" :key="item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="绑定分组设备">
            <el-select v-model.trim="strategyInfo.strategyInfo.groupIds" :disabled="dialogStatus=='detail'" :placeholder="dialogStatus=='detail'?'':'绑定分组设备'" clearable multiple>
              <el-option v-for="item in deviceList" :key="item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="生效日期" prop="startDate">
            <el-date-picker
              v-model="timeRange"
              :picker-options="pickerOptions"
              :clearable="false"
              :disabled="dialogStatus=='detail'"
              type="daterange"
              value-format="yyyy-MM-dd"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-tabs v-model="tab" type="border-card" >
      <el-tab-pane label="开灯命令" name="open">
        <el-form ref="openForm" :rules="rules1" :model="openForm" size="small" label-well-code="right" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="策略类型" prop="strategyType">
                <el-select v-model="openForm.strategyType" :disabled="dialogStatus=='detail'" filterable placeholder="策略类型" clearable value="">
                  <el-option
                    v-for="item in strategyTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                    style="width: 250px"/>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="openForm.strategyType === '2' || openForm.strategyType === '3'" :gutter="20">
            <el-col :span="12">
              <el-form-item label="调整时间类型">
                <el-select v-model="openForm.flexType" :disabled="dialogStatus=='detail'" filterable placeholder="调整时间类型" clearable value="">
                  <el-option key="0" label="准时" value="0"/>
                  <el-option key="1" label="提前" value="1"/>
                  <el-option key="2" label="延时" value="2"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="openForm.flexType === '1'|| openForm.flexType === '2'" :span="12">
              <el-form-item label="提前/延时">
                <el-input v-model.trim="openForm.batchCount" :disabled="dialogStatus=='detail'" type="text" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="openForm.strategyType === '1'" :gutter="20" >
            <el-col :span="12">
              <el-form-item label="固定时间" prop="fixTime">
                <el-time-picker
                  v-model="openForm.fixTime"
                  :disabled="dialogStatus=='detail'"
                  value-format="HH:mm:ss"
                  clearable
                  placeholder="固定时间"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="关灯命令" name="close">
        <el-form ref="closeForm" :rules="rules2" :model="closeForm" size="small" label-well-code="right" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="策略类型" prop="strategyType">
                <el-select v-model="closeForm.strategyType" :disabled="dialogStatus=='detail'" filterable placeholder="策略类型" clearable value="">
                  <el-option
                    v-for="item in strategyTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                    style="width: 250px"/>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="closeForm.strategyType === '2' || closeForm.strategyType === '3'" :gutter="20">
            <el-col :span="12">
              <el-form-item label="调整时间类型">
                <el-select v-model="closeForm.flexType" :disabled="dialogStatus=='detail'" filterable placeholder="调整时间类型" clearable value="">
                  <el-option key="0" label="准时" value="0"/>
                  <el-option key="1" label="提前" value="1"/>
                  <el-option key="2" label="延时" value="2"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="closeForm.flexType === '1'|| closeForm.flexType === '2'" :span="12">
              <el-form-item label="提前/延时">
                <el-input v-model.trim="closeForm.batchCount" :disabled="dialogStatus=='detail'" type="text" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="closeForm.strategyType === '1'" :gutter="20" >
            <el-col :span="12">
              <el-form-item label="固定时间" prop="fixTime">
                <el-time-picker
                  v-model="closeForm.fixTime"
                  :disabled="dialogStatus=='detail'"
                  value-format="HH:mm:ss"
                  clearable
                  placeholder="固定时间"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <div v-show="dialogStatus!=='detail'" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { batchUpdate, batchAdd, batchDetail } from '@/api/system/strategy'
import { getLampDeviceType, getPriority, getLampStrategyType } from '@/api/system/allDict'
import { getGroupList } from '@/api/system/report'
// import { getAllControllerList, getAllLamppostList, getAllLampList, getAllLampboxList, getAllCircuitList } from '@/api/device'

export default {
  name: 'EditStrategy',
  data() {
    return {
      tab: 'open',
      rules: {
        deviceType: [{ required: true, message: '设备类型必选', trigger: ['blur', 'change'] }],
        strategyName: [{ required: true, message: '策略名称不能为空', trigger: ['blur', 'change'] }],
        priority: [{ required: true, message: '优先级不能为空', trigger: ['blur', 'change'] }],
        startDate: [{ required: true, message: '开始日期必选', trigger: ['blur', 'change'] }],
        endDate: [{ required: true, message: '结束日期必选', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      rules1: {
        strategyType: [{ required: true, message: '策略类型不能为空', trigger: ['blur'] }],
        fixTime: [{ required: true, message: '固定时间不能为空', trigger: ['blur', 'change'] }]
      },
      rules2: {
        strategyType: [{ required: true, message: '策略类型不能为空', trigger: ['blur'] }],
        fixTime: [{ required: true, message: '固定时间不能为空', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      timeRange: [],
      strategyTypeList: [],
      lampDeviceType: [],
      // groupList: [],
      deviceList: [],
      prioritylist: [],
      openForm: {
        strategyId: '',
        strategyType: '',
        commandType: '1',
        fixTime: '',
        flexType: '',
        flexTime: ''
      },
      closeForm: {
        strategyId: '',
        strategyType: '',
        commandType: '0',
        fixTime: '',
        flexType: '',
        flexTime: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update,detail
      textMap: {
        update: '编辑策略',
        create: '新增策略',
        detail: '策略详情'
      }, // 表头显示标题
      strategyInfo: {
        strategyInfo: {
          id: '', // id
          strategyName: '',
          deviceType: '',
          groupIds: '',
          startDate: '',
          endDate: '',
          priority: ''
        },
        commandList: []
        // commandList: '',
        // strategyType: '',
        // commandType: '',
        // fixTime: '',
        // flexType: '',
        // flexTime: ''
      } // 表单
    }
  },
  watch: {
    addlist() {
      var listitem = { 'batchNumber': this.startnumber.toString(), 'batchCount': '', 'remaincount': '', 'contractId': '' }
      this.strategyInfo.commandList.push(listitem)
    },
    deletelist(e) {
      this.strategyInfo.commandList.batches.splice(e, 1)
    },
    timeRange(val) {
      if (val && val.length > 0) {
        this.strategyInfo.strategyInfo.startDate = val[0]
        this.strategyInfo.strategyInfo.endDate = val[1]
      } else {
        this.strategyInfo.strategyInfo.startDate = ''
        this.strategyInfo.strategyInfo.endDate = ''
      }
    }
  },
  created() {
    // getGroupList().then(response => {
    //   this.deviceList = response.data
    //   console.log(response)
    // })
    getLampDeviceType().then(response => {
      this.lampDeviceType = response.data
    })
    getPriority().then(response => {
      this.prioritylist = response.data
    })
    getLampStrategyType().then(response => {
      this.strategyTypeList = response.data
    })
  },
  methods: {
    changelist() {
      this.deviceList = []
      this.strategyInfo.strategyInfo.groupIds = []
      getGroupList(this.strategyInfo.strategyInfo.deviceType).then(response => {
        console.log(response,'111111111111111111')
        for (var i = 0; i < response.data.length; i++) {
          var item = { name: response.data[i].groupName, value: response.data[i].id }
          this.deviceList.push(item)
        }
      })
    },
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
          this.$refs['openForm'].clearValidate()
          this.$refs['closeForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'detail') { // 如果是修改,将row中数据填写到输入框中
        batchDetail(row.id).then(response => {
          if (response.code === 200) {
            this.strategyInfo = response.data
            console.log(response.data)
            this.timeRange = [response.data.strategyInfo.startDate, response.data.strategyInfo.endDate]
            for (var i = 0; i < this.strategyInfo.commandList.length; i++) {
              if (this.strategyInfo.commandList[i].commandType === '1') {
                this.openForm = this.strategyInfo.commandList[i]
              } else if (this.strategyInfo.commandList[i].commandType === '0') {
                this.closeForm = this.strategyInfo.commandList[i]
              }
            }
            this.deviceList = []
            getGroupList(this.strategyInfo.strategyInfo.deviceType).then(response => {
              console.log(response,'111111111111111111')
              for (var i = 0; i < response.data.length; i++) {
                var item = { name: response.data[i].groupName, value: response.data[i].id }
                this.deviceList.push(item)
              }
            })
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
              this.$refs['openForm'].clearValidate()
              this.$refs['closeForm'].clearValidate()
            })
          }
        }).catch(() => {
        })
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    submitList() {
      var result = true
      console.log(this.closeForm)
      this.strategyInfo.commandList = []
      if (this.openForm.strategyType !== '' || this.openForm.fixTime !== '' || this.openForm.flexType !== '' || this.openForm.flexTime !== '') {
        this.$refs['openForm'].validate((valid) => {
          if (valid) {
            this.strategyInfo.commandList.push(this.openForm)
          } else {
            result = false
          }
        })
      }
      if (this.closeForm.strategyType !== '' || this.closeForm.fixTime !== '' || this.closeForm.flexType !== '' || this.closeForm.flexTime !== '') {
        this.$refs['closeForm'].validate((valid) => {
          if (valid) {
            this.strategyInfo.commandList.push(this.closeForm)
          } else {
            result = false
          }
        })
      }
      return result
    },
    // 修改数据
    updateData: function() {
      if (this.submitList()) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log(JSON.stringify(this.strategyInfo), '@@@@@@@@@@@@@@@@@@@@')
            batchUpdate(this.strategyInfo).then(response => {
              if (response.code === 200) {
                this.$message.success('修改成功')
                this.$emit('watchChild')
                this.dialogFormVisible = false
              }
            })
          }
        })
      }
    },
    // 新增数据
    createData: function() {
      if (this.submitList()) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log(JSON.stringify(this.strategyInfo), '@@@@@@@@@@@@@@@@@@@@')
            batchAdd(this.strategyInfo).then(response => {
              if (response.code === 200) {
                this.$confirm('新增成功,是否继续新增?', '提示', {
                  confirmButtonText: '是',
                  cancelButtonText: '否',
                  type: 'info'
                }).then(() => {
                  this.resetForm()
                  this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                  })
                }).catch(() => {
                  this.$emit('watchChild')
                  this.dialogFormVisible = false
                })
              }
            })
          }
        })
      }
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    // 清除数据
    resetForm() {
      this.timeRange = []
      this.openForm = {
        strategyId: '',
        strategyType: '',
        commandType: '1',
        fixTime: '',
        flexType: '',
        flexTime: ''
      }
      this.closeForm = {
        strategyId: '',
        strategyType: '',
        commandType: '0',
        fixTime: '',
        flexType: '',
        flexTime: ''
      }
      this.strategyInfo = {
        strategyInfo: {
          id: '', // id
          strategyName: '',
          deviceType: '',
          groupIds: '',
          startDate: '',
          endDate: '',
          priority: ''
        },
        commandList: []
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .tableBox{
    div{
      height: 18px !important;
      line-height: 18px !important;
    }
  }
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .dialog-footer {
    margin-top: -20px;
    text-align: center;
  }
</style>