MaterialController.java 4.41 KB
package com.bs.reception.controller.materials;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.common.core.domain.AjaxResult;
import com.bs.reception.entity.materials.Material;
import com.bs.reception.model.materials.MaterialPageQuery;
import com.bs.reception.model.materials.MaterialPageVO;
import com.bs.reception.service.IMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
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.RestController;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "素材管理", description = "素材管理接口")
@RequestMapping("/materials/materials")
public class MaterialController {

    @Resource
    private IMaterialService materialService;

    @PostMapping("/list")
    @Operation(summary = "素材列表")
    public AjaxResult list(@RequestBody Material body) {
        try {
            List<Material> list = materialService.getMaterialList(body);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/create")
    @Operation(summary = "创建素材")
    public AjaxResult create(@RequestBody Material body) {
        try {
            Material data = materialService.createMaterial(body);
            return AjaxResult.success(data);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/update")
    @Operation(summary = "更新素材")
    public AjaxResult update(@RequestBody Material body) {
        try {
            boolean ok = materialService.updateMaterial(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+ e.getMessage());
        }
    }

    @PostMapping("/sort")
    @Operation(summary = "设置排序")
    public AjaxResult setSort(@RequestBody Material body) {
        try {
            boolean ok = materialService.setMaterialSort(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/homepage")
    @Operation(summary = "设置首页展示")
    public AjaxResult setHomepage(@RequestBody Material body) {
        try {
            boolean ok = materialService.setMaterialHomepage(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/page")
    @Operation(summary = "素材分页查询")
    public AjaxResult page(@RequestBody MaterialPageQuery query) {
        try {
            Page<MaterialPageVO> data = materialService.pageMaterials(query);
            return AjaxResult.success(data);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/delete")
    @Operation(summary = "删除素材")
    public AjaxResult delete(@RequestBody Material body) {
        try {
            boolean ok = materialService.deleteMaterial(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }

    @PostMapping("/detail")
    @Operation(summary = "素材详情")
    public AjaxResult detail(@RequestBody Material body) {
        try {
            Material ok = materialService.getMaterialDetail(body);
            return AjaxResult.success(ok);
        } catch (Exception e) {
            log.error("操作失败:{}", e.getMessage(), e);
            return AjaxResult.error("操作失败:"+e.getMessage());
        }
    }
}