package com.ankaibei.workFlow.webadmin.ankaibei.controller;

import com.ankaibei.workFlow.common.core.annotation.NoAuthInterface;
import com.ankaibei.workFlow.webadmin.ankaibei.entity.AccesoryInfoEntity;
import com.ankaibei.workFlow.webadmin.ankaibei.enums.CodeEnum;
import com.ankaibei.workFlow.webadmin.ankaibei.mapper.AccesoryInfoMapper;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.AccesoryInfoVo;
import com.ankaibei.workFlow.webadmin.ankaibei.vo.ResMsg;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wenqi
 */
@RequestMapping(path = "/files")
@RestController
public class FileController {

    @Resource
    private AccesoryInfoMapper accesoryInfoMapper;

    /**
     * 服务器存储路径
     */
    final String basePath = "/ankaibei/uploadaccesory/";
    /**
     * 本地存储路径
     */
    String locationPath = "D:/upload/";

    /**
     * 上传附件
     *
     * @param
     * @return ResMsg
     */
    @RequestMapping(value = "/uploadAccesory")
    public ResMsg upload(AccesoryInfoVo file) throws IOException {
        AccesoryInfoEntity accesoryInfoEntity = new AccesoryInfoEntity();
        accesoryInfoEntity.setProjectId(file.getProjectId());
        if (!StringUtils.isEmpty(file.getContract())) {
            String uploading = uploading(file.getContract());
            accesoryInfoEntity.setContract(uploading);
        }
        if (!StringUtils.isEmpty(file.getScheme())) {
            String uploading = uploading(file.getScheme());
            accesoryInfoEntity.setScheme(uploading);
        }
        if (!StringUtils.isEmpty(file.getPlan())) {
            String uploading = uploading(file.getPlan());
            accesoryInfoEntity.setPlan(uploading);
        }
        if (!StringUtils.isEmpty(file.getSubmitMaterial())) {
            String uploading = uploading(file.getSubmitMaterial());
            accesoryInfoEntity.setSubmitMaterial(uploading);
        }
        if (!StringUtils.isEmpty(file.getAcceptance())) {
            String uploading = uploading(file.getAcceptance());
            accesoryInfoEntity.setAcceptance(uploading);
        }
        accesoryInfoMapper.save(accesoryInfoEntity);
        return new ResMsg(CodeEnum.OK);
    }

    private String uploading(List<MultipartFile> files) throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        for (MultipartFile file : files) {
            InputStream inputStream = file.getInputStream();
            //文件后缀
            String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String fileName = UUID.randomUUID() + prefix;
            Files.copy(inputStream, new File(basePath + fileName).toPath(), StandardCopyOption.REPLACE_EXISTING);
            //拼接上传文件路径
            String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                    .path("/" + "static/" + fileName)
                    .toUriString();
            stringBuffer.append(fileDownloadUri + ",");
        }
        return stringBuffer.toString();
    }

}
