xiaoyi
3 天以前 07bb7e462ea911a698da03cf50e92103e6434e88
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
<script lang="ts" setup>
  import type { VbenFormSchema } from '#/adapter/form';
  import type { MesPdPriceApi } from '#/api/mes/pd/price';
 
  import { ref } from 'vue';
 
  import { useVbenModal } from '@vben/common-ui';
  import { $t } from '@vben/locales';
 
  import { Descriptions, Empty, Table } from 'ant-design-vue';
 
  import { useVbenForm } from '#/adapter/form';
  import { calcPrice } from '#/api/mes/pd/price';
  import { getServicePackageList, getVoltageLevelList } from '#/api/mes/pd/basedata';
 
  const result = ref<MesPdPriceApi.PriceCalcResp>();
  const hasResult = ref(false);
 
  const [Form, formApi] = useVbenForm({
    commonConfig: {
      componentProps: {
        class: 'w-full',
      },
      formItemClass: 'col-span-1',
      labelWidth: 130,
    },
    wrapperClass: 'grid-cols-3',
    layout: 'horizontal',
    schema: [],
    showDefaultActions: false,
  });
 
  /** 测算表单字段 */
  function useCalcSchema(): VbenFormSchema[] {
    return [
      {
        fieldName: 'calcType',
        label: '测算类型',
        component: 'Select',
        rules: 'selectRequired',
        componentProps: {
          options: [
            { label: '服务套餐', value: 1 },
            { label: '电压等级', value: 2 },
          ],
          placeholder: '请选择测算类型',
        },
      },
      {
        fieldName: 'packageId',
        label: '服务套餐',
        component: 'Select',
        rules: 'selectRequired',
        dependencies: {
          triggerFields: ['calcType'],
          show: (values) => values.calcType === 1,
          async componentProps(values, form) {
            if (values.calcType !== 1) {
              form.setFieldValue('packageId', undefined);
              return { allowClear: true, options: [], placeholder: '请选择服务套餐' };
            }
            const list = await getServicePackageList();
            return {
              allowClear: true,
              options: list.map((i) => ({ label: i.name, value: i.id })),
              placeholder: '请选择服务套餐',
            };
          },
        },
      },
      {
        fieldName: 'voltageLevelId',
        label: '电压等级',
        component: 'Select',
        rules: 'selectRequired',
        dependencies: {
          triggerFields: ['calcType'],
          show: (values) => values.calcType === 2,
          async componentProps(values, form) {
            if (values.calcType !== 2) {
              form.setFieldValue('voltageLevelId', undefined);
              return { allowClear: true, options: [], placeholder: '请选择电压等级' };
            }
            const list = await getVoltageLevelList();
            return {
              allowClear: true,
              options: list.map((i) => ({ label: i.name, value: i.id })),
              placeholder: '请选择电压等级',
            };
          },
        },
      },
      {
        fieldName: 'quantity',
        label: '用电量(kWh)',
        component: 'InputNumber',
        componentProps: { class: '!w-full', min: 0, precision: 2 },
        rules: 'required',
      },
    ];
  }
 
  const [Modal, modalApi] = useVbenModal({
    async onConfirm() {
      const { valid } = await formApi.validate();
      if (!valid) {
        return;
      }
      modalApi.lock();
      try {
        const data = (await formApi.getValues()) as MesPdPriceApi.PriceCalcReq;
        result.value = await calcPrice(data);
        hasResult.value = true;
      } finally {
        modalApi.unlock();
      }
    },
    async onOpenChange(isOpen: boolean) {
      if (!isOpen) {
        return;
      }
      result.value = undefined;
      hasResult.value = false;
      formApi.setState({ schema: useCalcSchema() });
      formApi.resetForm();
      modalApi.setState({ confirmText: '测算', showConfirmButton: true });
    },
  });
</script>
 
<template>
  <Modal :title="'阶梯电价测算'" class="w-4/5">
    <Form class="mx-4 mb-4" />
    <div v-if="hasResult && result" class="mx-4">
      <Descriptions
        :column="3"
        bordered
        size="small"
        class="mb-4"
      >
        <Descriptions.Item label="原价">
          {{ result.originalPrice ?? 0 }}
        </Descriptions.Item>
        <Descriptions.Item label="优惠金额">
          {{ result.discountAmount ?? 0 }}
        </Descriptions.Item>
        <Descriptions.Item label="优惠后总价">
          <span class="text-lg font-bold text-primary">
            {{ result.finalPrice ?? 0 }}
          </span>
        </Descriptions.Item>
      </Descriptions>
      <Table
        :columns="[
          { title: '档位下限(含,kWh)', dataIndex: 'startValue', key: 'startValue' },
          { title: '档位上限(不含,kWh)', dataIndex: 'endValue', key: 'endValue' },
          { title: '本档电量(kWh)', dataIndex: 'tierQuantity', key: 'tierQuantity' },
          { title: '档位单价', dataIndex: 'pricePerUnit', key: 'pricePerUnit' },
          { title: '小计', dataIndex: 'subtotal', key: 'subtotal' },
        ]"
        :data-source="result.details ?? []"
        :pagination="false"
        size="small"
        class="mb-4"
      />
    </div>
    <div v-else class="mx-4">
      <Empty description="请选择测算类型并输入用电量后点击「测算」" />
    </div>
  </Modal>
</template>