package com.njcn.rdms.module.system.controller.admin.permission;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.util.object.BeanUtils;
import com.njcn.rdms.module.system.controller.admin.permission.vo.userVisibilityConfig.UserVisibilityConfigRespVO;
import com.njcn.rdms.module.system.controller.admin.permission.vo.userVisibilityConfig.UserVisibilityConfigSaveReqVO;
import com.njcn.rdms.module.system.dal.dataobject.permission.UserVisibilityConfigDO;
import com.njcn.rdms.module.system.service.permission.UserVisibilityConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户数据可见性配置")
@RestController
@RequestMapping("/system/user-visibility-config")
@Validated
public class UserVisibilityConfigController {

    @Resource
    private UserVisibilityConfigService userVisibilityConfigService;

    @GetMapping("/get")
    @Operation(summary = "查询用户可见性配置")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('system:user-visibility-config:query')")
    public CommonResult<UserVisibilityConfigRespVO> getUserVisibilityConfig(@RequestParam("userId") Long userId) {
        UserVisibilityConfigDO config = userVisibilityConfigService.getByUserId(userId);
        return success(BeanUtils.toBean(config, UserVisibilityConfigRespVO.class));
    }

    @PostMapping("/save")
    @Operation(summary = "保存用户可见性配置（存在则更新，不存在则新增）")
    @PreAuthorize("@ss.hasPermission('system:user-visibility-config:save')")
    public CommonResult<Long> saveUserVisibilityConfig(@Valid @RequestBody UserVisibilityConfigSaveReqVO saveReqVO) {
        return success(userVisibilityConfigService.saveOrUpdate(saveReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户可见性配置")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('system:user-visibility-config:delete')")
    public CommonResult<Boolean> deleteUserVisibilityConfig(@RequestParam("userId") Long userId) {
        userVisibilityConfigService.deleteByUserId(userId);
        return success(true);
    }

}
