package jnpf.store.controller;

import cn.xuyanwu.spring.file.storage.FileInfo;
import jnpf.AppStatisticsApi;
import jnpf.base.ActionResult;
import jnpf.base.vo.DownloadVO;
import jnpf.constant.FileTypeConstant;
import jnpf.file.FileApi;
import jnpf.file.FileUploadApi;
import jnpf.model.vo.SalaryStaticsVo;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import jnpf.util.RandomUtil;
import jnpf.util.UpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * describe
 *
 * @author HuangLinPan
 * @date 2023/07/17
 */
@Slf4j
@RestController
//@Tag(name = "测试管理" , description = "example")
@RequestMapping("/test")
public class TestController {
    @Autowired
    private UserApi userApi;

    @Autowired
    private FileApi fileApi;
    @Autowired
    private FileUploadApi fileUploadApi;

    @Autowired
    private AppStatisticsApi appStatisticsApi;

    @PostMapping("/getTestList")
    public ActionResult list()throws IOException {
        List<UserEntity> list = userApi.getList();
        return ActionResult.success(list);
    }


    @GetMapping("/getSalaryStatics")
    public ActionResult getSalaryStatics(@RequestParam List<String> userIds, @RequestParam("startDay") String startDay, @RequestParam("endDay") String endDay)throws IOException {
        List<SalaryStaticsVo> salaryStatics = appStatisticsApi.getSalaryStatics(userIds, startDay, endDay);
        return ActionResult.success(salaryStatics);
    }
    @PostMapping("/testUploadFile")
    public ActionResult test()throws Exception {
        File file = new File("E:\\个人资料\\test.xlsx");
        FileItem fileItem = this.getMultipartFile(file, "test");
        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

        if (multipartFile.getOriginalFilename().endsWith(".xlsx") || multipartFile.getOriginalFilename().endsWith(".xls")) {
            String filePath = fileApi.getPath(FileTypeConstant.TEMPORARY);
            String fileName = RandomUtil.uuId() + "." + UpUtil.getFileType(multipartFile);
            //上传文件
            FileInfo fileInfo = fileUploadApi.uploadFile(multipartFile, filePath, fileName);
            DownloadVO vo = DownloadVO.builder().build();
            vo.setName(fileInfo.getFilename());
            return ActionResult.success(vo);
        } else {
            return ActionResult.fail("选择文件不符合导入");
        }

    }


    private FileItem getMultipartFile(File file, String fieldName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
        int bytesRead = 0;
        int len = 8192;
        byte[] buffer = new byte[len];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, len)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }


}
