ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import type { FormInstance } from 'ant-design-vue';
 
import { computed, reactive, ref, toRefs } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import { omit } from 'lodash-es';
 
import { getColumnList } from '#/api/onlineDev/visualDev';
import { create, getInfo, update } from '#/api/system/dataAuthorize';
import ConditionMain from '#/components/ColumnDesign/src/components/ConditionMain.vue';
import { $t } from '#/locales';
 
interface State {
  dataForm: any;
  dataRule: any;
  fieldOptions: any[];
}
 
const emit = defineEmits(['register', 'reload']);
const state = reactive<State>({
  dataForm: {
    id: '',
    moduleId: '',
    enCode: '',
    fullName: '',
    conditionJson: '',
    conditionText: '',
    matchLogic: 'and',
  },
  dataRule: {
    fullName: [{ required: true, message: '必填', trigger: 'blur' }],
    enCode: [{ required: true, message: '必填', trigger: 'blur' }],
  },
 
  fieldOptions: [],
});
const { dataForm, dataRule } = toRefs(state);
const formElRef = ref<FormInstance>();
const conditionMainRef = ref();
const keyList = new Set([
  'calculate',
  'createTime',
  'createUser',
  'currOrganize',
  'currPosition',
  'dateCalculate',
  'datePicker',
  'groupSelect',
  'inputNumber',
  'modifyTime',
  'modifyUser',
  'organizeSelect',
  'posSelect',
  'rate',
  'roleSelect',
  'slider',
  'timePicker',
  'userSelect',
  'usersSelect',
]);
 
const getTitle = computed(() => (state.dataForm.id ? $t('common.editText') : $t('common.addText')));
const { createMessage } = useMessage();
const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner(init);
 
function init(data) {
  state.dataForm.enCode = '';
  state.dataForm.fullName = '';
  state.dataForm.matchLogic = 'and';
  state.dataForm.id = data.id || '';
  state.dataForm.moduleId = data.menuId || '';
  changeLoading(true);
  getColumnList({ menuId: state.dataForm.moduleId })
    .then((res) => {
      state.fieldOptions =
        res.data.map((o) => ({
          ...omit(o, ['jnpfKey']),
          fullName: o.fieldName,
          id: o.fieldId,
          __config__: { jnpfKey: keyList.has(o.jnpfKey) ? o.jnpfKey : 'input', label: o.fieldName },
        })) || [];
      if (state.dataForm.id) {
        changeLoading(true);
        getInfo(state.dataForm.id)
          .then((res) => {
            state.dataForm = res.data;
            const conditions = res.data.conditionJson ? JSON.parse(res.data.conditionJson) : [];
            conditionMainRef.value?.init({ conditionList: conditions.conditionList, matchLogic: state.dataForm.matchLogic, fieldOptions: state.fieldOptions });
            changeLoading(false);
          })
          .then(() => {
            changeLoading(false);
          });
      } else {
        conditionMainRef.value?.init({ fieldOptions: state.fieldOptions });
        changeLoading(false);
      }
    })
    .then(() => {
      changeLoading(false);
    });
}
async function handleSubmit() {
  try {
    const values = await formElRef?.value?.validate();
    const conditions = conditionMainRef.value?.confirm();
    if (!values || !conditions) return;
    state.dataForm.conditionText = getConditionsContent(conditions.conditionList, conditions.matchLogic);
    state.dataForm.conditionJson = JSON.stringify(conditions);
    state.dataForm.matchLogic = conditions.matchLogic;
    changeOkLoading(true);
    const formMethod = state.dataForm.id ? update : create;
    formMethod(state.dataForm)
      .then((res) => {
        createMessage.success(res.msg);
        changeOkLoading(false);
        closeModal();
        emit('reload');
      })
      .catch(() => {
        changeOkLoading(false);
      });
  } catch {}
}
function getConditionsContent(conditions, matchLogic) {
  let content = '';
  for (let i = 0; i < conditions.length; i++) {
    const e = conditions[i];
    content += conditions.length == 1 ? '' : `${i == 0 ? '' : ` ${matchLogic} `}( `;
    for (let j = 0; j < e.groups.length; j++) {
      const groups = e.groups[j];
      const logic = j == 0 ? '' : ` ${e.logic} `;
      const text = ` ${groups.fieldName} ${groups.symbolName}${
        groups.fieldLabel ? groups.fieldLabel : groups.fieldValue || groups.fieldValue === 0 ? groups.fieldValue : ''
      } `;
      content += logic + text;
    }
    content += conditions.length == 1 ? '' : ' )';
  }
  return content;
}
</script>
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" show-ok-btn @ok="handleSubmit" class="jnpf-condition-modal" destroy-on-close>
    <a-form :colon="false" :label-col="{ style: { width: '0' } }" :model="dataForm" :rules="dataRule" ref="formElRef">
      <a-form-item name="enCode">
        <a-input v-model:value="dataForm.enCode" placeholder="请输入编码" :maxlength="50" />
      </a-form-item>
      <a-form-item name="fullName">
        <a-input v-model:value="dataForm.fullName" placeholder="请输入方案名称" />
      </a-form-item>
      <ConditionMain ref="conditionMainRef" default-add-empty show-field-value-type is-custom-field-value-type />
    </a-form>
  </BasicModal>
</template>