AddIndicatorsModal.tsx
10 KB
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
/* eslint-disable no-return-assign */
/* eslint-disable no-return-assign */
import React, { useEffect, useState } from "react";
import { Modal, Form, Select, InputNumber, message, Spin, Radio, Input, Button } from "antd";
import { saveRewardsListApi } from "../RewardsList/api";
import { useStore } from "../index";
import { queryPostIndicatorApi } from "@/pages/performance/KpiGroupSetting/EditComfirm/api";
import _ from "lodash";
import useInitail from "@/hooks/useInitail";
import LadderTable from "@/pages/performance/EvaGroupSetting/EditComfirm/components/LadderTable";
import CommissionParams from "@/pages/performance/EvaGroupSetting/EditComfirm/components/CommissionParams";
import LadderParams from "@/pages/performance/EvaGroupSetting/EditComfirm/components/LadderParams";
import Conds from "@/pages/performance/EvaGroupSetting/EditComfirm/components/Conds";
import { EvaGroupSetteing } from "@/pages/performance/EvaGroupSetting/interface";
const Option = Select.Option;
interface Props {
visible: boolean;
onCancel: Function;
postId?: number;
shopIds?: string;
onOk: (vales: any) => void;
currentItem: EvaGroupSetteing.Indicators;
comItem: EvaGroupSetteing.CommissionParams;
scopeType: number;
}
export default function AddIndicatorsModal(props: Props) {
const [form] = Form.useForm();
// console.log(form);
const { paramAlias, setParamAlias, preconditionAlias, setPreconditionAlias } = useStore();
// 是否为百分比
const { visible, onCancel, postId, shopIds, onOk, currentItem, comItem, scopeType } = props;
const [isPercent, setIsPercent] = useState<number>(0);
const [delay, setDelay] = useState(true);
// 显示前置条件
const [condsVisible, setCondsVisible] = useState(false);
// 判断占比相加是否为100
const [isHundred, setIsHundred] = useState(false);
// 保存过滤后指标列表
// const [newIndicators, setNewIndicators] = useState<EvaGroupSetteing.IndicatorByPost[]>([]);
const [id, setItemId] = useState<number>();
// 修改
useEffect(() => {
if (visible && currentItem.name) {
setParamAlias(currentItem.paramAlias);
form.setFieldsValue({
...currentItem,
});
}
if (currentItem.conds && currentItem.conds.length > 0) {
setPreconditionAlias(currentItem.preconditionAlias);
setCondsVisible(true);
} else {
setCondsVisible(false);
}
}, [visible]);
// useEffect(() => {
// if (indicatorsList && indicatorsList.length > 0 && !currentItem.id) {
// const res = indicatorsList.filter((item) => !selectedIndicators.find((y) => Number(y) === item.id));
// setNewIndicators([...res]);
// }
// }, [indicatorsList]);
// 校验表单数据
function transformDTO(formData: any) {
console.log("InformData", formData);
let detail = {};
_.each(formData, (value: any, key: string) => {
switch (key) {
case "commissionParams":
const _options = value || {};
detail.indicatorId = _options.key;
detail.name = _options.label;
detail.code = _options.value;
break;
default:
detail[key] = value;
break;
}
});
return formData;
}
function handSubmit(fieldsValue: any) {
console.log(fieldsValue);
const pa: any = transformDTO(fieldsValue);
// pa.targetType = targetType;
// pa.id = id;
pa.paramAlias = paramAlias;
if (pa?.ladderParams && pa?.ladderParams.length > 0) {
if (isPercent == 0) {
pa.laddersType = currentItem.laddersType;
} else {
pa.laddersType = isPercent;
}
}
console.log("大", pa);
let hundred = 0;
if (pa?.commissionParams) {
hundred = pa?.commissionParams.reduce((data: number, item: EvaGroupSetteing.CommissionParams) => {
return data + item.proportion;
}, 0);
}
if (pa?.ladderParams) {
hundred = pa?.ladderParams.reduce((data: number, item: EvaGroupSetteing.CommissionParams) => {
return data + item.proportion;
}, 0);
}
//设置conds的sort
if (pa?.conds && pa?.conds.length > 0) {
pa?.conds.forEach((item: any, index: number) => {
Object.assign(item, { sort: index + 1 });
});
pa.preconditionAlias = preconditionAlias;
}
console.log("cond", pa?.conds);
// 校验标准分不能大于绩效分值
const indicatorValue = pa.baseScore;
if (!!pa.ladders && pa.ladders.length > 0) {
const _indicatorLadders: any[] = pa.ladders;
const res = _indicatorLadders.every((x, index) => {
if (x.standardScore <= indicatorValue) {
return true;
}
message.error(`第${index + 1}组标准分不满足条件: 标准分不能大于绩效分值`, 3);
return false;
});
const _result = verifySteps(_indicatorLadders);
if (!(res && _result)) {
return;
}
}
const newItemId = pa.id;
// 编辑时,不需要push id
// if (currentItem.id) {
// const tmpIds = [...selectedIndicators];
// tmpIds.push(newItemId);
// setSelectedIndicators([...tmpIds]);
// setSelectedIndicatorsConds([...tmpIds]);
// }
if (hundred === 100) {
setIsHundred(false);
onOk(pa);
console.log(pa);
onCancel && onCancel();
} else {
setIsHundred(true);
}
}
// 校验阶梯越小,标准分越小
const verifySteps = (_indicatorLadders: any[]) => {
let res = true;
_indicatorLadders.forEach((item, index) => {
if (index > 0) {
const curScore = item.standardScore;
const lastScore = _indicatorLadders[index - 1].standardScore;
if (curScore < lastScore) {
message.error(`第${index + 1}组标准分不满足条件: 阶梯越小,标准分越小`, 3);
res = false;
}
}
});
return res;
};
return (
<Modal
title={`${currentItem.name ? "编辑" : "新增"}指标`}
visible={visible}
maskClosable={false}
afterClose={() => {
form.resetFields();
onCancel();
}}
onCancel={() => onCancel()}
onOk={form.submit}
width={1200}
>
<Form
form={form}
labelCol={{ span: 6 }}
wrapperCol={{ span: 18 }}
onFinish={handSubmit}
initialValues={{
ladders: [{ lower: 0, standardScore: 0, key: 0 }],
}}
>
<Form.Item name="name" label="指标名称" rules={[{ required: true, message: "请选择指标" }]}>
<Input placeholder="请输入指标名称" />
</Form.Item>
<Form.Item label="绩效分值" name="baseScore" rules={[{ required: true, message: "请输入绩效分值" }]}>
<InputNumber placeholder="请输入" style={{ width: "100%" }} addonAfter="分" />
</Form.Item>
<Form.Item name="scoreWay" label="计算方式" rules={[{ required: true, message: "请选择计算方式" }]}>
<Select placeholder="选择计算方式">
<Option value={1} key={1}>
台阶得分
</Option>
<Option value={2} key={2}>
固定得分
</Option>
</Select>
</Form.Item>
<div style={{ marginLeft: 218, marginBottom: 20 }}>
<span>是否添加前置条件设置:</span>
<Radio.Group
onChange={(e) => {
setCondsVisible(e.target.value);
}}
value={condsVisible}
>
<Radio value>是</Radio>
<Radio value={false}>否</Radio>
</Radio.Group>
</div>
{condsVisible && (
<Form.Item name="conds" label="前置条件">
<Conds postId={postId} shopIds={shopIds} setItemId={setItemId} scopeType={scopeType} />
</Form.Item>
)}
<Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.scoreWay !== currentValues.scoreWay}>
{({ getFieldValue }) =>
getFieldValue("scoreWay") === 1 ? (
<>
<Form.Item
name="ladderParams"
label="台阶得分"
rules={[{ required: true, message: "请增加台阶得分指标" }]}
>
<LadderParams
postId={postId}
shopIds={shopIds}
setItemId={setItemId}
isHundred={isHundred}
scopeType={scopeType}
/>
</Form.Item>
<Form.Item name="ladders" label="台阶得分阶梯" rules={[{ required: true, message: "台阶得分阶梯" }]}>
<LadderTable visible isPercent={isPercent} laddersType={currentItem.laddersType} />
</Form.Item>
</>
) : getFieldValue("scoreWay") === 2 ? (
<Form.Item
name="commissionParams"
label="固定得分"
rules={[{ required: true, message: "请增加固定得分指标" }]}
>
<CommissionParams
postId={postId}
shopIds={shopIds}
setItemId={setItemId}
isHundred={isHundred}
scopeType={scopeType}
/>
</Form.Item>
) : null
}
</Form.Item>
<Form.Item
noStyle
shouldUpdate={(prevValues, currentValues) => prevValues.ladderParams !== currentValues.ladderParams}
>
{({ getFieldValue }) => {
const ladderParams = getFieldValue("ladderParams");
if (ladderParams && ladderParams.length > 0) {
console.log("ladderParams", ladderParams);
if (ladderParams.length > 1) {
setIsPercent(2);
} else if (ladderParams[0].targetValue) {
setIsPercent(2);
} else if (ladderParams[0].dataType === 2) {
setIsPercent(2);
} else if (ladderParams[0].dataType === 1 || ladderParams[0].dataType === 3) {
setIsPercent(1);
}
}
return null;
}}
</Form.Item>
</Form>
</Modal>
);
}