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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.configure.controller.admin.materialmanagement.vo.*;
import cn.iocoder.yudao.module.configure.convert.materialmanagement.MaterialManagementConvert;
import cn.iocoder.yudao.module.configure.dal.dataobject.materialmanagement.MaterialManagementDO;
import cn.iocoder.yudao.module.configure.service.materialmanagement.MaterialManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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-management")
@Validated
public class MaterialManagementController {

    @Resource
    private MaterialManagementService materialManagementService;

    @PostMapping("/create")
    @ApiOperation("创建素材管理")
    @PreAuthorize("@ss.hasPermission('configure:material-management:create')")
    public CommonResult<Boolean> createMaterialManagement(@Valid @RequestBody List<MaterialManagementCreateReqVO> createReqVO) {
        return success(materialManagementService.createMaterialManagement(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新素材管理")
    @PreAuthorize("@ss.hasPermission('configure:material-management:update')")
    public CommonResult<Boolean> updateMaterialManagement(@Valid @RequestBody MaterialManagementUpdateReqVO updateReqVO) {
        materialManagementService.updateMaterialManagement(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/batchGroup")
    @ApiOperation("批量移动素材管理分组")
    @PreAuthorize("@ss.hasPermission('configure:material-management:update')")
    public CommonResult<Boolean> updateMaterialManagementGroupBatch(@Valid @RequestBody MaterialManagementGroupUpdate materialManagementGroupUpdate) {
        materialManagementService.updateMaterialManagementGroupBatch(materialManagementGroupUpdate);
        return success(true);
    }

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

    @DeleteMapping("/delete/batch")
    @ApiOperation("批量删除素材管理")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('configure:material-management:delete')")
    public CommonResult<Boolean> deleteMaterialManagementBatch(@RequestParam("ids") List<Long> ids) {
        materialManagementService.deleteMaterialManagementBatch(ids);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得素材管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('configure:material-management:query')")
    public CommonResult<MaterialManagementRespVO> getMaterialManagement(@RequestParam("id") Long id) {
        MaterialManagementDO materialManagement = materialManagementService.getMaterialManagement(id);
        return success(MaterialManagementConvert.INSTANCE.convert(materialManagement));
    }

    @GetMapping("/list")
    @ApiOperation("获得素材管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('configure:material-management:query')")
    public CommonResult<List<MaterialManagementRespVO>> getMaterialManagementList(@RequestParam("ids") Collection<Long> ids) {
        List<MaterialManagementDO> list = materialManagementService.getMaterialManagementList(ids);
        return success(MaterialManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得素材管理分页")
    @PreAuthorize("@ss.hasPermission('configure:material-management:query')")
    public CommonResult<PageResult<MaterialManagementRespVO>> getMaterialManagementPage(@Valid MaterialManagementPageReqVO pageVO) {
        PageResult<MaterialManagementDO> pageResult = materialManagementService.getMaterialManagementPage(pageVO);
        return success(MaterialManagementConvert.INSTANCE.convertPage(pageResult));
    }

}
