import { SelectQueryBuilder } from "typeorm";
import CommonHelper from "../helpers/common";
import { OrmHelper } from "../helpers/orm";
import { PharmacyInfo } from "entity";

export class PharmacyInfoModel {
    static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
        const repo = OrmHelper.DB.getRepository(PharmacyInfo);
        let whereAttr: string[] = [];
        let whereVal: any = {};
        if (filter && Object.keys(filter).length > 0) {
            whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter, "pharmacy_info").whereAttr];
            whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter, "pharmacy_info").whereVal };
        }

        var query = repo.createQueryBuilder("pharmacy_info")
        .leftJoinAndSelect("pharmacy_info.default_room", "default_room")
        .leftJoinAndSelect("pharmacy_info.munisipiu", "munisipiu")
        .leftJoinAndSelect("pharmacy_info.postu_admin", "postu_admin")
        .leftJoinAndSelect("pharmacy_info.suco", "suco")
        .leftJoinAndSelect("pharmacy_info.aldeia", "aldeia")
        // .leftJoinAndSelect("pharmacy_info.province", "province")
        // .leftJoinAndSelect("pharmacy_info.city", "city")
        // .leftJoinAndSelect("pharmacy_info.subdistrict", "subdistrict")
        // .leftJoinAndSelect("pharmacy_info.ward", "ward");
        if (whereAttr.length != 0) {
            query = query.where(whereAttr.join(" and "), whereVal);
        }

        return query;
    }
}