quanyawei
2024-08-28 247dacca9784396eeaf17ef3e85c7ae646b786e4
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
<template>
  <div>
    <a-card :bordered="false" style="margin-top:24px">
      <div class="tableList">
        <div class="tableListForm">
          <a-form layout="horizontal">
            <a-row :gutter="{ md: 8, lg: 24, xl: 48 }">
              <a-button
                icon="plus"
                type="primary"
                @click="() => this.handleModalVisible(true)"
                style="margin-left: 25px;margin-bottom: 30px"
              >新建</a-button>
 
 
            </a-row>
          </a-form>
        </div>
 
        <av-standard-table
          :dataSource="dataSource"
          :columns="columns"
          :loading="tableLoading"
          :paginationProps="pagination"
          @tableChange="handlerTableChange"
        ></av-standard-table>
      </div>
    </a-card>
 
    <a-modal
      title="添加-单位转换"
      destroyOnClose
      :visible="visibleCreateModal"
      @ok="handleCreateModalOk"
      @cancel="handleCreateModalCancel"
      okText="保存"
    >
      <a-form style="margin-top: 8px" :form="form">
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="初始单位">
          <a-select
            placeholder="请选择初始单位"
            v-decorator="['initialUnit', { rules: [{ required: true,message:'请选择初始单位!' }] }]"
          >
            <a-select-option v-for="(item,index) in unitHttp" :key="index">{{item}}</a-select-option>
          </a-select>
        </a-form-item>
 
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="计算规则">
          <span>
            <a-select
              v-decorator="['initialMath', {initialValue:'*', rules: [{ required: true }] }]"
              style="width:60px"
            >
              <a-select-option value="+">加</a-select-option>
              <a-select-option value="-">减</a-select-option>
              <a-select-option value="*">乘</a-select-option>
              <a-select-option value="/">除</a-select-option>
            </a-select>
 
            <a-input
              v-decorator="['roleValue', {initialValue:1000 ,rules: [{ required: true}] },]"
              placeholder="请输入数字"
              style="width:100px;margin-left:10px;margin-right:10px"
            ></a-input>
            <a-button @click="AddInputClick">+</a-button>
          </span>
          <span>
            <a-input
              v-decorator="['createFormula', { rules: [{ required: true }] }]"
              placeholder="通过上面选择框添加规则"
              style="width:200px;margin-right:10px"
              disabled
            ></a-input>
            <a-button @click="CancelInputClick">×</a-button>
          </span>
        </a-form-item>
 
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="转换单位">
          <a-select
            placeholder="请选择转换单位"
            v-decorator="['changeUnit', { rules: [{ required: true,message:'请选择转换单位!'}] }]"
          >
            <a-select-option v-for="(item,index) in unitHttp" :key="index">{{item}}</a-select-option>
          </a-select>
        </a-form-item>
 
          <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="专用因子">
          <a-select
            placeholder="请选择专用因子"
            v-decorator="['sensorKey', { rules: [{ required: false,message:'请选择转换单位!'}] }]"
           allow-clear
          >
            <a-select-option v-for="(item,key,index) in specificSensorKey" :key="key">{{item}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
    <!-- @ok="handleUpdateOk" -->
    <update-task-form
      :visible.sync="updateTaskFormVisible"
      :record="updateRecord"
      @updateData="loadRuleData(1)"
    ></update-task-form>
  </div>
</template>
 
<script lang="tsx">
import { Component, Prop, Vue, Model, Watch } from "vue-property-decorator";
 
import { get, post } from "@/util/request";
 
import UpdateTaskForm from "./components/UpdateUnitRule.vue";
 
@Component({
  components: {
    UpdateTaskForm
  }
})
export default class sensorChangeUnit extends Vue {
  private pagination: any = {
    total: 0,
    current: 1,
    pageSize: 5,
    showSizeChanger: false,
    showQuickJumper: false
  };
 
  private tableLoading: boolean = false;
 
  private dataSource: any[] = [];
 
  private defaultKey: any = null;
 
  private visibleCreateModal: boolean = false;
 
  private updateTaskFormVisible: boolean = false;
 
  private updateRecord: any = {};
 
  private unitHttp: any = {};
 
  private form: any = {};
  private sensorAll:any=[];
  private specificSensorKey:any=[]
 
  private columns: any[] = [
    {
      title: "初始单位",
      dataIndex: "originalUnitName"
    },
    {
      title: "公式",
      dataIndex: "formula"
    },
    {
      title: "转换单位",
      dataIndex: "targetUnitName"
    },
    {
      title: "专用因子",
      dataIndex: "sensorName"
    },
    {
      title: "操作",
      customRender: this.opRender
    }
  ];
 
  // 存放字典表因子下拉数据
  private sensors: any = []
  //初始化,加载table
  private created() {
    this.form = this.$form.createForm(this, { name: "sensorChangeUnit" });
    //初始加载字典表中默认配置专用因子
     get("dict/data/query?type=specificSensorKey", {}).then(res => {
        this.specificSensorKey = res.data.data;
        this.loadRuleData(1);
      })
      .catch(err => {
        console.log(err);
      });
  }
 
  private selectAllSensor(dataSourceMiddle:any){
    if(this.sensorAll){
      get("sensor/getAllSensorWithoutPage", {})
      .then(res => {
        if (res.data.code != 0) {
          this.$message.error(res.data.message);
        } else {
          this.sensorAll=res.data.data.sensors
           this.computeSensorName(dataSourceMiddle);
        }
      })
      .catch(err => {
        console.log(err);
      });
    }
 
  }
  //添加公式进入input
  private AddInputClick() {
    this.form.validateFields((err: any, values: any) => {
      //把值存到input框
      this.form.setFieldsValue({
        createFormula: values.createFormula
          ? "(" +
            values.createFormula +
            ")" +
            values.initialMath +
            values.roleValue
          : "初值" + values.initialMath + values.roleValue
      });
    });
  }
 
  //清除input框公式
  private CancelInputClick() {
    this.form.validateFields((err: any, values: any) => {
      //把值存到input框
      this.form.setFieldsValue({
        createFormula: null
      });
    });
  }
  //新建 ,调取初始单位,转换单位
  private handleModalVisible(isVisible: boolean): void {
    this.defaultKey = 1000;
    this.visibleCreateModal = isVisible;
    get("dict/data/query?type=unit", {
        headers: { token: this.$ss.get("token") }
      })
      .then(res => {
        this.unitHttp = res.data.data;
      })
      .catch(err => {
        console.log(err);
      });
  }
 
  private handleUpdateModalVisible(visible: boolean, record: any): void {
    this.updateTaskFormVisible = true;
    this.updateRecord = record;
  }
 
  //新建保存
  private handleCreateModalOk() {
    this.form.validateFields((err: any, fieldsValue: any) => {
 
      if (err) {
        return;
      }
      //初始单位和转换单位相同
      if (fieldsValue.initialUnit == fieldsValue.changeUnit) {
        this.$message.error("单位相同");
        return;
      }
      //修改公式 用占位符替代
      let fm = fieldsValue.createFormula.replace("初值", "{0}");
      post("unitConversion/insert",
          {
            originalUnitKey: fieldsValue.initialUnit,
            targetUnitKey: fieldsValue.changeUnit,
            formula: fm,
            sensorCode:fieldsValue.sensorKey?fieldsValue.sensorKey:null
 
          }
        )
        .then((res: any) => {
          if (res.data.code !== 0) {
            this.$message.error(res.data.message, 2);
            return;
          }else {
            this.visibleCreateModal = false;
            this.$message.success(res.data.message, 2);
            this.loadRuleData(1);
          }
        })
        .catch(err => {
          console.log(err);
        });
    });
  }
 
  //切换页码触发
  private handlerTableChange(pagination: any, filter: any, sorter: any): void {
    this.loadRuleData(pagination.current);
  }
  private handleCreateModalCancel(): any {
    this.visibleCreateModal = false;
  }
 
  private loadRuleData(pageCurrent: any) {
    this.tableLoading = true;
    get("unitConversion/query", {
          page: pageCurrent,
          size: 5,
          orderType: 1,
          order: "updateTime"
      })
      .then(res => {
        this.pagination.total = res.data.data.total;
        this.pagination.current = res.data.data.current;
        this.pagination.pageSize = res.data.data.size;
        if (res.data.data.unitConversions) {
          for (let i = 0; i < res.data.data.unitConversions.length; i++) {
            //修改公式占位符
            res.data.data.unitConversions[
              i
            ].formula = res.data.data.unitConversions[i].formula.replace(
              "{0}",
              "初值"
            );
          }
          let dataSourceMiddle = res.data.data.unitConversions === undefined ? [] :  res.data.data.unitConversions;
         this.selectAllSensor(dataSourceMiddle);
          this.tableLoading = false;
        } else {
          this.dataSource = []
          this.tableLoading = false;
        }
      })
      .catch(err => {
        console.log(err);
      });
  }
 
  //把sensorCode转成sensorName
  private computeSensorName(dataSourceMiddle: any) {
    if(this.sensorAll){
     for(let i=0;i<dataSourceMiddle.length;i++){
       if(dataSourceMiddle[i].sensorCode){
         for(let j=0;j<this.sensorAll.length;j++){
           if(dataSourceMiddle[i].sensorCode==this.sensorAll[j].code){
             dataSourceMiddle[i].sensorName=this.sensorAll[j].name
           }
         }
       }
     }
      this.dataSource=dataSourceMiddle
    }
 
  }
 
  private deleteManageRole(record: any) {
    post("unitConversion/delete",
        {
          id: record.id
        }
      )
      .then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.message)
          if(res.data.data === null)
          this.loadRuleData(1);
        }else {
          this.$message.warning(res.data.message)
        }
      })
      .catch(function(error) {
        console.log("删除失败");
      });
  }
 
  private opRender(text: string, record: any, index: number) {
    return (
      <div>
        <a onClick={() => this.handleUpdateModalVisible(true, record)}>
          {" "}
          编辑{" "}
        </a>
        <a-divider type="vertical" />
        <a-popconfirm
          title="确认删除吗?"
          ok-text="确定"
          cancel-text="取消"
          onConfirm={() => this.deleteManageRole(record)}
        >
          <a href="#">删除</a>
        </a-popconfirm>
      </div>
    );
  }
}
</script>
 
<style lang="less">
.tableList {
  .tableListOperator {
    margin-bottom: 16px;
 
    button {
      margin-right: 8px;
    }
  }
}
</style>