package com.ztmap.framework.web.file.controller;

import com.ztmap.framework.web.controller.BaseController;
import com.ztmap.framework.web.domain.AjaxResult;
import com.ztmap.framework.web.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 文件上传controller
 * @author fuce 
 * @date: 2018年9月16日 下午4:23:50
 */
@Controller
@RequestMapping("/FileController")
@ApiIgnore
public class FileController extends BaseController {
	@Autowired
	private FileService fileService;

	/**
	 * 上传文件
	 */
	@PostMapping("/uploadImg")
	@ResponseBody
	public AjaxResult uploadImg(@RequestParam("file") MultipartFile file)
	{
		try
		{
			if (!file.isEmpty())
			{
				//插入文件存储表
				String id=fileService.uploadRepairImg(file);
				if(id!=null){
					return AjaxResult.successData(200, id);
				}
			}
			return error();
		}
		catch (Exception e)
		{
			return error(e.getMessage());
		}
	}

	/**
	 * 删除本地文件
	 * @param ids
	 * @return
	 */
	@PostMapping("/del_file")
	@ResponseBody
	public AjaxResult del_file(String ids){
		int b=fileService.deleteBydataFile(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
}
