package com.engine.shuzhi.web;

import com.alipay.oceanbase.jdbc.StringUtils;
import com.engine.common.util.ServiceUtil;
import com.engine.shuzhi.service.SzBatchExportService;
import com.engine.shuzhi.service.impl.SzBatchExportServiceImpl;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:dxfeng
 * @createTime: 2023/06/02
 * @version: 1.0
 */
public class SzBatchExportController {

    public SzBatchExportService getService(User user) {
        return ServiceUtil.getService(SzBatchExportServiceImpl.class, user);
    }

    @GET
    @Path("/resource/export")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response resourceExport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                   @QueryParam("ids") String ids) {
        List<Long> idList = new ArrayList<>();
        if (StringUtils.isNotBlank(ids)) {
            idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        User user = HrmUserVarify.getUser(request, response);

        Map<String, Object> map = getService(user).downloadResourceFile(idList);
        String zipFilename = Util.null2String(map.get("zipFilename"));
        String outPutPath = Util.null2String(map.get("outPutPath"));
        try {
            zipFilename = URLEncoder.encode(zipFilename, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        java.nio.file.Path path = Paths.get(outPutPath);
        StreamingOutput output = outputStream -> {
            byte[] data = Files.readAllBytes(path);
            outputStream.write(data);
            outputStream.flush();
        };
        response.setContentType("application/octet-stream");
        return Response.ok(output).header("Content-disposition", "attachment;filename=" + zipFilename).header("Cache-Control", "no-cache").build();
    }

    @GET
    @Path("/contract/export")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response contractExport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                   @QueryParam("ids") String ids) {
        List<Long> idList = new ArrayList<>();
        if (StringUtils.isNotBlank(ids)) {
            idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        User user = HrmUserVarify.getUser(request, response);

        Map<String, Object> map = getService(user).downloadContractFile(idList);
        String zipFilename = Util.null2String(map.get("zipFilename"));
        String outPutPath = Util.null2String(map.get("outPutPath"));
        try {
            zipFilename = URLEncoder.encode(zipFilename, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        java.nio.file.Path path = Paths.get(outPutPath);
        StreamingOutput output = outputStream -> {
            byte[] data = Files.readAllBytes(path);
            outputStream.write(data);
            outputStream.flush();
        };
        response.setContentType("application/octet-stream");
        return Response.ok(output).header("Content-disposition", "attachment;filename=" + zipFilename).header("Cache-Control", "no-cache").build();
    }


}
