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

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.user.vo.signature.UserSignatureRespVO;
import com.njcn.rdms.module.system.controller.admin.user.vo.signature.UserSignatureSaveReqVO;
import com.njcn.rdms.module.system.dal.dataobject.user.UserSignatureDO;
import com.njcn.rdms.module.system.service.user.UserSignatureService;
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;
import static com.njcn.rdms.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 用户电子签名")
@RestController
@RequestMapping("/system/user-signatures")
@Validated
public class UserSignatureController {

    @Resource
    private UserSignatureService userSignatureService;

    @GetMapping("/get")
    @Operation(summary = "获取用户电子签名")
    @Parameter(name = "userId", description = "用户 ID", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-signature:query')")
    public CommonResult<UserSignatureRespVO> get(@RequestParam("userId") Long userId) {
        return success(toRespVO(userSignatureService.getByUserId(userId)));
    }

    @PostMapping("/save")
    @Operation(summary = "保存用户电子签名")
    @PreAuthorize("@ss.hasPermission('system:user-signature:update')")
    public CommonResult<Boolean> save(@Valid @RequestBody UserSignatureSaveReqVO reqVO) {
        userSignatureService.save(reqVO.getUserId(), reqVO.getFileId(), reqVO.getFileName(), reqVO.getStatus(), reqVO.getRemark());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户电子签名")
    @Parameter(name = "userId", description = "用户 ID", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-signature:update')")
    public CommonResult<Boolean> delete(@RequestParam("userId") Long userId) {
        userSignatureService.deleteByUserId(userId);
        return success(true);
    }

    @GetMapping("/my")
    @Operation(summary = "获取当前登录用户电子签名")
    public CommonResult<UserSignatureRespVO> my() {
        return success(toRespVO(userSignatureService.getByUserId(getLoginUserId())));
    }

    private UserSignatureRespVO toRespVO(UserSignatureDO signature) {
        return BeanUtils.toBean(signature, UserSignatureRespVO.class);
    }
}
