package com.engine.salary.report.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.report.entity.dto.SalaryStatisticsEmployeeListDTO;
import com.engine.salary.report.entity.param.SalaryStatisticsEmployeeDetailQueryParam;
import com.engine.salary.report.entity.param.SalaryStatisticsEmployeeQueryParam;
import com.engine.salary.report.entity.param.SalaryStatisticsEmployeeSalaryQueryParam;
import com.engine.salary.report.wrapper.SalaryStatisticsEmployeeWrapper;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.page.PageInfo;
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.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
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;


/**
 * 薪酬统计员工明细
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class SalaryStatisticsEmployeeController {

    private SalaryStatisticsEmployeeWrapper getSalaryStatisticsEmployeeWrapper(User user) {
        return ServiceUtil.getService(SalaryStatisticsEmployeeWrapper.class, user);
    }
    
    /**
     * 员工列表
     *
     * @param queryParam
     * @return
     */
    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String list(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsEmployeeQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsEmployeeQueryParam, PageInfo<SalaryStatisticsEmployeeListDTO>>(user).run(getSalaryStatisticsEmployeeWrapper(user)::list, queryParam);
    }

    /**
     * 员工详情列表
     *
     * @param queryParam
     * @return
     */
    @POST
    @Path("/detailList")
    @Produces(MediaType.APPLICATION_JSON)
    public String detailList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsEmployeeDetailQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsEmployeeDetailQueryParam, Map<String, Object>>(user).run(getSalaryStatisticsEmployeeWrapper(user)::detailList, queryParam);
    }

    /**
     * 员工薪资列表
     *
     * @param queryParam
     * @return
     */
    @POST
    @Path("/salaryList")
    @Produces(MediaType.APPLICATION_JSON)
    public String salaryList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsEmployeeSalaryQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsEmployeeSalaryQueryParam, Map<String, Object>>(user).run(getSalaryStatisticsEmployeeWrapper(user)::salaryList, queryParam);
    }

    /**
     * 员工薪资列表合计行
     *
     * @param queryParam
     * @return
     */
    @POST
    @Path("/salaryListSum")
    @Produces(MediaType.APPLICATION_JSON)
    public String salaryListSum(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsEmployeeSalaryQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsEmployeeSalaryQueryParam, Map<String, Object>>(user).run(getSalaryStatisticsEmployeeWrapper(user)::salaryListSum, queryParam);
    }

    /**
     * 导出员工薪资列表
     *
     * @param queryParam
     * @return
     */
    @POST
    @Path("/exportSalaryList")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportSalaryList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsEmployeeSalaryQueryParam queryParam) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            XSSFWorkbook workbook = getSalaryStatisticsEmployeeWrapper(user).exportSalaryList(queryParam);

            String fileName = "薪资明细" + LocalDate.now();

            try {
                fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
            } catch (UnsupportedEncodingException e) {
                log.error(e.getMessage(), e);
            }

            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;
        }
    }

//    /**
//     * 导出员工详情列表
//     *
//     * @param queryParam
//     * @return
//     */
//    @PostMapping("/exportDetailList")
//    @ApiOperation("导出员工详情列表")
//    @WeaPermission(publicPermission = true)
//    public WeaResult<Map<String, Object>> exportDetailList(@RequestBody SalaryStatisticsEmployeeDetailQueryParam queryParam) {
//        return WeaResult.success(getSalaryStatisticsEmployeeWrapper(user).exportDetailList(queryParam, UserContext.getCurrentEmployeeId(), TenantContext.getCurrentTenantKey()));
//    }
}
