list-util.service.ts 3.4 KB
import customerDictConversionFilter from "../filtres/customer-dict-conversion.filter";
import mathService from "@/services/math.service";

class ListUtil {
  /**
   * @Description: 格式化金额
   * @author LiuBo
   * @date 2021/2/22
   * @time 16:27
   * @param value        金额数字
   * @param precision    保留小数位
   */
  doFormatMoney(value: any, precision?: number): string {
    precision = precision || 2; // 默认保留两位小数
    if (value != null && !isNaN(Number(value))) {
      if (precision != null && precision !== 0) {
        value = Number(value).toFixed(precision);
      }
    } else {
      return value;
    }
    let val = String(value).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    if (precision > 3) {
      const idx = val.lastIndexOf(".");
      val = val.substring(0, idx) + val.substring(idx).replace(/,/g, "");
    }
    return val;
  }
  /**
   * @Description: 格式化金额
   * @author LiuBo
   * @date 2021/2/22
   * @time 16:27
   * @param precision    保留小数位
   */
  customerFormatMoney(precision: number): any {
    return (row: any, column: any, val: any): string => {
      return listUtil.doFormatMoney(val, precision);
    };
  }
  /**
   * @Description:  列表中金额格式化方法
   * @author LiuBo
   * @date 2021/2/22
   * @time 16:25
   * @param row            列表行
   * @param column         列表列
   * @param cellValue      当前值
   * @param index          当前列索引
   * @param precision      保留小数位
   */
  formatMoney(row: any, column: any, cellValue: any, index: any, precision?: number): string {
    return listUtil.doFormatMoney(cellValue, precision);
  }
  /**
   * @Description: 转换数据字典
   * @author LiuBo
   * @date 2021/2/22
   * @time 21:43
   * @param dicName        字典名称
   */
  convertDic(dicName: string): any {
    return (row: any, column: any, val: any) => {
      if (!!val && val.indexOf(",") > 0) {
        const dictValues = val.split(",");
        const dictions: string[] = [];
        dictValues.forEach((item: any) => {
          dictions.push(customerDictConversionFilter(item, dicName));
        });
        // return dictions.join(',') || "字典项为空"; // 去掉‘字典项为空’的描述信息
        return dictions.join(",") || "";
      } else {
        // return customerDictConversionFilter(val, dicName) || "字典项为空"; // 此处调用公共方法进行字典转译 // 去掉‘字典项为空’的描述信息
        return customerDictConversionFilter(val, dicName) || ""; // 此处调用公共方法进行字典转译
      }
    };
  }
  /**
   * @Description: 小数转换百分数
   * @author chenminjie
   * @date 2021/4/13
   * @time 10:37
   */
  formatPercent(row: any, column: any, cellValue: any): any {
    if (cellValue != null && !isNaN(Number(cellValue)) && cellValue != "") {
      return mathService.evaluate(`${cellValue}*${100}`, 4) + "%";
    } else {
      return "";
    }
  }

  /**
   * @Description: 小数转换百分数保留十四位小数
   * @author wangyixin
   * @date 2021/6/16
   * @time 9:02
   */
  formatPercentEight(row: any, column: any, cellValue: any): any {
    if (cellValue != null && !isNaN(Number(cellValue)) && cellValue != "") {
      return mathService.evaluate(`${cellValue}*${100}`, 14) + "%";
    } else {
      return "";
    }
  }
}
const listUtil = new ListUtil();
export default listUtil;