import { Injectable, Injector, Optional } from '@angular/core'; import { NumberHelperService } from '@farris/ui-common/number'; import { DateTimeHelperService } from '@farris/ui-common/date'; import { ColumnFormatter, NumberFormatOptions, EnumFormatOptions, ImageFormatOptions, BooleanFormatOptions } from './column-format.options'; import { FilterCondition, Compare, FilterRelation, CompareOperators, SortCondition, SortType } from '@farris/ui-common/types'; /* * @Author: 疯狂秀才(Lucas Huang) * @Date: 2019-01-02 14:12:47 * @LastEditors: 疯狂秀才(Lucas Huang) * @LastEditTime: 2019-10-18 13:59:34 * @Company: Inspur * @Version: v0.0.1 */ @Injectable({ providedIn: 'root' }) export class ColumnFormatService { constructor(public datehelper: DateTimeHelperService, public numberhelper: NumberHelperService) { } format(value: any, data?: any, formatter?: any, context?: any) { if (formatter) { if (typeof(formatter) === 'function') { context = context || {}; return formatter(value, data, { date: this.datehelper, number: this.numberhelper, format: this, ...context }); } else { if (formatter['type']) { const fmt = formatter as ColumnFormatter; switch (fmt.type) { case 'datetime': if (fmt.options && fmt.options.format) { return this.dateTimeFormat(value, fmt.options); } return value; case 'number': return this.numberFormat(value, fmt.options); case 'enum': return this.enumFormat(value, fmt.options); case 'image': return this.imageFormat(value, fmt.options); case 'boolean': return this.booleanFormat(value, fmt.options); default: return value; } } } } return value; } private dateTimeFormat(value: any, opts: any) { if (value) { let fmt = 'yyyy-MM-dd'; if (typeof(opts) === 'string') { fmt = opts; } else if (typeof(opts) === 'object') { fmt = opts.format; } fmt = fmt.replace('YYYY', 'yyyy').replace('-DD', '-dd'); if (typeof(opts) === 'object' && opts.dateRange) { const splitStr = opts.dateRangeDatesDelimiter || '~'; let [beginDate, endDate ] = value.split(splitStr); beginDate = this.datehelper.formatTo(beginDate, fmt); endDate = this.datehelper.formatTo(endDate, fmt); return beginDate + splitStr + endDate; } return this.datehelper.formatTo(value, fmt); } return value; } private numberFormat(value: any, opts?: NumberFormatOptions) { return this.numberhelper.formatNumber(value, opts); // if (value !== undefined && value !== '' && value !== NaN) { // return this.numberhelper.formatMoney(value, opts); // } // return value; } private enumFormat(value: any, opts?: EnumFormatOptions) { if (value === undefined || value === null ) { value = ''; } if (opts && opts.data && opts.data.length) { const val = value.toString(); let arr = [val]; if (val.indexOf(',') > -1) { // 多选 arr = val.split(','); } const str = []; arr.forEach(v => { const n = opts.data.find(item => item[opts.valueField].toString() == v); if (n) { str.push(n[opts.textField]); } }); return str.join(','); } return value; } private imageFormat(value, opts?: ImageFormatOptions) { if (value) { if (opts) { const arrStr = [ `'); return arrStr.join(''); } else { return ``; } } return value; } private booleanFormat(value, opts?: BooleanFormatOptions) { if (value !== undefined) { if (opts) { const val = value ? opts.trueText : opts.falseText; if (val === null || val === undefined) { return value; } return val; } else { return value; } } return ''; } private convertCompare(val: number) { const op = CompareOperators.find( (item: any) => item.value === val); if (op) { return op.label; } } buildSortString(sorts: SortCondition[]): string { if (sorts && sorts.length) { let str = sorts.map(s => { return `${s.sortField} ${SortType[s.sortType].toString().toLowerCase()},`; }).join(' '); if (str) { str = str.substr(0, str.length - 1); } return str; } return ''; } buildSqlWhere(conditions: FilterCondition[]): string { if (conditions && conditions.length) { const result = []; const conditionList = conditions.filter(c => c.filterField !== ''); conditionList.forEach((condition, index) => { if (!condition.filterField) { return; } result.push(condition.lbracket); result.push(condition.filterField); const opCode = parseInt(condition.compare.toString(), 10); const op = this.convertCompare(opCode); result.push(' ' + op.replace(/\%/g, '').replace('...', '')); if (opCode === Compare.Like || opCode === Compare.NotLike || opCode === Compare.NotLikeEndWith || opCode === Compare.LikeEndWith) { result.push('\''); result.push('%'); } else { result.push(' '); result.push('\''); } if (opCode === Compare.In || opCode === Compare.NotIn) { result.push(condition.value.replace(/\r\n/g, ',')); } else { result.push(condition.value); } if (opCode === Compare.Like || opCode === Compare.NotLike || opCode === Compare.LikeStartWith || opCode === Compare.NotLikeStartWith) { result.push('%'); } result.push('\''); result.push(condition.rbracket); result.push(' '); if (index !== conditionList.length - 1) { result.push(condition.relation === FilterRelation.Empty ? '' : FilterRelation[condition.relation].toString().toLowerCase()); result.push(' '); } }); return result.join(''); } return ''; } }