package cn.iocoder.yudao.module.configure.controller.admin.materialgroup;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.configure.controller.admin.materialgroup.vo.MaterialGroupCreateReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.materialgroup.vo.MaterialGroupPageReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.materialgroup.vo.MaterialGroupRespVO;
import cn.iocoder.yudao.module.configure.controller.admin.materialgroup.vo.MaterialGroupUpdateReqVO;
import cn.iocoder.yudao.module.configure.convert.materialgroup.MaterialGroupConvert;
import cn.iocoder.yudao.module.configure.dal.dataobject.materialgroup.MaterialGroupDO;
import cn.iocoder.yudao.module.configure.service.materialgroup.MaterialGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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 java.util.Collection;
import java.util.List;

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

@Api(tags = "管理后台 - 素材管理分组")
@RestController
@RequestMapping("/configure/material-group")
@Validated
public class MaterialGroupController {

    @Resource
    private MaterialGroupService materialGroupService;

    @PostMapping("/create")
    @ApiOperation("创建素材管理分组")
    @PreAuthorize("@ss.hasPermission('configure:material-group:create')")
    public CommonResult<Long> createMaterialGroup(@Valid @RequestBody MaterialGroupCreateReqVO createReqVO) {
        return success(materialGroupService.createMaterialGroup(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新素材管理分组")
    @PreAuthorize("@ss.hasPermission('configure:material-group:update')")
    public CommonResult<Boolean> updateMaterialGroup(@Valid @RequestBody MaterialGroupUpdateReqVO updateReqVO) {
        materialGroupService.updateMaterialGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除素材管理分组")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('configure:material-group:delete')")
    public CommonResult<Boolean> deleteMaterialGroup(@RequestParam("id") Long id) {
        materialGroupService.deleteMaterialGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得素材管理分组")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('configure:material-group:query')")
    public CommonResult<MaterialGroupRespVO> getMaterialGroup(@RequestParam("id") Long id) {
        MaterialGroupDO materialGroup = materialGroupService.getMaterialGroup(id);
        return success(MaterialGroupConvert.INSTANCE.convert(materialGroup));
    }

    @GetMapping("/list/ids")
    @ApiOperation("获得素材管理分组列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('configure:material-group:query')")
    public CommonResult<List<MaterialGroupRespVO>> getMaterialGroupListByIds(@RequestParam("ids") Collection<Long> ids) {
        List<MaterialGroupDO> list = materialGroupService.getMaterialGroupListByids(ids);
        return success(MaterialGroupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得素材管理分组分页")
    @PreAuthorize("@ss.hasPermission('configure:material-group:query')")
    public CommonResult<PageResult<MaterialGroupRespVO>> getMaterialGroupPage(@Valid MaterialGroupPageReqVO pageVO) {
        PageResult<MaterialGroupDO> pageResult = materialGroupService.getMaterialGroupPage(pageVO);
        return success(MaterialGroupConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list")
    @ApiOperation("获得素材管理分组列表")
    @PreAuthorize("@ss.hasPermission('configure:material-group:query')")
    public CommonResult<List<MaterialGroupRespVO>> getMaterialGroupList(@Valid MaterialGroupPageReqVO pageVO) {
        List<MaterialGroupDO> list = materialGroupService.getMaterialGroupList(pageVO);
        return success(MaterialGroupConvert.INSTANCE.convertList(list));
    }

}
