| <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>  |