package com.ho.business.controller;

import com.ho.business.entity.Picture;
import com.ho.business.service.PictureService;
import com.ho.business.vo.req.StationReq;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.annotation.TokenIgnore;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Description 图片
 * Author yule
 * Date 2023/1/4 10:48
 */
@RestController
@RequestMapping( "outerApi")
public class PictureController {

    @Autowired
    PictureService pictureService;

    //根据路径新增图片对象数据入库
    @PostMapping("/addPicture")
    @LogAnnotation(title = "图片", action = "新增图片对象")
    @TokenIgnore
    public Picture addPicture(@RequestBody String url){
        Picture picture = pictureService.insertPicture(url);
        return picture;
    }
    //根据id查询图片对象
    @PostMapping("/selectByIdPicture")
    @TokenIgnore
    public Picture selectPicture(@RequestBody Integer id){
        Picture picture = pictureService.selectById(id);
        return picture;
    }

    @PostMapping("/deletePicture")
    @LogAnnotation(title = "图片", action = "删除图片对象")
    @TokenIgnore
    public void deletePicture(@RequestBody Integer id){
        Picture picture = pictureService.selectById(id);
        //先删除文件
        File file = new File(picture.getUrl());
        if (file.exists()){
            file.delete();
        }
        //在删除数据库中数据
        pictureService.deletedById(id);
    }

    @PostMapping("/saveOrUpdatePicture")
    @LogAnnotation(title = "保存或更新图片", action = "保存或更新图片")
    @TokenIgnore
    public Picture saveOrUpdatePicture(@RequestBody Picture vo){
        Picture picture = pictureService.saveOrUpdatePicture(vo);
        return picture;
    }
}
