todo: KPI 设置和查看
parent
31481fd301
commit
836d4b9597
@ -0,0 +1,135 @@
|
||||
import { makeAutoObservable, runInAction, toJS } from 'mobx';
|
||||
import * as req from '../utils/request';
|
||||
import { isEmpty, sortBy, groupBy, cloneDeep } from '../utils/commons';
|
||||
import moment from 'moment';
|
||||
|
||||
const currentYear = moment().year();
|
||||
|
||||
class KPI {
|
||||
constructor(appStore) {
|
||||
this.appStore = appStore;
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
saveOrUpdate(x) {
|
||||
console.log('ssssssssssss', x);
|
||||
console.log(toJS(this.pageData));
|
||||
const tableData = this.pageData.reduce((r, curObj) => {
|
||||
const allMonth = new Array(12).fill(1).map((_, index) => {
|
||||
const mIndex = index+1;
|
||||
const startM = moment([this.settingYear, index, 1]);
|
||||
const pick = (({
|
||||
object, object_name, object_id, subject,date_type,
|
||||
}) => ({
|
||||
object, object_name, object_id, subject,date_type,
|
||||
}))(curObj);
|
||||
return {
|
||||
...pick,
|
||||
start_date: startM.format('YYYY-MM-DD'),
|
||||
end_date: startM.endOf('M').format('YYYY-MM-DD HH:mm:ss'),
|
||||
value: curObj[`M${mIndex}`],
|
||||
// ...(curObj[`M${mIndex}_id`] ? { kpi_id: curObj[`M${mIndex}_id`] } : {}),
|
||||
kpi_id: curObj[`M${mIndex}_id`] || undefined,
|
||||
key: undefined,
|
||||
};
|
||||
});
|
||||
console.log('cccccccccc', allMonth);
|
||||
return r.concat(allMonth);
|
||||
}, []);
|
||||
console.log('ppppp', tableData);
|
||||
const data = { 'kpis': tableData };
|
||||
req.postJSON('/service-Analyse2/setkpi_multi/test', data).then((json) => {
|
||||
// req.postJSON('/service-Analyse2/setkpi_multi', data).then((json) => {
|
||||
if (json.errcode === 0) {
|
||||
runInAction(() => {
|
||||
console.log({ loading: false, ...json }, 'post kpi');
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getList() {
|
||||
const param = {
|
||||
date_type: 'applyDate',
|
||||
start_date: '2020-01-01',
|
||||
end_date: '2024-12-31 23:59:59',
|
||||
};
|
||||
// return req.fetchJSON('/service-Analyse2/getkpi/test', param).then((json) => {
|
||||
return req.fetchJSON('/service-Analyse2/getkpi', param).then((json) => {
|
||||
if (json.errcode === 0) {
|
||||
runInAction(() => {
|
||||
this.originData = json.result;
|
||||
const yearData = parseKPI(json.result, ['subject', 'object_id']);
|
||||
console.log(111, yearData, yearData[this.settingYear]);
|
||||
this.pageData = yearData?.[this.settingYear]?.[this.settingSubject] || [];
|
||||
});
|
||||
}
|
||||
return this.pageData;
|
||||
});
|
||||
}
|
||||
|
||||
handleTableEdit(data) {
|
||||
console.log('handle change ', data);
|
||||
// this.pageData = data;
|
||||
}
|
||||
|
||||
settingYear = 2023;
|
||||
settingSubject = 'sum_profit';
|
||||
|
||||
originData =[];
|
||||
pageData = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 把kpi数据按照对象和类型转换格式
|
||||
* @param {array} keyArr 转换的字段的顺序, 返回结果的key层级
|
||||
* @example
|
||||
* * parseKPI(json.result, ['object_id', 'subject']);
|
||||
* // => { 2023: { 114: [{...}, {...}], 216: [...] } }
|
||||
* * parseKPI(json.result, ['subject', 'object_id']);
|
||||
* // => { 2023: { in_order_count: [{...}, {...}], sum_profit: [...] } }
|
||||
*/
|
||||
export const parseKPI = (kpis, keyArr = []) => {
|
||||
const result = kpis.map((row) => ({
|
||||
...row,
|
||||
yearIndex: moment(row.start_date).year(),
|
||||
monthIndex: moment(row.start_date).month() + 1,
|
||||
monthRange: [moment(row.start_date).month() + 1, moment(row.end_date).month() + 1],
|
||||
fullYear: moment(row.start_date).month() === 0 && moment(row.end_date).month() === 11,
|
||||
}));
|
||||
const byYear = groupBy(result, (ele) => ele.yearIndex);
|
||||
const yearData = {};
|
||||
const initialPercentKey = new Array(12).fill(1).reduce((r, v, i) => ({ ...r, [`M${i+1}Percent`]: 0 }), {});
|
||||
const ret = {};
|
||||
const [key0, key1] = keyArr;
|
||||
Object.keys(byYear).map((_yearVal) => {
|
||||
const _subjectRet = groupBy(byYear[_yearVal], (ele) => `${ele[key0]}`);
|
||||
Object.keys(_subjectRet).map((_subject) => {
|
||||
const subjectObject = groupBy(_subjectRet[_subject], (row) => row[key1]);
|
||||
const afterGroup = Object.keys(subjectObject).map((oID) => {
|
||||
const _ByFull = subjectObject[oID].reduce((r, v) => {
|
||||
(r[String(v.fullYear)] || (r[String(v.fullYear)] = [])).push(v);
|
||||
return r;
|
||||
}, {});
|
||||
const kpiYear = (_ByFull?.true || []).reduce((r, v) => {
|
||||
// r.push({ monthIndex: v.monthIndex, yearIndex: v.yearIndex, value: v.value, kpi_id: v.kpi_id });
|
||||
return { monthIndex: v.monthIndex, yearIndex: v.yearIndex, value: v.value, kpi_id: v.kpi_id };
|
||||
}, {});
|
||||
const kpiData = _ByFull.false.reduce((r, v) => {
|
||||
r.push({ monthIndex: v.monthIndex, yearIndex: v.yearIndex, value: v.value, kpi_id: v.kpi_id, percentVal: (v.value/kpiYear.value*100) });
|
||||
return r;
|
||||
}, []);
|
||||
const kpiDataMapped = kpiData.reduce((r, v) => ({...r, [`M${v.monthIndex}`]: v }), {});
|
||||
const kpiDataFlat = kpiData.reduce((r, v) => ({...r, [`M${v.monthIndex}Val`]: v.value, [`M${v.monthIndex}Percent`]: v.percentVal}), {});
|
||||
const { start_date, end_date, kpi_id, value, unit, monthIndex, monthRange, ...objectEle } = subjectObject[oID][0];
|
||||
return { ...cloneDeep(initialPercentKey), ...objectEle, ...kpiDataFlat, kpiData, kpiDataMapped, kpiYear, yearValue: kpiYear?.value || 0 };
|
||||
});
|
||||
ret[_subject] = afterGroup;
|
||||
return 1;
|
||||
});
|
||||
Object.assign(yearData, { [_yearVal]: ret });
|
||||
return _yearVal;
|
||||
});
|
||||
return yearData;
|
||||
};
|
||||
export default KPI;
|
Loading…
Reference in New Issue