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

import com.yuchen.saas.park.api.commons.PageWrapper;
import com.yuchen.saas.park.api.dto.door.DoorAccessControlConfigBindDTO;
import com.yuchen.saas.park.api.dto.door.DoorAccessControlConfigSettingDTO;
import com.yuchen.saas.park.api.dto.door.DoorAccessControlConfigUpdateDTO;
import com.yuchen.saas.park.api.entity.door.config.DoorAccessControlUserPermissions;
import com.yuchen.saas.park.service.controller.door.util.OrganizationIdExtractor;
import com.yuchen.saas.park.service.mapper.domain.DoorAccessControlBlacklistedCondition;
import com.yuchen.saas.park.service.mapper.domain.task.DoorAccessControlConfigIssueTaskCondition;
import com.yuchen.saas.park.service.mapper.door.config.DoorAccessControlUserPermissionsRepository;
import com.yuchen.saas.park.service.service.door.config.DoorAccessControlConfigService;
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.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/doorConfig")
@RequiredArgsConstructor
public class DoorAccessControlConfigController {
    private final DoorAccessControlConfigService doorAccessControlConfigService;

    private final DoorAccessControlUserPermissionsRepository doorAccessControlUserPermissionsRepository;


    /**
     * 黑名单分页查询
     * @param o
     * @return
     */
    @PostMapping("/pageBlacklisted")
    public R setPermissions(@Valid @RequestBody PageWrapper<DoorAccessControlBlacklistedCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlConfigService.pageBlacklisted(o));
    }

    /**
     * 黑名单批量设置
     * @param o
     * @return
     */
    @PostMapping("/batchSaveBlacklisted")
    public R batchSaveBlacklisted(@Valid @RequestBody @NotEmpty Set<@Positive Long> o) {
        doorAccessControlConfigService.batchSaveBlacklisted(o);
        return R.data(Boolean.TRUE);
    }

    /**
     * 黑名单批量删除
     * @param o
     * @return
     */
    @PostMapping("/batchDeleteBlacklistedByUserIds")
    public R batchDeleteBlacklistedByUserIds(@Valid @RequestBody @NotEmpty Set<@Positive Long> o) {
        doorAccessControlConfigService.batchDeleteBlacklistedByUserIds(o);
        return R.data(Boolean.TRUE);
    }

    /**
     * 权限配置:添加权限
     * @param o
     * @return
     */
    @PostMapping("/bind")
    public R batchDeleteBlacklistedByUserIds(@Valid @RequestBody DoorAccessControlConfigBindDTO o) {
        doorAccessControlConfigService.bind(o.getTarget(), o.getSetting());
        return R.data(Boolean.TRUE);
    }

    /**
     * 批量删除（计划模板）配置
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    public R batchDelete(@Valid @RequestBody @NotEmpty Set<@Positive Long> ids) {
        doorAccessControlConfigService.batchDelete(ids);
        return R.data(Boolean.TRUE);
    }

    /**
     * 批量修改（计划模板）配置
     * @param o
     * @return
     */
    @PostMapping("/batchUpdate")
    public R batchUpdate(@Valid @RequestBody DoorAccessControlConfigUpdateDTO o) {
        doorAccessControlConfigService.batchUpdate(o.getIds(), o.getSetting());
        return R.data(Boolean.TRUE);
    }

    /**
     * 批量修改（计划模板）配置ByList
     * @param o
     * @return
     */
    @PostMapping("/batchUpdateByList")
    public R batchUpdateByList(@Valid @RequestBody DoorAccessControlConfigSettingDTO o) {
        doorAccessControlConfigService.batchUpdateById(o);
        return R.data(Boolean.TRUE);
    }

    /**
     * 设置人员权限状态
     * @param permissionList
     * @return
     */
    @PostMapping("/batchSetUserState")
    public R batchSetUserState(@NotEmpty @RequestBody List< DoorAccessControlUserPermissions > permissionList) {
        doorAccessControlUserPermissionsRepository.batchSetUserState(permissionList);
        return R.success();
    }
}
