Ben Lin
2024-08-28 0a8a3f71f2e50f0603077197d9b1971431a64b36
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
<template>
  <PageWrapper class="high-form" title="规则管理" content="这是规则管理页面。" @back="goBack">
    <a-card title="规则信息" :bordered="false">
      <BasicForm @register="register" />
    </a-card>
    <a-card title="规则明细" :bordered="false" class="!mt-5">
      <div>
        <BasicTable :columns="dtlColumns" ref="tableRef" :dataSource="data" :canResize="canResize" :loading="loading"
          :striped="striped" :bordered="border" :actionColumn="actionColumn" :pagination="pagination" showTableSetting>
          <template #toolbar>
            <a-button type="primary" @click="handleCreate"> 新增规则 </a-button>
          </template>
          <template #action="{ record }">
            <TableAction :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
                name: ''
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除?',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
                name: ''
              },
            ]" />
          </template>
        </BasicTable>
      </div>
    </a-card>
    <a-card title="校验测试" :bordered="false" class="!mt-5">
      <BasicForm @register="registerTest" />
    </a-card>
 
    <template #rightFooter>
      <a-button class="mr-4" type="info" @click="cancel"> 取消 </a-button>
      <a-button type="primary" @click="submitAll"> 提交 </a-button>
    </template>
    <CheckruleDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
