import {makeAutoObservable, runInAction} from "mobx"; import * as config from "../config"; import * as comm from '@haina/utils-commons'; import { fetchJSON } from '@haina/utils-request'; // 财务管理 class FinancialStore { constructor(rootStore) { this.rootStore = rootStore; makeAutoObservable(this); this.get_bill_types(); } bill_type_data = { data: [], bill_type_mode: false, bill_types: ['ALL'], bt_handleChange: this.bt_handleChange.bind(this), }; bt_handleChange(value) { this.bill_type_data.bill_types = value; }; get_bill_types() { const date_picker_store = this.rootStore.date_picker_store; const url = '/service-web/QueryData/GetCreditCardBillType'; fetch(config.HT_HOST + url) .then((response) => response.json()) .then((json) => { runInAction(() => { this.bill_type_data.data = json.billtype; }); }) .catch((error) => { console.log('fetch data failed', error); }); } set_bill_filtered(values) { if (Array.isArray(values)) { this.credit_card_data.filteredValue = values; } else if (comm.emptyValue(values)) { this.credit_card_data.filteredValue = []; } else { this.credit_card_data.filteredValue = [values]; } } set_active_tab(value) { this.credit_card_data.active_tab = value; } set_table_handleChange = (pagination, filters, sorter) => { const filters_arr = Object.values(filters); if (!comm.emptyValue(filters_arr)) { this.set_bill_filtered(filters_arr[0]); } }; credit_card_data = { data: [], data_by_type: [], active_tab: 'summarized_data', loading: false, bu_select_mode: false, business_units: ['ALL'], group_select_mode: false, groups: ['ALL'], filteredValue: [],// 默认的筛选值 bu_handleChange: this.bu_handleChange.bind(this), group_handleChange: this.group_handleChange.bind(this), set_bill_filtered: this.set_bill_filtered.bind(this), set_active_tab: this.set_active_tab.bind(this), set_table_handleChange: this.set_table_handleChange.bind(this), }; bu_handleChange(value) { this.credit_card_data.business_units = value; }; group_handleChange(value) { this.credit_card_data.groups = value; }; setSearchValues(obj, values) { this.credit_card_data.business_units = obj.businessUnits; // this.credit_card_data.groups = obj.businessUnits; this.bill_type_data.bill_types = obj.billtype; } // 请求信用卡账单 get_credit_card_bills() { const date_picker_store = this.rootStore.date_picker_store; let url = '/service-web/QueryData/GetCreditCardBills'; url += `?business_unit=${this.credit_card_data.business_units}&groups=${this.credit_card_data.groups.toString()}&billtype=${this.bill_type_data.bill_types}`; url += '&billdate1=' + date_picker_store.start_date.format(config.DATE_FORMAT) + '&billdate2=' + date_picker_store.end_date.format(config.SMALL_DATETIME_FORMAT); if (date_picker_store.start_date_cp && date_picker_store.end_date_cp) { url += '&billdateOld1=' + date_picker_store.start_date_cp.format(config.DATE_FORMAT) + '&billdateOld2=' + date_picker_store.end_date_cp.format(config.SMALL_DATETIME_FORMAT); } fetch(config.HT_HOST + url) .then((response) => response.json()) .then((json) => { runInAction(() => { this.credit_card_data.data = json; }); }) .catch((error) => { console.log('fetch data failed', error); }); } // 请求信用卡账单 get_credit_card_bills_by_type() { const date_picker_store = this.rootStore.date_picker_store; let url = '/service-web/QueryData/GetCreditCardBillsByType'; url += `?business_unit=${this.credit_card_data.business_units}&groups=${this.credit_card_data.groups.toString()}`; url += '&billdate1=' + date_picker_store.start_date.format(config.DATE_FORMAT) + '&billdate2=' + date_picker_store.end_date.format(config.SMALL_DATETIME_FORMAT); if (date_picker_store.start_date_cp && date_picker_store.end_date_cp) { url += '&billdateOld1=' + date_picker_store.start_date_cp.format(config.DATE_FORMAT) + '&billdateOld2=' + date_picker_store.end_date_cp.format(config.SMALL_DATETIME_FORMAT); } fetch(config.HT_HOST + url) .then((response) => response.json()) .then((json) => { runInAction(() => { this.credit_card_data.data_by_type = json; }); }) .catch((error) => { console.log('fetch data failed', error); }); } /** * 服务人数页面 ---- */ serviceModelMapper = { 'inbound': { url: config.HT_HOST + '/service-Analyse2/inbound_person_num', keySort: true, dynamicsX: false }, 'outbound': { url: config.HT_HOST + '/service-Analyse2/outbound_person_num', keySort: true, dynamicsX: false }, 'domestic': { url: config.HT_HOST + '/service-Analyse2/domestic_person_num', keySort: true, dynamicsX: false }, }; servicePersonNum = { curTab: 'inbound', loading: false, 'inbound': { loading: false, dataSource: [], rawData: [], NoEmptyData: [], }, 'outbound': { loading: false, dataSource: [], rawData: [], NoEmptyData: [], }, 'domestic': { loading: false, dataSource: [], rawData: [], NoEmptyData: [], }, }; setCurTab(v) { this.servicePersonNum.curTab = v; } resetPersonNumData = () => { this.servicePersonNum.inbound = { loading: false, dataSource: [], rawData: [], NoEmptyData: [], }; this.servicePersonNum.outbound = { loading: false, dataSource: [], rawData: [], NoEmptyData: [], }; this.servicePersonNum.domestic = { loading: false, dataSource: [], rawData: [], NoEmptyData: [], }; }; setPersonNumTableDataSource = (notnull) => { const mkey = this.servicePersonNum.curTab; if (comm.isEmpty(this.servicePersonNum[mkey].dataSource)) { return false; } this.servicePersonNum[mkey].dataSource = (notnull === false ? this.servicePersonNum[mkey].rawData : this.servicePersonNum[mkey].NoEmptyData); }; handleRows = (json) => { const mkey = this.servicePersonNum.curTab; const sumFun = (_data, key='sum') => ['orgz', 'orgzPDays', 'hosts', 'hostsPDays'].reduce((r, skey) => ({ ...r, [skey]: _data.reduce((a, c) => a + (c[skey] || 0), 0) }), { groupsKey: key, // `sumResult`, groupsLabel: '合计', }); const percentRow = (row, sumResult) => { return ['orgz', 'orgzPDays', 'hosts', 'hostsPDays'].reduce((r, skey) => ({ ...r, [`${skey}Percent`]: sumResult[skey] ? comm.fixTo4Decimals(row[skey] / sumResult[skey]) : 0 }), row); }; const sumResult = sumFun(json.result, 'sumResult'); const emptyRows = json.result.filter(ele => ele.groupsLabel === '' || ele.groupsLabel === '未知'); const sumEmptyResult = sumFun(emptyRows, 'sumEmptyResult'); const NotEmptyRows = json.result.filter(ele => ele.groupsLabel !== '' && ele.groupsLabel !== '未知'); const sumNotEmptyRows = sumFun(NotEmptyRows, 'sumNotEmptyRows'); const result = NotEmptyRows.map((row) => ({...row, ...percentRow(row, sumNotEmptyRows)})).sort(comm.sortBy('orgz')).reverse().map((row) => { const newData = ['orgz', 'orgzPDays', 'hosts', 'hostsPDays'].reduce( (r, skey) => ({ ...r, [skey]: row[`${skey}Percent`] ? row[skey] + comm.fixToInt(sumEmptyResult[skey] * row[`${skey}Percent`]) : row[skey] }), row ); return newData; }); const sumAfter = sumFun(result, 'sumAfter'); const diffSum = ['orgz', 'orgzPDays', 'hosts', 'hostsPDays'].reduce((r, skey) => ({ ...r, [skey]: sumResult[skey]-sumAfter[skey]}), {}); const result0 = ['orgz', 'orgzPDays', 'hosts', 'hostsPDays'].reduce((r, skey) => ({...r, [skey]: (result?.[0]?.[skey] || 0)+(diffSum?.[skey] || 0)}), {}); result[0] = { ...result[0], ...result0 }; const totalRow = Object.keys(json.resultTotal).length > 1 ? { ...json.resultTotal, groupsKey: `total${mkey}` } : (mkey === 'domestic' ? sumResult : { groupsKey: 'empty'}); const IndividualServiceRow = {orgz: json.resultTotal.IndividualService, groupsKey: `individualService${mkey}`, groupsLabel: '单项服务人数', }; return { sumResult, result, rawData: [].concat([IndividualServiceRow, totalRow], json.result.sort(comm.sortBy('orgz')).reverse()), NoEmptyData: [].concat([IndividualServiceRow, totalRow], result), }; }; /** * 获取服务人数 */ async getPersonNum(queryData) { const mkey = this.servicePersonNum.curTab; const url = this.serviceModelMapper[mkey].url; this.servicePersonNum.loading = true; this.servicePersonNum[this.servicePersonNum.curTab].loading = true; const json = await fetchJSON(url, {...queryData, DateType: 'startDate'}); const { rawData, NoEmptyData } = this.handleRows(json); if (json.errcode === 0) { runInAction(() => { this.servicePersonNum.loading = false; this.servicePersonNum[this.servicePersonNum.curTab].loading = false; this.servicePersonNum[this.servicePersonNum.curTab].dataSource = rawData; this.servicePersonNum[this.servicePersonNum.curTab].rawData = rawData; this.servicePersonNum[this.servicePersonNum.curTab].NoEmptyData = NoEmptyData; }); } return json; } /** * ---- end 服务人数页面 */ } export default FinancialStore;