package cn.iocoder.yudao.module.label.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.label.entity.Label;
import cn.iocoder.yudao.module.label.service.LabelService;
import cn.iocoder.yudao.module.label.vo.LabelPageReqVO;
import cn.iocoder.yudao.module.label.vo.LabelRespVO;
import cn.iocoder.yudao.module.label.vo.LabelSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 标签库")
@RestController
@RequestMapping("/base/label")
@Validated
public class LabelController {

    @Resource
    private LabelService labelService;

    /**
     * 新建标签
     *
     * @param createReqVO 标签扩展类
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.String>
     * @author PQZ
     * @date 14:18 2024/8/4
     **/
    @PostMapping("/create")
    @Operation(summary = "创建标签库")
    @PreAuthorize("@ss.hasPermission('base:label:create')")
    public CommonResult<Boolean> createLabel(@Valid @RequestBody LabelSaveReqVO createReqVO) {
        labelService.saveLabel(createReqVO);
        return success(true);
    }

    /**
     * 更新标签库
     *
     * @param updateReqVO 标签扩展类
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.String>
     * @author PQZ
     * @date 14:18 2024/8/4
     **/
    @PutMapping("/update")
    @Operation(summary = "更新标签库")
    @PreAuthorize("@ss.hasPermission('base:label:update')")
    public CommonResult<Boolean> updateLabel(@Valid @RequestBody LabelSaveReqVO updateReqVO) {
        labelService.saveLabel(updateReqVO);
        return success(true);
    }

    /**
     * 删除标签
     *
     * @param id 标签id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 14:33 2024/8/4
     **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:label:delete')")
    public CommonResult<Boolean> deleteLabel(@RequestParam("id") String id) {
        labelService.deleteLabel(id);
        return success(true);
    }

    /**
     * 通过id获取标签
     *
     * @param id 标签id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.label.vo.LabelRespVO>
     * @author PQZ
     * @date 14:33 2024/8/4
     **/
    @GetMapping("/get")
    @Operation(summary = "获得标签库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:label:query')")
    public CommonResult<LabelRespVO> getLabel(@RequestParam("id") String id) {
        Label label = labelService.getLabel(id);
        return success(BeanUtils.toBean(label, LabelRespVO.class));
    }

    /**
     * 标签库分页查询
     *
     * @param pageReqVO LabelPageReqVO实体
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 14:37 2024/8/4
     **/
    @GetMapping("/page")
    @Operation(summary = "获得标签库分页")
    @PreAuthorize("@ss.hasPermission('base:label:query')")
    public CommonResult<IPage<?>> getLabelPage(LabelPageReqVO pageReqVO,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<LabelRespVO> page = new Page<>(pageNo, pageSize);
        return success(labelService.queryListPage(pageReqVO, page));
    }


}