CaptionSetController.java 2.46 KB
package com.viontech.storage.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.viontech.storage.entity.Message;
import com.viontech.storage.model.CaptionSet;
import com.viontech.storage.model.PicConfig;
import com.viontech.storage.service.CaptionSetService;
import com.viontech.storage.service.PicConfigService;
import com.viontech.storage.vo.CaptionSetVo;
import org.h2.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/11/8
 */

@RestController
@RequestMapping("/captionSets")
public class CaptionSetController {

    @Resource
    private CaptionSetService captionSetService;
    @Resource
    private PicConfigService picConfigService;

    @GetMapping("/{id}")
    public Message<CaptionSet> getById(@PathVariable("id") Long id) {
        CaptionSet byId = captionSetService.getById(id);
        return Message.success(byId);
    }

    @GetMapping
    public Message<List<CaptionSet>> listAll(CaptionSetVo captionSetVo) {
        LambdaQueryChainWrapper<CaptionSet> query = captionSetService.lambdaQuery();
        if (!StringUtils.isNullOrEmpty(captionSetVo.getNameLike())) {
            query.like(CaptionSet::getName, captionSetVo.getNameLike());
        }
        List<CaptionSet> list = captionSetService.list(query.getWrapper());
        return Message.success(list);
    }

    @PostMapping
    public Message<CaptionSet> add(@RequestBody CaptionSet captionSet) {
        boolean save = captionSetService.save(captionSet);
        if (save) {
            return Message.success(captionSet);
        } else {
            return Message.error();
        }
    }

    @PutMapping("/{id}")
    public Message<CaptionSet> updateById(@PathVariable Long id, @RequestBody CaptionSet captionSet) {
        captionSet.setId(id);
        boolean b = captionSetService.updateById(captionSet);
        return Message.success(captionSet);
    }

    @DeleteMapping("/{id}")
    public Message<CaptionSet> deleteById(@PathVariable Long id) {
        List<PicConfig> list = picConfigService.list(picConfigService.lambdaQuery().eq(PicConfig::getCaptionSetId, id).getWrapper());
        if (CollectionUtil.isNotEmpty(list)) {
            return Message.error("字幕配置使用中");
        }

        boolean b = captionSetService.removeById(id);
        return Message.success();
    }

}