jinlin
2024-03-13 91c67198a84e9c0a76cc0154fbebf62813c1de26
modules/mainPart/src/main/java/com/zt/life/modules/sysPictureBase/controller/SysPictureBaseController.java
@@ -1,6 +1,7 @@
package com.zt.life.modules.sysPictureBase.controller;
import com.spire.xls.Workbook;
import com.zt.common.annotation.LogOperation;
import com.zt.common.constant.Constant;
import com.zt.common.annotation.QueryParam;
@@ -16,13 +17,26 @@
import com.zt.life.modules.sysPictureBase.service.SysPictureBaseService;
import com.zt.life.sys.dto.OssDto;
import com.zt.life.sys.service.SysOssConfigService;
import com.zt.modules.oss.enums.CloudChannel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
/**
@@ -32,7 +46,7 @@
 * @since 1.0.0 2024-02-27
 */
@RestController
@RequestMapping("/sysPictureBase/SysPictureBase/")
@RequestMapping("/sysPictureBase/")
@Api(tags = "sys_picture_base")
public class SysPictureBaseController {
    @Autowired
@@ -40,6 +54,8 @@
    @Autowired
    private SysOssConfigService sysOssConfigService;
    @Value("${zt.oss.local-path}")
    private String localPath;
    @GetMapping("page")
    @ApiOperation("分页")
@@ -58,38 +74,92 @@
    @ApiOperation("信息")
    public Result<SysPictureBase> get(@PathVariable("id") Long id) {
        SysPictureBase data = sysPictureBaseService.get(id);
        if (id != null) {
            OssDto ossDto = sysOssConfigService.getOssByBusiType(id, "sys_picture");
            if (ossDto != null) {
                data.setFiles(ossDto);
            }
        }
        return Result.ok(data);
    }
    @PostMapping
    @ApiOperation("新增")
    @LogOperation("新增")
    public Result insert(@RequestBody SysPictureBase sysPictureBase) {
        //效验数据
        ValidatorUtils.validateEntity(sysPictureBase, AddGroup.class, DefaultGroup.class);
        Integer no = sysPictureBaseService.getNo();
        sysPictureBase.setSortNo(no + 1);
        sysPictureBaseService.insert(sysPictureBase);
        sysOssConfigService.updateOss(sysPictureBase.getId(), sysPictureBase.getFiles());// 保存附件
    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody MultipartFile file, Long id, String type, String subType,
                       String name, String contentType, String systemMark, Integer sortNo, String remark) {
        SysPictureBase sysPictureBase;
        if (id != null) {
            sysPictureBase = sysPictureBaseService.get(id);
            sysPictureBase.setType(type);
            sysPictureBase.setSubType(subType);
            sysPictureBase.setName(name);
            sysPictureBase.setContentType(contentType);
            sysPictureBase.setSystemMark(systemMark);
            sysPictureBase.setSortNo(sortNo);
            sysPictureBase.setRemark(remark);
            sysPictureBaseService.update(sysPictureBase);
        } else {
            sysPictureBase = new SysPictureBase();
            sysPictureBase.setType(type);
            sysPictureBase.setSubType(subType);
            sysPictureBase.setName(name);
            sysPictureBase.setContentType(contentType);
            sysPictureBase.setSystemMark(systemMark);
            Integer no = sysPictureBaseService.getNo();
            sysPictureBase.setSortNo(no + 1);
            sysPictureBase.setRemark(remark);
            sysPictureBaseService.insert(sysPictureBase);
        }
        if (file != null) {
            BufferedImage bufferedImage = null;
            try {
                String fileName = file.getOriginalFilename();
                String[] arr = fileName.split("\\.");
                String suffixName = arr[arr.length - 1].toLowerCase();
                bufferedImage = ImageIO.read(file.getInputStream());
                // 宽度
                int width = bufferedImage.getWidth();
                sysPictureBase.setWidth(width);
                // 高度
                int height = bufferedImage.getHeight();
                sysPictureBase.setHeight(height);
                sysPictureBaseService.update(sysPictureBase);
                String tempUploadDir = localPath + "/product_img/";
                File dir = new File(tempUploadDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                ImageIO.write(bufferedImage, suffixName, new File(tempUploadDir + sysPictureBase.getId().toString()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Result.ok();
    }
    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody SysPictureBase sysPictureBase) {
        //效验数据
        ValidatorUtils.validateEntity(sysPictureBase, UpdateGroup.class, DefaultGroup.class);
        sysPictureBaseService.update(sysPictureBase);
        sysOssConfigService.updateOss(sysPictureBase.getId(), sysPictureBase.getFiles());// 保存附件
    @RequestMapping("/getProductImg")
    public void getProductImg(HttpServletResponse response, Long id) {
        try {
            String tempUploadDir = localPath + "/product_img/" + id;
            File file = new File(tempUploadDir);
            //读取指定路径下面的文件
            InputStream in = new FileInputStream(file);
        return Result.ok();
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            //创建存放文件内容的数组
            byte[] buff = new byte[1024];
            //所读取的内容使用n来接收
            int n;
            //当没有读取完时,继续读取,循环
            while ((n = in.read(buff)) != -1) {
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff, 0, n);
            }
            //强制将缓存区的数据进行输出
            outputStream.flush();
            //关流
            outputStream.close();
            in.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
    @DeleteMapping