package com.backendsys.modules.material.controller;

import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.material.entity.Material;
import com.backendsys.modules.material.service.MaterialService;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "素材管理")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @Anonymous
    @Operation(summary = "获取素材列表")
//    @PreAuthorize("@sr.hasPermission('20.1')")
    @GetMapping("/api/material/getMaterialList")
    public Result getMaterialList(Material material) {
        return Result.success().put("data", materialService.selectMaterialList(material));
    }

    @Operation(summary = "获取素材详情")
    @PreAuthorize("@sr.hasPermission('20.1.1')")
    @GetMapping("/api/material/getMaterialDetail")
    public Result getMaterialDetail(@Validated(Material.Detail.class) Material material) {
        return Result.success().put("data", materialService.selectMaterialDetail(material));
    }

    @Operation(summary = "创建素材")
    @PreAuthorize("@sr.hasPermission('20.1.2')")
    @PostMapping("/api/material/createMaterial")
    public Result createMaterial(@Validated(Material.Create.class) @RequestBody Material material) {
        material.setUser_id(SecurityUtil.getUserId());
        return Result.success().put("data", materialService.insertMaterial(material));
    }

    @SysLog("编辑素材")
    @Operation(summary = "编辑素材")
    @PreAuthorize("@sr.hasPermission('20.1.3')")
    @PutMapping("/api/material/updateMaterial")
    public Result updateMaterial(@Validated(Material.Update.class) @RequestBody Material material) {
        material.setUser_id(SecurityUtil.getUserId());
        return Result.success().put("data", materialService.updateMaterial(material));
    }

    @SysLog("删除素材")
    @Operation(summary = "删除素材")
    @PreAuthorize("@sr.hasPermission('20.1.4')")
    @DeleteMapping("/api/material/deleteMaterial")
    public Result deleteMaterial(@Validated(Material.Delete.class) @RequestBody Material material) {
        return Result.success().put("data", materialService.deleteMaterial(material));
    }

}
