quanyawei
2023-10-07 0c70804c83d63f5dd1c29550aa6eac60bd8dac1a
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
<template>
  <a-form style="margin-top: 8px">
    <a-modal
      title="编辑-因子信息"
      destroyOnClose
      :visible="visible"
      @cancel="handleCancel"
      @ok="handleOk"
      okText="保存"
    >
      <a-form style="margin-top: 8px" :form="form">
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="名称">
          <a-input
            v-decorator="['sensorName', {initialValue:iSensorName,rules: [{ required: true,message:'请输入名称'}]}]"
            placeholder="请输入名称"
          />
        </a-form-item>
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="code">
          <a-input
            v-decorator="['sensorCode', {initialValue:iSensorCode,rules: [{ required: true,message:'请输入code'}]}]"
            placeholder="请输入code"
          />
        </a-form-item>
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="下限值">
          <a-input
            v-decorator="['lower', {initialValue:lower,rules: [{ required: false,message:'请输入下限值'}]}]"
            placeholder="请输入下限值"
          />
        </a-form-item>
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="上限值">
          <a-input
            v-decorator="['upper', {initialValue:upper,rules: [{ required: false,message:'请输入上限值'}]}]"
            placeholder="请输入上限值"
          />
        </a-form-item>
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="默认单位">
          <a-select
                placeholder="请选择单位"
                allow-clear
                v-decorator="['sensorUnit', {initialValue:defaultUnitKey, rules: [{ required: true, message:'请选择单位' }] }]"
              >
                <a-select-option v-for="(item,key) in this.selectTypes" :value="key" :key="key" >{{item}}</a-select-option>
              </a-select>
        </a-form-item>
 
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="描述">
          <a-input v-decorator="['sensorDesc',{initialValue:iSensorDesc} ]" placeholder="请输入描述" />
        </a-form-item>
      </a-form>
    </a-modal>
  </a-form>
</template>
 
<script lang="ts">
import {
  Component,
  Prop,
  Vue,
  Emit,
  Model,
  Watch
} from "vue-property-decorator";
import { State, Mutation, namespace } from "vuex-class";
 
import { get, post } from "@/util/request";
 
@Component({
  components: {}
})
export default class UpdateSensorBasic extends Vue {
  private iSensorName: any = null;
  private iSensorCode: any = null;
  private iSensorDesc: any = null;
  private id: any = null;
  private form: any = {};
  private lower:any=null;
  private upper:any=null;
  // private unit:any=null;
  private selectTypes:any[]=[];
  private defaultUnitKey:any=null;
 
  @Prop({
    type: Boolean,
    default: false
  })
  private visible!: boolean;
 
  @Prop({
    type: Object,
    default() {
      return {};
    }
  })
  private record!: any;
 
  private handleCancel(): void {
    this.updateVisible(false);
  }
    //默认单位下拉框筛选,选择
   private filterOption(input, option) {
    return (
      option.componentOptions.children[0].text
        .toLowerCase()
        .indexOf(input.toLowerCase()) >= 0
    );
  }
 
 //初始化
  private created() {
    this.form = this.$form.createForm(this, { name: "UpdateSensorBasic" });
    //请求默认单位接口
     get("dict/data/query?type=unit", {}).then(res => {
        this.selectTypes = res.data.data;
      })
      .catch(err => {
        console.log(err);
      });
  }
  // 保存 更新数据库
  private handleOk(): void {
    this.form.validateFields((err: any, values: any) => {
      //遇到问题,如果未修改,直接点保存,values.unit会直接拿到值 mg/m3 而不是1 ,保存是传key,而不是value
      if (err) {
        return;
      }
      let name = values.sensorName == this.record.name ? null : values.sensorName
      let code = values.sensorCode == this.record.code ? null : values.sensorCode
      let lower = ''
      if (this.record.lower) {
        lower = values.lower == this.record.lower ? null : values.lower
      }else{
        lower = values.lower
      }
      let upper = ''
      if (this.record.upper) {
        upper = values.upper == this.record.upper ? null : values.upper
      }else{
        upper = values.upper
      }
      let default_unit_key = values.sensorUnit == this.record.defaultUnitKey ? null : values.sensorUnit
      let desc = ''
      if (this.record.desc) {
        desc = values.sensorDesc == this.record.desc ? null : values.sensorDesc
      }else{
        desc = values.sensorDesc
      }
 
      if ((name === null && code === null && lower === null && upper === null && default_unit_key === null && desc === null)) {
        this.$message.warning('未改变!')
        return;
      }
 
      post("sensor/updateSensor", {
            id: this.id,
            name: name,
            code: code,
            lower: lower,
            upper: upper,
            default_unit_key: default_unit_key,
            desc: desc
          }
        )
        .then(res => {
          if (res.data.code != 0) {
            this.$message.error(res.data.message);
          } else {
            //保存成功,组件刷新数据
            this.$message.success(res.data.message)
            this.$emit("updateData");
            this.updateVisible(false);
          }
        })
        .catch(err => {
          console.log(err);
        });
    });
  }
 
  private updateVisible(visible: boolean): void {
    this.$emit("update:visible", visible);
  }
 
  //监听编辑回调值
  @Watch("record", {
    immediate: true, //监听开始后是否立即调用该回调函数;
    deep: true //深度监听
  })
  //监听编辑逻辑赋值
  private recordChange(value: any, oldValue: any): void {
    this.iSensorName = this.record.name;
    this.iSensorCode = this.record.code;
    if(this.record.lower)
       this.lower=this.record.lower;
       else
       this.lower=null;
    if(this.record.upper)
      this.upper=this.record.upper;
      else
       this.upper=null;
 
    if(this.record.default_unit_key){
      this.defaultUnitKey=this.record.defaultUnitKey;
    }
    else{
      this.defaultUnitKey=undefined;
    }
 
 
 
 
    if(this.record.desc){
         this.iSensorDesc = this.record.desc;
    }else{
        this.iSensorDesc=null;
    }
 
    this.id = this.record.id;
 
  }
}
</script>