import Zondy from './Zondy'
import { defaultValue, isNull } from '../util'
/**
* 统计信息结构
* @class OutStatistic
* @param {Object} options 构造参数
* @param {StatisticType} [options.statisticType = 无] 统计方法类型,必传
* @param {String} [options.onStatisticField = 无] 要统计的字段,仅支持统计一个字段,必传
* @param {String} [options.outStatisticFieldName = 无] 输出统计结果时,要统计的字段的别名
* @example
* //创建统计信息结构对象
* let OutStatistic = new Zondy.OutStatistic({
* //统计最大值
* statisticType: StatisticType.functionMax,
* //要统计的字段
* onStatisticField: 'GDP2011',
* //要统计的字段的别名
* outStatisticFieldName: 'anotherName'
* });
*/
class OutStatistic {
constructor(options) {
options = defaultValue(options, {})
/**
* 统计方法类型
* @member {String} OutStatistic.prototype.statisticType
* */
this.statisticType = defaultValue(options.statisticType, undefined)
/**
* 要统计的字段
* @member {String} OutStatistic.prototype.onStatisticField
* */
this.onStatisticField = defaultValue(options.onStatisticField, undefined)
/**
* 输出统计结果时,要统计的字段的别名
* @member {String} OutStatistic.prototype.outStatisticFieldName
* */
this.outStatisticFieldName = defaultValue(
options.outStatisticFieldName,
undefined
)
// 正确性校验
if (isNull(this.statisticType)) {
console.warn('statisticType不能为空!')
}
if (isNull(this.onStatisticField)) {
console.warn('onStatisticField不能为空!')
}
}
/**
* @function OutStatistic.prototype.toString
* @description 将对象转为字符串
* @return Sting 转化后的字符串
* @example
* OutStatistic.toString();
* //statisticType和onStatisticField存在
* //字符串为:'{"statisticType": "FUNCTION_SUM","onStatisticField": "GDP2011"'
* //statisticType、onStatisticField和outStatisticFieldName存在
* //字符串为:'{"statisticType": "FUNCTION_SUM","onStatisticField": "GDP2011", "outStatisticFieldName":"test"}'
*/
toString() {
let str = ''
// 确保statisticType和onStatisticField存在
if (!isNull(this.statisticType) && !isNull(this.onStatisticField)) {
// 字段别名存在
if (!isNull(this.outStatisticFieldName)) {
str += `{"statisticType":"${this.statisticType}","onStatisticField":"${this.onStatisticField}","outStatisticFieldName":"${this.outStatisticFieldName}"}`
} else {
str += `{"statisticType":"${this.statisticType}","onStatisticField":"${this.onStatisticField}"}`
}
}
return str
}
}
Zondy.OutStatistics = OutStatistic
export default OutStatistic