/*
 * @Author: Maple
 * @Date: 2021-02-27 17:40:54
 * @LastEditors: Do not edit
 * @LastEditTime: 2021-03-23 20:58:18
 * @Description: 处理ST表格的单列排序
 */
import { STRequestOptions } from '@delon/abc/st';

/**
 * 处理ST表格单条件排序
 * @param reqOtions ST的Req
 * @param sortField 排序字段，默认为：sortType
 * @returns ST的Req
 */
export function processSingleSort(reqOtions: STRequestOptions, sortField: string = 'sort'): STRequestOptions {
  if (!reqOtions.body) {
    return reqOtions;
  }
  // 获取排序字段
  const body: any = {};
  for (const _key of Object.keys(reqOtions.body)) {
    if (typeof reqOtions.body[_key] === 'string') {
      const value: string = reqOtions.body[_key]?.trim();
      if (value && (value === 'ascend' || value === 'descend')) {
        body[sortField] = `${_key}.${value}`;
      } else {
        body[_key] = reqOtions.body[_key];
      }
    } else {
      body[_key] = reqOtions.body[_key];
    }
  }
  if (JSON.stringify(body) !== '{}') {
    reqOtions.body = body;
  }
  return reqOtions;
}
