package com.engine.organization.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.browser.bean.SearchConditionGroup;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.PersonnelResumeWrapper;
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.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.ws.rs.core.Response;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2022/12/28
 * @version: 1.0
 */
public class PersonnelResumeController {
    public PersonnelResumeWrapper getPersonnelResumeWrapper(User user) {
        return ServiceUtil.getService(PersonnelResumeWrapper.class, user);
    }

    /**
     * 左侧树接口
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchTree")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getSearchTree(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        SearchTreeParams params = JSONObject.toJavaObject((JSON) JSONObject.toJSON(map), SearchTreeParams.class);
        return getPersonnelResumeWrapper(user).getSearchTree(params);
    }

    @GET
    @Path("/hasRight")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult hasRight(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getPersonnelResumeWrapper(user).hasRight());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }

    }

    @GET
    @Path("/getResumeList")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getResumeList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            Integer uId = Integer.parseInt(Util.null2String(map.get("id")));
            return ReturnResult.successed(getPersonnelResumeWrapper(user).getResumeList(uId));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }

    }

    @GET
    @Path("/personnelScreening")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult personnelScreening(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String,Object> returnMap = new HashMap<>();
            List<SearchConditionGroup> addGroups = new ArrayList<>();
            addGroups.add(new SearchConditionGroup("高级搜索条件", true, getPersonnelResumeWrapper(user).personnelScreening()));
            returnMap.put("conditions",addGroups);
            return ReturnResult.successed(returnMap);
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }

    }

    /**
     * 人员简历下载
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/downloadPerResume")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response downloadPerResume(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        // type: 0:合并 1：不合并
        Integer type = Integer.parseInt(Util.null2String(map.get("type")));

        String realPath = getPersonnelResumeWrapper(user).downloadPerResume(type,response);
        File file = new File(realPath);
        // 如果文件不存在，提示404
        if (!file.exists()) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        String fileName = URLEncoder.encode(file.getName(), "UTF-8");
        return Response
                .ok(file)
                .header("Content-disposition", "attachment;filename=" + fileName)
                .header("Cache-Control", "no-cache").build();
    }
}
