package com.ho.flow.controller;

import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.entity.WorkOrderPicture;
import com.ho.flow.service.WorkOrderPictureService;
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 java.io.File;

/**
 * @Description 图片
 * Author yule
 * Date 2023/1/4 10:48
 */
@RestController
@RequestMapping( ContextConstant.FLOW_CONTEXT + "picture")
public class WorkOrderPictureController {

    @Autowired
    WorkOrderPictureService workOrderPictureService;

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

    @PostMapping("/deletePicture")
    @TokenIgnore
    public void deletePicture(@RequestBody Integer id){
        WorkOrderPicture picture = workOrderPictureService.selectById(id);
        //先删除文件
        File file = new File(picture.getUrl());
        if (file.exists()){
            file.delete();
        }
        //在删除数据库中数据
        workOrderPictureService.deletedById(id);
    }
}