import { BasicForm, useForm } from '/@/components/Form';
import { defineComponent, onMounted, ref, unref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { PageWrapper } from '/@/components/Page';
import { Card } from 'ant-design-vue';
import { useGo } from '/@/hooks/web/usePage';
import { useDrawer } from '/@/components/Drawer';
import { BasicTable, TableAction, TableActionType } from '/@/components/Table';
import { columns, dtlColumns, newFormSchema, testSchemas } from './checkrule.data';
import CheckruleDrawer from './CheckruleDrawer.vue';
import { useTabs } from '/@/hooks/web/useTabs';
import {
  DeleteRuleDtl,
  getRuleById,
  getRuleDtl,
  SaveRule,
  SaveRuleDtl,
} from '/@/api/tigerapi/bas/checkrule';
import {
  CheckRuleDtlParams,
  CheckRuleListItem,
  iBAS_CODE_DTL,
} from '/@/api/tigerapi/model/mesModel';
import { useUserStore } from '/@/store/modules/user';
import { buildUUID } from '/@/utils/uuid';
import { formatToDateTime } from '/@/utils/dateUtil';
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
import { isEmpty, isNullOrEmpty } from '/@/utils/is';
 
export default defineComponent({
  name: 'RuleDetail',
  components: {
    BasicForm,
    BasicTable,
    PageWrapper,
    CheckruleDrawer,
    [Card.name]: Card,
    TableAction,
  },
  setup(props) {
    const route = useRoute();
    const go = useGo();
    const tableRef = ref<Nullable<TableActionType>>(null);
    const RuleObj = ref(JSON.parse(decodeURI(route.params?.id as string)));
    const { setTitle } = useTabs();
    const isUpdate = ref(true);
    const canResize = ref(false);
    const loading = ref(false);
    const striped = ref(true);
    const border = ref(true);
    const pagination = ref<any>(false);
    const actionColumn = ref({
      width: 80,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
    });
    const tabStore = useMultipleTabStore();
    const router = useRouter();
 
    const { currentRoute } = router;
    function getCurrentTab() {
      const route = unref(currentRoute);
      return tabStore.getTabList.find((item) => item.fullPath === route.fullPath)!;
    }
    const currentTab = getCurrentTab();
    var _title =
      RuleObj.value.RULE_CODE == '0' ? '新增规则' : '编辑规则:' + RuleObj.value.RULE_CODE;
    setTitle(_title);
    const [registerDrawer, { openDrawer }] = useDrawer();
    const [register, { resetFields, setFieldsValue, getFieldsValue, validate }] = useForm({
      labelWidth: 120,
      baseColProps: {
        span: 24,
      },
      schemas: newFormSchema,
      showActionButtonGroup: false,
    });
    const [
      registerTest,
      {
        setFieldsValue: setFieldsValueTest,
        getFieldsValue: getFieldsValueTest,
        validate: validateTestForm,
      },
    ] = useForm({
      labelWidth: 120,
      baseColProps: {
        span: 24,
      },
      schemas: testSchemas,
      showActionButtonGroup: false,
    });
    const params: CheckRuleDtlParams = {
      RULE_ID: RuleObj.value.RULE_CODE == '0' ? buildUUID() : (RuleObj.value.ID as string),
    };
    const data = ref([] as iBAS_CODE_DTL[]);
    onMounted(async () => {
      resetFields();
      pagination.value = { pageSize: 6 };
      data.value = (await getRuleDtl(params)).Items;
      const ruleData = (await getRuleById(params)).Items as CheckRuleListItem[];
      if (!isNullOrEmpty(ruleData)) {
        setFieldsValue(ruleData[0]);
        setFieldsValueTest({
          CHECK_REGEX: ruleData[0]?.CHECK_REGEX,
        });
      } else {
        // setFieldsValue({
        //   ID: buildUUID(),
        //   CHECK_TYPE:0,
        //   CODE_LENGTH:0,
        // });
        // setFieldsValueTest({});
      }
    });
 
    function getTableAction() {
      const tableAction = unref(tableRef);
      if (!tableAction) {
        throw new Error('tableAction is null');
      }
      return tableAction;
    }
 
    async function submitAll() {
      try {
        const [values, testValues] = await Promise.all([validate(), validateTestForm()]);
        values.ID = params.RULE_ID;
        const action = await SaveRule({ ...values, ...testValues });
        if (action.IsSuccessed) {
          await DeleteRuleDtl(params.RULE_ID);
          const dtlAction = await SaveRuleDtl(data.value);
          if (dtlAction.IsSuccessed) {
            cancel();
          }
        }
      } catch (error) { }
    }
    async function cancel() {
      try {
        tabStore.closeTab(currentTab, router);
      } catch (error) { }
    }
    async function handleCreate() {
      await validate();
      openDrawer(true, {
        RULE_ID: params.RULE_ID,
        isUpdate: false,
      });
    }
 
    function handleEdit(record: Recordable) {
      openDrawer(true, {
        record,
        isUpdate: true,
      });
    }
 
    function handleDelete(record: Recordable) {
      var _data = data.value.map((item) => {
        return item;
      });
      let index = _data.indexOf(record);
      _data.splice(index, 1);
      data.value = _data;
      getTableAction().setProps({
        dataSource: [],
      });
      getTableAction().setProps({
        dataSource: data,
      });
      getTableAction().reload();
      setData();
    }
 
    //新增规则明细
    function handleSuccess(d, u) {
      var Id = buildUUID();
      isUpdate.value = u.isUpdate;
      if (u.isUpdate) {
        //更新
        var _data = data.value.map((item) => {
          if (item.ID == d.ID)
            return {
              ...item,
              CHECK_LENGTH: d.CHECK_LENGTH,
              DATA_CASE: d.DATA_CASE,
              DATA_TYPE: d.DATA_TYPE,
              DATA_REGEX: d.DATA_REGEX,
              SERIAL_MAX: d.SERIAL_MAX,
              SERIAL_MIN: d.SERIAL_MIN,
              DATA_VALUE: d.DATA_VALUE,
              REPEAT_TIMES: d.REPEAT_TIMES,
              DESCRIPTION: d.DESCRIPTION,
              CHECK_TABLE: d.CHECK_TABLE,
              CHECK_FIELD: d.CHECK_FIELD,
              FIXED_NOTLIKE: d.FIXED_NOTLIKE,
              SERIAL_INTERVAL: d.SERIAL_INTERVAL,
              SERIAL_IGNORE: d.SERIAL_IGNORE,
            };
          return item;
        });
        data.value = _data;
      } else {
        //新增
        d.ID = Id;
        d.CREATE_USER = useUserStore().getUserInfo.userId as string;
        d.UPDATE_TIME = formatToDateTime(new Date());
        d.UPDATE_USER = useUserStore().getUserInfo.userId as string;
        var _data2: any[] = [];
        if (!isEmpty(data.value)) {
          _data2 = data.value.map((item) => {
            return item;
          });
          d.RULE_SEQ =
            data.value.reduce((maxLength, item) => {
              return Math.max(maxLength, item.RULE_SEQ);
            }, 0) + 1;
        } else {
          d.RULE_SEQ = 1;
        }
        // _data2.splice(0, 0, d);
        _data2.push(d);
        data.value = _data2;
      }
 
      //重新修改数据源
 
      getTableAction().setProps({
        dataSource: [],
      });
      getTableAction().setProps({
        dataSource: data,
      });
      getTableAction().reload();
      setData();
    }
    function setData() {
      var CHECK_TYPE = 0;
      var CHECK_REGEX = '^';
      const total = data.value.reduce((length, item) => {
        if (item.DATA_TYPE == 6) {
          CHECK_TYPE = 1;
        }
        CHECK_REGEX += item.DATA_REGEX;
        return (length += Number(item.CHECK_LENGTH) * Number(item.REPEAT_TIMES));
      }, 0);
 
      setFieldsValue({
        ...getFieldsValue(),
        ...{
          CODE_LENGTH: total,
          CHECK_TYPE: CHECK_TYPE,
        },
      });
      setFieldsValueTest({
        CHECK_REGEX: CHECK_REGEX + '$',
      });
    }
    function goBack() {
      // 本例的效果时点击返回始终跳转到账号列表页,实际应用时可返回上一页
      go('/CheckRule');
    }
    return {
      columns,
      dtlColumns,
      register,
      registerTest,
      resetFields,
      setFieldsValue,
      getFieldsValue,
      validate,
      data,
      canResize,
      loading,
      striped,
      border,
      pagination,
      actionColumn,
      registerDrawer,
      submitAll,
      cancel,
      tableRef,
      goBack,
      setData,
      handleCreate,
      handleEdit,
      handleDelete,
      handleSuccess,
    };
  },
});
</script>
<style lang="less" scoped>
.high-form {
  padding-bottom: 48px;
}
</style>