SearchQuery.ts 5.04 KB
import type {CarType} from "lib/src/domain/VehicleType";
import dayjs from "dayjs";

export class SearchQuery {

    cityCode: string | null;

    tag: string | null;

    carTypes: CarType[] | null;

    priceRange = [0, 0];

    vehicleClass: { name: string, url: string, activeUrl: string } [] = [];

    overallRating: { name: string, value: number } | null;

    /**
     * 变速箱类型
     */
    bxslxs: string[] = [];

    /**
     * 燃料形式(汽油/柴油等)
     */

    rlxs: string | null;

    ageRange = [0, 0];

    mileageRange = [0, 0];

    /**
     * 过户次数
     */
    transferCount: number | null;

    /**
     * 排量
     */
    displacementRange = [0, 0];

    /**
     * 排放标准(3-国3/4-国4/5-国5/6-国6)
     */
    emissionStandards: number [] = [];

    /**
     * 车辆性质
     */
    usageTypes: { name: string, value: number } [] = [];

    /**
     * 车身颜色
     */
    exteriorColor: { name: string, color: string }[] = [];

    seats: number[] = [];

    reset(): void {
        this.cityCode = null;

        this.tag = null;

        this.carTypes = null;

        this.priceRange = [0, 0];

        this.vehicleClass = null;

        this.overallRating = null;

        /**
         * 变速箱类型
         */
        this.bxslxs = [];

        /**
         * 燃料形式(汽油/柴油等)
         */

        this.rlxs = null;

        this.ageRange = [0, 0];

        this.mileageRange = [0, 0];

        /**
         * 过户次数
         */
        this.transferCount = null;

        /**
         * 排量
         */
        this.displacementRange = [0, 0];

        /**
         * 排放标准(3-国3/4-国4/5-国5/6-国6)
         */
        this.emissionStandards = [];

        /**
         * 车辆性质
         */
        this.usageTypes = [];

        /**
         * 车身颜色
         */
        this.exteriorColor = [];

        this.seats = [];
    }

    setQueryForm(queryForm: any): void {
        console.log(queryForm);
        queryForm.cityCode = this.cityCode;
        if (this.tag !== '不限') {
            queryForm.tags = this.tag;
        } else {
            queryForm.tags = null;
        }


        if (this.carTypes && this.carTypes.length > 0) {
            queryForm.carTypeId = this.carTypes.map(item => item.id);
        } else {
            queryForm.carTypeId = null;
        }

        if (this.priceRange && this.priceRange[1] > 0) {
            if (this.priceRange[1] < 50) {
                queryForm.startingBidPrice = [this.priceRange[0] * 10000, this.priceRange[1] * 10000];
            } else {
                queryForm.startingBidPrice = [this.priceRange[0] * 10000, 99999 * 10000];
            }
        } else {
            queryForm.startingBidPrice = null;
        }

        queryForm.vehicleClass = this.vehicleClass.map(item => item.name);
        queryForm.overallRating = this.overallRating ? this.overallRating.value : null;

        queryForm.bxslx = this.bxslxs;
        queryForm.rlxs = this.rlxs;

        // 计算车龄
        if (this.ageRange && this.ageRange[1] > 0) {
            const today = dayjs();
            const start = today.add(-this.ageRange[1], 'years').format('YYYY-MM-DD');
            const end = today.add(-this.ageRange[0], 'years').format('YYYY-MM-DD');
            queryForm.productionDate = [start, end];
        } else {
            queryForm.productionDate = null;
        }

        if (this.mileageRange && this.mileageRange[1] > 0) {
            if (this.mileageRange[1] < 30) {
                queryForm.mileage = [this.mileageRange[0] * 10000, this.mileageRange[1] * 10000];
            } else {
                queryForm.mileage = [this.mileageRange[0] * 10000, 9990000];
            }
        } else {
            queryForm.mileage = null;
        }
        queryForm.transferCount = this.transferCount;
        if (this.displacementRange && this.displacementRange[1] > 0) {
            if (this.displacementRange[1] < 5) {
                queryForm.displacement = this.displacementRange;
            } else {
                queryForm.displacement = [this.displacementRange[0], 999];
            }
        } else {
            queryForm.displacement = null;
        }
        queryForm.emissionStandard = this.emissionStandards;

        queryForm.usageType = this.usageTypes.map(item => item.value);
        queryForm.exteriorColor = this.exteriorColor.map(item => item.name);
        queryForm.seat = this.seats;

    }

    isNotEmpty(): boolean {
        return (this.cityCode || this.tag || (this.carTypes && this.carTypes.length > 0) || (this.priceRange && this.priceRange[1] > 0) || this.vehicleClass.length > 0
            || this.overallRating || this.bxslxs.length > 0 || this.rlxs || (this.ageRange && this.ageRange[1] > 0) || (this.mileageRange && this.mileageRange[1] > 0) || this.transferCount || this.transferCount == 0
            || (this.displacementRange && this.displacementRange[1] > 0)
            || this.emissionStandards.length > 0
            || this.usageTypes.length > 0 || this.exteriorColor.length > 0 || (this.seats.length > 0));
    }

}