package lingtao.net.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.Bug;
import lingtao.net.bean.Msg;
import lingtao.net.service.BugService;

@RestController
public class BugController {

    @Autowired
    private BugService bugService;

    private String localPrefix = "abc\\bug";

    private String localPath = "C:\\lingtao\\quote_price\\upload";

    //private String localDomain = "http://47.114.150.226:8080/erp";

    /**
     * bug列表
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getBugs")
    public PageInfo<Bug> getBugs(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit, Bug bug) {
        PageHelper.startPage(page, limit);
        List<Bug> bugList = bugService.getBugs(bug);
        PageInfo<Bug> pageInfo = new PageInfo<Bug>(bugList);
        return pageInfo;
    }

    /**
     * 添加
     */
    @RequestMapping("/addBug")
    public Msg addBug(Bug bug, HttpSession session) {
        return bugService.addBug(bug, session);
    }

    /**
     * 更新
     */
    @RequestMapping("/updateBug")
    public Msg updateBug(Bug bug, HttpSession session) {
        return bugService.updateBug(bug);
    }

    // 图片上传及新增
    @RequestMapping("/bugUpload")
    public Msg upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return Msg.fail("文件不能为空");
        }
        // 获取文件名后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        // 获取path
        String path = getPath(extension, FilenameUtils.getBaseName(file.getOriginalFilename()));
        // 保存文件信息
        File newFile = new File(localPath + File.separator + path);
        try {
            FileUtils.copyInputStreamToFile(new ByteArrayInputStream(file.getBytes()), newFile);
        } catch (IOException e) {
            throw new RuntimeErrorException(null, "");
        }
        return Msg.success();
    }

    /**
     * @param prefixSelf 根据上传的接口存入自己的文件夹
     * @param suffix     文件的后缀
     * @param fileName   文件名
     * @return
     */
    public String getPath(String suffix, String fileName) {

        // 生成uuid
        // String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String path = null;

        // 文件路径
        path = DateUtils.formatDate(new Date(), "yyyyMMdd") + File.separator + fileName;

        path = localPrefix + File.separator + File.separator + path;

        return path + "." + suffix;
    }

}
