package com.mvc.controller.taizhang;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.FileUploadModel;
import com.mvc.netty.dealconstant.ResourceTag;
import com.mvc.service.taizhang.FileUploadService;
import com.mvc.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;

@Api(tags = "文件上传接口接口")
@RequestMapping("/FileUploadAction")
@Controller
public class FileUploadAction {


    @Autowired
    private FileUploadService fileUploadService;

    @Resource
    private Environment env;


    @ApiOperation("分页查询文件上传信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllFileUpload(@RequestBody FileUploadModel model) {

        try {
            Map<String, Object> res = fileUploadService.findAllFileUpload(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("保存上传文件调用接口")
    @PostMapping(value = "/saveFileUpload/file")
    @ResponseBody
    public Map<String, Object> saveFileUpload(@RequestParam("file") MultipartFile file, @RequestParam String filetypecode,
                                              @RequestParam(required = false) String version, @RequestParam(required = false) String uploadman,
                                              @RequestParam(required = false) String fileinfo,@RequestParam(required = false) String uploadpass) {
        try {
            //得到文件名称
            String fileName = file.getOriginalFilename();
            String time = DateUtil.getCurrentTime("yyyyMMddHHmmss");
            //获取文件后缀
            //String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //生成文件名称UUID
            String downpath = env.getProperty(ResourceTag.DownloadPathTag.getTag());
            File localfile = new File(downpath + time + fileName);
            file.transferTo(localfile);

            FileUploadModel fileUploadModel = new FileUploadModel();
            fileUploadModel.setFilename(fileName);
            fileUploadModel.setFiletypecode(filetypecode);
            fileUploadModel.setVersion(version);
            fileUploadModel.setUploadman(uploadman);
            fileUploadModel.setFileinfo(fileinfo);
            fileUploadModel.setUploadpass(uploadpass);
            fileUploadModel.setUploadsize((int)file.getSize());

            fileUploadModel = fileUploadService.saveFileUpload(fileUploadModel);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", fileUploadModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除所有文件上传列表")
    @RequestMapping(value = "/deleteFileUpload/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteFileUpload(@PathVariable("id") int id) {
        try {
            int res = fileUploadService.deleteFileUpload(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

}
