StorageConfigController.java 4.47 KB
package com.viontech.storage.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.viontech.storage.entity.Context;
import com.viontech.storage.entity.Generator;
import com.viontech.storage.entity.Message;
import com.viontech.storage.model.StorageConfig;
import com.viontech.storage.service.StorageConfigService;
import com.viontech.storage.vo.StorageConfigVo;
import org.h2.util.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

@RestController
@RequestMapping("/storageConfigs")
public class StorageConfigController {

    @Resource
    private StorageConfigService storageConfigService;

    @GetMapping("/{id}")
    public Message<StorageConfig> getById(@PathVariable("id") Long id) {
        StorageConfig byId = storageConfigService.getById(id);
        StorageConfigVo copy = StorageConfigVo.copy(byId);
        return Message.success(copy);
    }

    @GetMapping
    public Message<List<StorageConfig>> listAll(StorageConfigVo vo, @RequestParam(required = false, defaultValue = "true") Boolean withoutConfig) {
        QueryChainWrapper<StorageConfig> query = storageConfigService.query();
        if (!StringUtils.isNullOrEmpty(vo.getNameLike())) {
            query.like("name", vo.getNameLike());
        }
        if (vo.getType() != null) {
            query.eq("type", vo.getType());
        }

        List<StorageConfig> list = storageConfigService.list(query.getWrapper());
        Stream<StorageConfigVo> stream = list.stream().map(StorageConfigVo::copy);
        if (withoutConfig) {
            stream = stream.peek(x -> x.setConfig(null));
        }
        List<StorageConfig> collect = stream.collect(Collectors.toList());
        return Message.success(collect);
    }

    @PostMapping
    public Message<StorageConfig> add(@RequestBody StorageConfigVo storageConfigVo) {
        List<Context> contexts = storageConfigVo.getContexts();
        if (CollectionUtil.isNotEmpty(contexts)) {
            storageConfigVo.setContext(JSONUtil.toJsonStr(contexts));
        }
        boolean save = storageConfigService.save(storageConfigVo);
        return Message.success(storageConfigVo);
    }

    @PutMapping("/{id}")
    public Message<StorageConfig> updateById(@PathVariable Long id, @RequestBody StorageConfigVo storageConfigVo) {
        List<Context> contexts = storageConfigVo.getContexts();
        if (CollectionUtil.isNotEmpty(contexts)) {
            storageConfigVo.setContext(JSONUtil.toJsonStr(contexts));
        }
        storageConfigVo.setId(id);
        boolean b = storageConfigService.updateById(storageConfigVo);
        return Message.success(storageConfigVo);
    }

    @DeleteMapping("/{id}")
    public Message<StorageConfig> deleteById(@PathVariable Long id) {
        boolean b = storageConfigService.removeById(id);
        return Message.success();
    }


    @GetMapping("/build/{id}")
    public Message<String> buildConfig(@PathVariable Long id) {
        String build = new Generator().create(id).build();
        return Message.success(build);
    }

    @PostMapping("/upload")
    public Message<Object> upload(String name, MultipartFile file) throws IOException {
        FastByteArrayOutputStream read = IoUtil.read(file.getInputStream());
        StorageConfig storageConfig = new StorageConfig().setName(name).setConfig(read.toByteArray()).setType(1);
        storageConfigService.save(storageConfig);
        return Message.success();
    }

    @GetMapping("/download")
    public void download(Long id, HttpServletResponse response) throws IOException {
        StorageConfig byId = storageConfigService.getById(id);
        byte[] bytes = byId.getConfig();
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(byId.getName() + ".xml", "utf-8"));
        response.setCharacterEncoding("GBK");
        IoUtil.write(response.getOutputStream(), false, bytes);
    }

}