package com.engine.jucailinkq.attendance.component.ImportAndExport.web;

import com.engine.jucailinkq.attendance.component.ImportAndExport.service.ExcelExportImportService;
import com.engine.jucailinkq.attendance.component.ImportAndExport.service.impl.ExcelExportImportServiceImpl;
import com.engine.jucailinkq.attendance.component.ImportAndExport.service.param.ImportParam;
import com.engine.common.util.ParamUtil;
import com.engine.jucailinkq.common.util.ResponseResult;
import com.engine.common.util.ServiceUtil;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
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.time.LocalDate;
import java.util.Map;

/**
 * @Author: sy
 * @Description:
 * @Date: 2024/6/5
 **/
public class ExcelController {

    public ExcelExportImportService getExcelService(User user) {
        return ServiceUtil.getService(ExcelExportImportServiceImpl.class, user);
    }

    @GET
    @Path("/schedule/export")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response scheduleExport(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String,Object> paramMap = ParamUtil.request2Map(request);
        XSSFWorkbook workbook = getExcelService(user).exportSchedule(paramMap);
        String time = LocalDate.now().toString();
        String fileName = "日历排班列表" + time;
        try {
            fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        StreamingOutput output = outputStream -> {
            workbook.write(outputStream);
            outputStream.flush();
        };
        response.setContentType("application/octet-stream");
        return Response.ok(output).header("Content-disposition", "attachment;filename=" + fileName).header("Cache-Control", "no-cache").build();
    }

    @POST
    @Path("/schedule/import")
    @Produces(MediaType.APPLICATION_JSON)
    public String importSchedule(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ImportParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ImportParam, Map<String, Object>>(user).run(getExcelService(user)::importSchedule, param);
    }

    @POST
    @Path("/schedule/preview")
    @Produces(MediaType.APPLICATION_JSON)
    public String importSalaryAcctResultPreview(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ImportParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ImportParam, Map<String, Object>>(user).run(getExcelService(user)::previewImportSchedule, param);
    }
}
