package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.setting.dto.PageListTemplateDTO;
import com.engine.salary.entity.setting.dto.PageListTemplateDetailDTO;
import com.engine.salary.entity.setting.param.*;
import com.engine.salary.entity.setting.po.PageListSettingPO;
import com.engine.salary.entity.setting.po.PageListTemplatePO;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.SalaryEnumUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.wrapper.SalaryCommonWrapper;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
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.List;
import java.util.Map;

@Slf4j
public class SalaryCommonController {

    private SalaryCommonWrapper getSalaryCommonWrapper(User user) {
        return ServiceUtil.getService(SalaryCommonWrapper.class, user);
    }

    @GET
    @Path("/enum/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "enumClass") String enumClass) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, List<Map<String, Object>>>(user).run(SalaryEnumUtil::enumSelectList, enumClass);
    }


    @GET
    @Path("/cache/info")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCacheInfo(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "key") String key) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Object>(user).run(getSalaryCommonWrapper(user)::getCacheInfo, key);
    }

    @GET
    @Path("/cache/remove")
    @Produces(MediaType.APPLICATION_JSON)
    public String removeCache(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "key") String key) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, String>(user).run(getSalaryCommonWrapper(user)::removeCache, key);
    }


    /**
     * 页面模板设置
     *
     * @param param
     * @return
     */
    @POST
    @Path("/pageList/save/setting")
    @Produces(MediaType.APPLICATION_JSON)
    public String savePageListSetting(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PageListSettingSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListSettingSaveParam, String>(user).run(getSalaryCommonWrapper(user)::savePageListSetting, param);
    }

    @POST
    @Path("/pageList/reset/setting")
    @Produces(MediaType.APPLICATION_JSON)
    public String resetPageListSetting(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PageListSettingResetParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListSettingResetParam, String>(user).run(getSalaryCommonWrapper(user)::resetPageListSetting, param);
    }

    /**
     * 页面默认值
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/pageList/get/setting")
    @Produces(MediaType.APPLICATION_JSON)
    public String getPageListSetting(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListSettingQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListSettingQueryParam, PageListTemplateDetailDTO>(user).run(getSalaryCommonWrapper(user)::getPageListSetting, param);
    }

    /**
     * 页面模板表单
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/pageList/template/get")
    @Produces(MediaType.APPLICATION_JSON)
    public String getPageListTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListTemplateQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListTemplateQueryParam, PageListTemplateDetailDTO>(user).run(getSalaryCommonWrapper(user)::getPageListTemplate, param);
    }

    /**
     * 页面模板列表
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/pageList/template/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String getPageListTemplates(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListTemplateQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListTemplateQueryParam, List<PageListTemplatePO>>(user).run(getSalaryCommonWrapper(user)::getPageListTemplates, param);
    }

    @POST
    @Path("/pageList/template/pageList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getTemplates(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListTemplateQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListTemplateQueryParam, PageInfo<PageListTemplateDTO>>(user).run(getSalaryCommonWrapper(user)::getTemplates, param);
    }

    /**
     * 页面模板列表
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/pageList/template/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String savePageListTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListTemplateSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListTemplateSaveParam, PageListTemplatePO>(user).run(getSalaryCommonWrapper(user)::savePageListTemplate, param);
    }

    @POST
    @Path("/pageList/template/file/download")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response downloadPageExportTemplateFile(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody DownloadTemplateParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);

            XSSFWorkbook workbook = getSalaryCommonWrapper(user).downloadPageExportTemplateFile(param);

            String fileName = "薪资明细模板示例" + LocalDate.now();
            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();
        } catch (Exception e) {
            log.error("导出薪资明细模板异常", e);
            throw e;
        }
    }

    @POST
    @Path("/pageList/template/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String savePageListTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListTemplateDeleteParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListTemplateDeleteParam, PageListTemplatePO>(user).run(getSalaryCommonWrapper(user)::deletePageListTemplate, param);
    }


    /**
     * 页面模板列表
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/pageList/template/change")
    @Produces(MediaType.APPLICATION_JSON)
    public String changeTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody PageListTemplateChangeParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PageListTemplateChangeParam, PageListSettingPO>(user).run(getSalaryCommonWrapper(user)::changePageListTemplate, param);
    }
}
