package com.engine.organization.web;

import com.alipay.oceanbase.jdbc.StringUtils;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.hrmresource.param.HrmResourceSearchParam;
import com.engine.organization.wrapper.ExportCommonWrapper;
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.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.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author weaver_cl
 * @Description:
 * @Date 2022/6/28
 * @Version V1.0
 **/
public class ExportCommonController {

    private ExportCommonWrapper getExportCommonWrapper(User user) {
        return ServiceUtil.getService(ExportCommonWrapper.class,user);
    }

    @GET
    @Path("/resource/export")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response resourceExport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                   @QueryParam("ids") String ids) {
        //HrmResourceSearchParam param = buildResourceParam(request);
        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);
        XSSFWorkbook workbook = getExportCommonWrapper(user).resourceExport(idList);
        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();
    }



    private HrmResourceSearchParam buildResourceParam(HttpServletRequest request) {
        HrmResourceSearchParam param = new HrmResourceSearchParam();
        String lastName = request.getParameter("lastName");
        if (StringUtils.isNotBlank(lastName)){
            param.setLastName(lastName);
        }
        String managerId = request.getParameter("managerId");
        if (StringUtils.isNotBlank(managerId)){
            param.setManagerId(Long.valueOf(managerId));
        }
        String companyId = request.getParameter("companyId");
        if (StringUtils.isNotBlank(companyId)){
            param.setCompanyId(Long.valueOf(companyId));
        }
        String departmentId = request.getParameter("departmentId");
        if (StringUtils.isNotBlank(departmentId)){
            param.setDepartmentId(Long.valueOf(departmentId));
        }
        String mobile = request.getParameter("mobile");
        if (StringUtils.isNotBlank(mobile)){
            param.setMobile(mobile);
        }
        String telephone = request.getParameter("telephone");
        if (StringUtils.isNotBlank(telephone)){
            param.setTelephone(telephone);
        }
        String mobileCall = request.getParameter("mobileCall");
        if (StringUtils.isNotBlank(mobileCall)){
            param.setMobileCall(mobileCall);
        }
        String jobTitle = request.getParameter("jobTitle");
        if (StringUtils.isNotBlank(jobTitle)){
            param.setJobTitle(Long.valueOf(jobTitle));
        }
        return param;
    }


}
