123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- class DataHandler {
- constructor() {
- this[CONSTANTS.KEY_MCH_SERVICE_RATE] = [];
- this.structureMapping = {
- // mch_service_rate: 机构id(mchid), 日期(day), 服务费比率(service_rate)
- [CONSTANTS.KEY_MCH_SERVICE_RATE]: ['mchid', 'day', 'service_rate'],
- };
- }
- translateToIndex(structure, data) {
- return this.structureMapping[structure].map((key, index) => data[key]);
- }
- translateToField(structure, data) {
- let translatedData = {};
- this.structureMapping[structure].forEach((key, index) => {
- translatedData[key] = data[index];
- });
- return translatedData;
- }
- getServiceRateList() {
- const list = [], that = this;
- this[CONSTANTS.KEY_MCH_SERVICE_RATE].forEach(v => {
- list.push(that.translateToField(CONSTANTS.KEY_MCH_SERVICE_RATE, v));
- });
- return list;
- }
- async addServiceRate(structure, newData, backend = true) {
- const list = this[structure];
- const translatedData = this.translateToIndex(structure, newData);
- const mchid = translatedData[0];
- const day = translatedData[1];
- let mchidData = list.filter(item => item[0] === mchid);
- const index = mchidData.findIndex(item => item[1] === day);
- if (index !== -1) {
- mchidData[index] = translatedData;
- } else {
- mchidData.push(translatedData);
- }
- if (mchidData.length > 90) {
- mchidData.shift();
- }
- this[structure] = list.filter(item => item[0] !== mchid);
- this[structure] = [...this[structure], ...mchidData];
- if (backend === true) {
- await this.saveDataToBackend(structure);
- }
- }
- async removeData(structure, mchid, day) {
- let list = this[structure];
- const index = list.findIndex(item => item[0] === mchid && item[1] === day);
- if (index !== -1) {
- list.splice(index, 1);
- }
- }
- async saveDataToBackend(structure) {
- this.checkAndFixData(structure);
- const compressedData = this.compressData(structure);
- await statsApi.setStatsSettings(structure, compressedData);
- }
- checkAndFixData(structure) {
- let list = this[structure];
- let structureLength = this.structureMapping[structure].length;
- list.forEach(item => {
- if (item.length < structureLength) {
- for (let i = item.length; i < structureLength; i++) {
- item.push('');
- }
- }
- });
- }
- compressData(structure) {
- return this[structure];
- }
- decompressData(structure, data) {
- this[structure] = data[structure] || [];
- this.checkAndFixData(structure);
- }
- async loadServiceRate() {
- const data = await statsApi.getStatsSettings([CONSTANTS.KEY_MCH_SERVICE_RATE]);
- if (data.state !== true) {
- showErr('网络异常,请刷新页面重试');
- return;
- }
- this.decompressData(CONSTANTS.KEY_MCH_SERVICE_RATE, data);
- }
- }
- class ServicefeeApiService extends BaseService {
- getStatsSettings(keys = []) {
- const endpoint = '?act=refill_service_stats&op=stats_settings&key=' + keys.join(',');
- return this.request(endpoint, 'GET', {});
- };
- setStatsSettings(key, value) {
- const endpoint = '?act=refill_service_stats&op=stats_settings_save';
- let data = {
- key: key,
- value: value
- };
- return this.request(endpoint, 'POST', data);
- };
- }
- const statsApi = new ServicefeeApiService('');
- const dataHelper = new DataHandler();
|