package com.yuchen.saas.park.service.controller.door;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.commons.PageWrapper;
import com.yuchen.saas.park.service.controller.door.util.OrganizationIdExtractor;
import com.yuchen.saas.park.service.mapper.domain.config.details.DoorAccessControlCondition;
import com.yuchen.saas.park.service.mapper.domain.config.details.DoorAccessControlConfigDetailsCondition;
import com.yuchen.saas.park.service.mapper.domain.config.details.UserDoorAccessControlCondition;
import com.yuchen.saas.park.service.mapper.domain.config.mapping.*;
import com.yuchen.saas.park.service.mapper.domain.task.DoorAccessControlConfigIssueTaskCondition;
import com.yuchen.saas.park.service.service.adapter.AdapterComponent;
import com.yuchen.saas.park.service.service.door.config.DoorAccessControlConfigIssueTaskScheduleService;
import com.yuchen.saas.park.service.service.door.config.DoorAccessControlQueryService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/door/query")
@RequiredArgsConstructor
public class DoorAccessControlConfigQueryController {
    private final AdapterComponent adapterComponent;
    private final DoorAccessControlQueryService doorAccessControlQueryService;

    @Resource
    private DoorAccessControlConfigIssueTaskScheduleService deviceTaskScheduleService;;


    @PostMapping("/test")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取所有监控点信息及其视频流url", notes = "获取所有监控点信息及其视频流url")
    public void test() {
        deviceTaskScheduleService.scheduleTrigger();
        deviceTaskScheduleService.scheduleTask();
    }

    /**
     * 下发配置明细分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageConfigIssueTaskByCondition")
    public R pageConfigIssueTaskByCondition(@Valid @RequestBody PageWrapper<DoorAccessControlConfigIssueTaskCondition> o) {
        return R.data(doorAccessControlQueryService.pageConfigIssueTaskByCondition(o));
    }

    // -----------------------

    /**
     * 门禁基础信息查询
     */
    @PostMapping("/pageDoorAccessControl")
    public R pageDoorAccessControl(@Valid @RequestBody PageWrapper<DoorAccessControlCondition> o) {
        return R.data(adapterComponent.pageDoorAccessControl(o));
    }

    /**
     * 人员权限明细
     */
    @PostMapping("/pageForDoorAccessControlConfigDetails")
    public R pageForDoorAccessControlConfigDetails(@Valid @RequestBody PageWrapper<DoorAccessControlConfigDetailsCondition> o) {
        return R.data(doorAccessControlQueryService.pageForDoorAccessControlConfigDetails(o));
    }

    /**
     * 人员权限首页
     */
    @PostMapping("/pageUserDoorAccessControl")
    public R pageUserDoorAccessControl(@Valid @RequestBody PageWrapper<UserDoorAccessControlCondition> o) {
        return R.data(doorAccessControlQueryService.pageUserDoorAccessControl(o));
    }

    // -----------------------

    /**
     * 部门-门禁分组分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageForDepartmentGroup")
    public R pageForDepartmentGroup(@Valid @RequestBody PageWrapper<DoorAccessControlConfigMappingDepartmentGroupCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlQueryService.pageForDepartmentGroup(o));
    }

    /**
     * 部门-门禁分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageForDepartmentPersonal")
    public R pageForDepartmentPersonal(@Valid @RequestBody PageWrapper<DoorAccessControlConfigMappingDepartmentPersonalCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlQueryService.pageForDepartmentPersonal(o));
    }

    /**
     * 用户分组-门禁分组分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageForGroupGroup")
    public R pageForGroupGroup(@Valid @RequestBody PageWrapper<DoorAccessControlConfigMappingGroupGroupCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlQueryService.pageForGroupGroup(o));
    }

    /**
     * 用户分组-门禁分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageForGroupPersonal")
    public R pageForGroupPersonal(@Valid @RequestBody PageWrapper<DoorAccessControlConfigMappingGroupPersonalCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlQueryService.pageForGroupPersonal(o));
    }

    /**
     * 个人-门禁分组分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageForPersonalGroup")
    public R pageForPersonalGroup(@Valid @RequestBody PageWrapper<DoorAccessControlConfigMappingPersonalGroupCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlQueryService.pageForPersonalGroup(o));
    }

    /**
     * 门禁点权限明细分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageForPersonalPersonal")
    public R pageForPersonalPersonal(@Valid @RequestBody PageWrapper<DoorAccessControlConfigMappingPersonalPersonalCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlQueryService.pageForPersonalPersonal(o));
    }
}
