package com.goafanti.RD.controller;

import com.goafanti.RD.bo.InputRdDetails;
import com.goafanti.RD.bo.InputRdList;
import com.goafanti.RD.service.RDService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.controller.PublicController;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.common.utils.excel.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/api/admin/RD")
public class RDController extends BaseApiController {

    @Autowired
    private RDService rdService;

    @Value(value = "${upload.path}")
    private final String	uploadPath			= null;


    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result add( InputRdDetails in ) {
        Result res= new Result();
        res.data(rdService.add(in));
        return res;
    }



    @RequestMapping(value="/update",method = RequestMethod.POST)
    public Result update( InputRdDetails in) {
        Result res= new Result();
        res.data(rdService.update(in));
        return res;
    }



    @RequestMapping(value = "/addBrowse",method = RequestMethod.POST)
    public Result addBrowse(Long id){
        Result res =new Result();
        res.setData(rdService.updateBrowseOrDownload(id,0));
        return res;
    }

    @RequestMapping(value = "/addDownload",method = RequestMethod.POST)
    public Result addDownload(Long id){
        Result res =new Result();
        res.setData(rdService.updateBrowseOrDownload(id,1));
        return res;
    }

    @RequestMapping(value = "/details",method = RequestMethod.GET)
    public Result details(Long id){
        Result res =new Result();
        res.setData(rdService.details(id));
        return res;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(InputRdList in){
        Result res =new Result();
        res.setData(rdService.list(in));
        return res;
    }

    @RequestMapping(value = "/exportList",method = RequestMethod.GET)
    public Result exportList(InputRdList in){
        return rdService.exportList(in,uploadPath);
    }


    /**
     * 	Excel批量导入
     *
     */
    @RequestMapping(value = "/import" , method = RequestMethod.POST)
    public Result importTemplate(@RequestParam(value = "file", required = false) MultipartFile file) {
        Result res=new Result();
        //判断文件是否存在
        if(null == file){
            res.getError().add(buildError("文件不存在！","文件不存在！"));
            return res;
        }
        String fileName = file.getOriginalFilename();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            res.getError().add(buildError("格式不正确","格式不正确"));
            return res;
        }
        res.data(rdService.batchList(file));
        return res;
    }

    /**
     * 	批量导入回款模版
     *
     */
    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
    public void downloadTemplate(HttpServletResponse response) {
        StringBuilder url= new StringBuilder(uploadPath);
        url.append("/tmp").append("/RD_details_template.xlsx");
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, url.toString(),"研发活动导入模版.xlsx");
            FileUtils.writeBytes(url.toString(), response.getOutputStream());
        }
        catch (Exception e) {
            LoggerUtils.error(PublicController.class,"下载文件失败", e);
        }
    }

    @RequestMapping(value = "/downloadDetailsWord",method = RequestMethod.GET)
    public void downloadDetailsWord(Long id,HttpServletResponse response){
        rdService.pushDownloadDetailsWord(id,response);
    }

    @RequestMapping(value = "/downloadDetailsPDF",method = RequestMethod.GET)
    public void downloadDetailsPDF(Long id,HttpServletResponse response){
        rdService.pushDownloadDetailsPDF(id,response);
    }

}
