You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dashboard/src/stores/KPI.js

116 lines
4.7 KiB
JavaScript

import { makeAutoObservable, runInAction, toJS } from 'mobx';
import * as req from '../utils/request';
import { isEmpty, sortBy, groupBy, cloneDeep, fixTo4Decimals, flush } from '../utils/commons';
import moment from 'moment';
class KPI {
constructor(appStore) {
this.appStore = appStore;
makeAutoObservable(this);
}
async delByID(ids) {
const data = { 'kpi_ids': ids };
const json = await req.delJSON('/service-Analyse2/delkpi_multi', data);
return json.errcode === 0;
}
async saveOrUpdate(tableData) {
const data = { 'kpis': tableData };
const json = await req.postJSON('/service-Analyse2/setkpi_multi', data);
return json.errcode === 0;
}
async onSubmit(tableData, { delQueue }) {
const flushData = tableData.filter(row => !isEmpty(row.value) || !isEmpty(row?.kpi_id));
const postRes = isEmpty(flushData) ? true : await this.saveOrUpdate(flushData);
const delRes = isEmpty(flush(delQueue)) ? true : await this.delByID(delQueue);
return postRes && delRes;
}
getList(param = {}) {
const _param = {
date_type: 'applyDate',
start_date: '2020-01-01',
end_date: '2024-12-31 23:59:59',
...param,
};
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;
});
}
settingYear = moment().year();
setSettingYear(v) {
this.settingYear = v;
}
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: (fixTo4Decimals(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];
const allKey = !isEmpty(kpiData) ? kpiData.map(ek => ek.kpi_id).join('_') : `${Object.values(kpiYear).join('_')}`;
return { ...cloneDeep(initialPercentKey), ...objectEle, ...kpiDataFlat, kpiData, kpiDataMapped, kpiYear, yearValue: kpiYear?.value || 0, key: allKey };
});
ret[_subject] = afterGroup;
return 1;
});
Object.assign(yearData, { [_yearVal]: ret });
return _yearVal;
});
return yearData;
};
export default KPI;