/**
 * 数据集操作工具类
 * @author cls
 * @date 2021-03-12
 */
import axios from "@birt/funclib/AxiosUtil";
import { $CONST } from "@birt/funclib/ConstUtil"

export const StoreUtil = {
  /**
   * showdoc
   * @catalog JS工具类/StoreUtil
   * @title loadMethodEap5
   * @description eap5 数据集加载远程数据方法
   * @method static
   * @url import { StoreUtil } from '@birt/funclib/StoreUtil.js'
   * @param store 必选 当前数据集实例
   * @param cdion 必选 查询条件
   * @param option.url 必选 数据请求路径
   * @param resolveLoadData 必选 查询结果回调
   * @return {status:200,dataPack:{rows:[]}} 标准数据包返回
   */
  loadMethodEap5(store, cdion, option, resolveLoadData) {
    let param = {};
    let __body = {};
    // 附加数据集模型路径 
    param[$CONST.MODELFILEPATH] = option[$CONST.MODELFILEPATH];

    // 附加分页条件
    if (store._isPagging()) {
      param[$CONST.PAGESIZE] = store.getPageSize();
      param[$CONST.PAGENUM] = store.getPageNum();
      if (option[$CONST.RETURNCOUNT] == undefined) {
        param[$CONST.RETURNCOUNT] = option.loadCdionChange && store.getReturnCount();
      } else {
        param[$CONST.RETURNCOUNT] = option[$CONST.RETURNCOUNT] && (param[$CONST.PAGENUM] == 1);
      }
    }
    // 附加查询条件
    if (cdion) {
      __body[$CONST.QUERYCDIONS] = cdion;
    }
    // 附加排序
    let orderBy = store.getSort();
    if (orderBy.length > 0) {
      __body[$CONST.ORDERBY] = orderBy;
    }
    // 附加__body参数包
    param[$CONST.__BODY] = JSON.stringify(__body);

    // 执行查询
    axios.post(option.url, param)
      .then(response => {
        if (response[$CONST.OUTPARAMETER]) {
          option.recordCount = response[$CONST.OUTPARAMETER][$CONST.TOTALRECORDCOUNT];
        }
        resolveLoadData.call(store, response[$CONST.DATAPACK][$CONST.ROWS], option);
      })
      .catch(error => {
        console.log(error);
      });
  },
  // 获取单个数据集保存包
  _getSaveDataItem(store) {
    let mpath = store.getModelFilePath();
    let data = store.getDirtyData();
    if (mpath && data.length > 0) {
      return { modelFilePath: mpath, data: data };
    }
  },
  // 获取保存数据包
  getSaveData(store) {
    let saveData = [];
    if (Array.isArray(store)) {
      store.forEach(ds => {
        let saveItem = this._getSaveDataItem(ds);
        if (saveItem) {
          saveData.push(saveItem);
        }
      })
    } else {
      let saveItem = this._getSaveDataItem(store);
      if (saveItem) {
        saveData.push(saveItem);
      }
    }
    return saveData;
  },
  /**
    * showdoc
    * @catalog JS工具类/StoreUtil
    * @title saveDataEap5
    * @description eap5 数据集保存数据
    * @method static
    * @url import { StoreUtil } from '@birt/funclib/StoreUtil.js'
    * @param store 必选 当前数据集实例,或数据集组
    * @param option.url 必选 数据请求路径
    * @param saveSucc 可选 保存成功回调
    */
  saveDataEap5(store, option, saveSucc) {
    let saveData = this.getSaveData(store);
    axios.post(option.url, { __body: JSON.stringify(saveData), __version: "1.0.0" }).then((res) => {
      // 保存成功回调
      saveSucc && saveSucc.call(store, saveData, res);
    });
  }
};