import { Inject } from "@midwayjs/core";
import { CoolController, BaseController } from "@cool-midway/core";
import { RecycleAppointmentEntity } from "../../entity/recycleappointment";
import { RecycleAppointmentService } from "../../service/recycleappointment";
import { BaseSysDepartmentEntity } from "../../../base/entity/sys/department";
import { BasicdataSorterEntity } from "../../../basicdata/entity/sorter";
import { WithDeptFilterWhere } from '../../../base/middleware/with-dept-filter-where'


/**
 * 预约管理
 */
@CoolController({
    api: ["add", "delete", "update", "info", "list", "page"],
    entity: RecycleAppointmentEntity,
    service: RecycleAppointmentService,
    pageQueryOp: {
        keyWordLikeFields: ["a.appointmentNo", "a.contactName", "a.contactPhone"],
        fieldEq: ["a.status", "a.departmentId", "a.sorterId"],
        select: [
            "a.*",
            "b.name AS departmentName",
            "c.businessName AS sorterName"
        ],
        join: [
            {
                entity: BaseSysDepartmentEntity,
                alias: "b",
                condition: "a.departmentId = b.id",
                type: "leftJoin"
            },
            {
                entity: BasicdataSorterEntity,
                alias: "c",
                condition: "a.sorterId = c.businessId",
                type: "leftJoin"
            }
        ],
        where: async (ctx) => {
            const { startDate, endDate } = ctx.request.body;
            const conditions: any[][] = [];
            if (startDate && endDate) {
                conditions.push(["a.appointmentDate BETWEEN :startDate AND :endDate", { startDate, endDate }]);
            }
            conditions.push(...(await WithDeptFilterWhere(ctx, { alias: 'a', field: 'departmentId' })));
            return conditions;
        }
    }
})
export class AdminRecycleAppointmentController extends BaseController {
    @Inject()
    recycleAppointmentService: RecycleAppointmentService;
}