package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.datacollection.dto.VariableItemListDTO;
import com.engine.salary.entity.datacollection.param.VariableItemQueryParam;
import com.engine.salary.entity.datacollection.param.VariableItemSaveParam;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.wrapper.VariableItemWrapper;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
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 java.util.List;

/**
 * 数据采集-浮动薪酬项目
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: 泛微软件</p>
 *
 * @author xzy
 * @version 1.0
 **/
@Slf4j
public class VariableItemController {

    private VariableItemWrapper getVariableItemWrapper(User user) {
        return ServiceUtil.getService(VariableItemWrapper.class, user);
    }

    /**
     * 浮动薪酬项目列表
     *
     * @return
     */
    @POST
    @Path("/listPage")
    @Produces(MediaType.APPLICATION_JSON)
    public String listPage(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody VariableItemQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<VariableItemQueryParam, PageInfo<VariableItemListDTO>>(user).run(getVariableItemWrapper(user)::listPage, queryParam);
    }


    /**
     * 浮动薪酬项目详细信息
     *
     * @return
     */
    @POST
    @Path("/getDetail")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDetail(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody VariableItemQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, VariableItemListDTO>(user).run(getVariableItemWrapper(user)::getDetail, queryParam.getId());
    }


    /**
     * 保存浮动薪资项目
     *
     * @param request
     * @param response
     * @param saveParam
     * @return
     */
    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveVariableItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody VariableItemSaveParam saveParam) {
        User user = HrmUserVarify.getUser(request, response);
        if (saveParam.getId() == null || saveParam.getId() <= 0) {
            return new ResponseResult<VariableItemSaveParam, String>(request, response, user).run(getVariableItemWrapper(user)::save, saveParam);
        } else {
            return new ResponseResult<VariableItemSaveParam, String>(request, response, user).run(getVariableItemWrapper(user)::update, saveParam);
        }
    }



    /**
     * 删除浮动薪资项目
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody VariableItemQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<Long>, String>(request, response, user).run(getVariableItemWrapper(user)::deleteItems, param.getItemIds());
    }






























    // @GET
    // @Path("/downloadTemplate")
    // @Produces(MediaType.APPLICATION_OCTET_STREAM)
    // public Response getAll(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    //     try {
    //         User user = HrmUserVarify.getUser(request, response);
    //         OtherDeductionQueryParam param = buildParam(request);
    //
    //         XSSFWorkbook workbook = getOtherDeductionWrapper(user).downloadTemplate(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;
    //     }
    // }
    //
    //
    // /**
    //  * 导出
    //  *
    //  * @param
    //  * @return
    //  */
    // @GET
    // @Path("/export")
    // @Produces(MediaType.APPLICATION_OCTET_STREAM)
    // public Response export(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    //     try {
    //         User user = HrmUserVarify.getUser(request, response);
    //
    //         OtherDeductionQueryParam param = buildParam(request);
    //
    //         XSSFWorkbook workbook = getOtherDeductionWrapper(user).export(param);
    //
    //         String fileName = null;
    //         try {
    //             fileName = URLEncoder.encode("其他免税扣除" + LocalDate.now() + ".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;
    //     }
    // }
    //
    //
    //
    //
    // @Nullable
    // private OtherDeductionQueryParam buildParam(HttpServletRequest request) {
    //     OtherDeductionQueryParam param = new OtherDeductionQueryParam();
    //     String ids = request.getParameter("ids");
    //     if (StringUtils.isNotBlank(ids)) {
    //         param.setIds(Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList()));
    //     }
    //     String keyword = request.getParameter("keyword");
    //     if (StringUtils.isNotBlank(keyword)) {
    //         param.setKeyword(keyword);
    //     }
    //     String id = request.getParameter("id");
    //     if (StringUtils.isNotBlank(id)) {
    //         param.setId(Long.valueOf(id));
    //     }
    //     String declareMonth = request.getParameter("declareMonth");
    //     if (StringUtils.isNotBlank(declareMonth)) {
    //         param.setDeclareMonth(Arrays.stream(declareMonth.split(",")).map(e -> e + "-01 00:00:00").collect(Collectors.toList()));
    //         param.setDeclareMonthDate(Arrays.stream(declareMonth.split(",")).map(e -> e + "-01 00:00:00").map(SalaryDateUtil::dateStrToLocalTime).collect(Collectors.toList()));
    //     }
    //
    //     String username = request.getParameter("username");
    //     if (StringUtils.isNotBlank(username)) {
    //         param.setUsername(username);
    //     }
    //     String employeeId = request.getParameter("employeeId");
    //     if (StringUtils.isNotBlank(employeeId)) {
    //         param.setEmployeeId(Long.valueOf(employeeId));
    //     }
    //     String taxAgentId = request.getParameter("taxAgentId");
    //     if (StringUtils.isNotBlank(taxAgentId)) {
    //         param.setTaxAgentId(Long.valueOf(taxAgentId));
    //     }
    //     String departmentIds = request.getParameter("departmentIds");
    //     if (StringUtils.isNotBlank(departmentIds)) {
    //         param.setDepartmentIds(Arrays.stream(departmentIds.split(",")).map(Long::valueOf).collect(Collectors.toList()));
    //     }
    //     String jobNum = request.getParameter("jobNum");
    //     if (StringUtils.isNotBlank(jobNum)) {
    //         param.setJobNum(jobNum);
    //     }
    //     String idNo = request.getParameter("idNo");
    //     if (StringUtils.isNotBlank(idNo)) {
    //         param.setIdNo(idNo);
    //     }
    //     String hiredate = request.getParameter("hiredate");
    //     if (StringUtils.isNotBlank(hiredate)) {
    //         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    //         List<Date> dates = Arrays.stream(hiredate.split(",")).map(d -> {
    //             try {
    //                 return format.parse(d);
    //             } catch (ParseException e) {
    //                 e.printStackTrace();
    //             }
    //             return null;
    //         }).collect(Collectors.toList());
    //         param.setHiredate(dates);
    //     }
    //     String mobile = request.getParameter("mobile");
    //     if (StringUtils.isNotBlank(mobile)) {
    //         param.setMobile(mobile);
    //     }
    //     String otherTaxExemptDeductionId = request.getParameter("otherTaxExemptDeductionId");
    //     if (StringUtils.isNotBlank(otherTaxExemptDeductionId)) {
    //         param.setOtherTaxExemptDeductionId(Long.valueOf(otherTaxExemptDeductionId));
    //     }
    //
    //     String hasData = request.getParameter("hasData");
    //     if (StringUtils.isNotBlank(hasData)) {
    //         param.setHasData("true".equals(hasData));
    //     }
    //     return param;
    // }
    //
    // @POST
    // @Path("/preview")
    // @Produces(MediaType.APPLICATION_JSON)
    // public String preview(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody OtherDeductionImportParam importParam) {
    //     User user = HrmUserVarify.getUser(request, response);
    //     return new ResponseResult<OtherDeductionImportParam, Map<String, Object>>(user).run(getOtherDeductionWrapper(user)::preview, importParam);
    // }
    //
    // @POST
    // @Path("/importData")
    // @Produces(MediaType.APPLICATION_JSON)
    // public String importAddUpDeduction(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody OtherDeductionImportParam importParam) {
    //     User user = HrmUserVarify.getUser(request, response);
    //     return new ResponseResult<OtherDeductionImportParam, Map<String, Object>>(user).run(getOtherDeductionWrapper(user)::importData, importParam);
    // }
    //
    // /**
    //  * @description 获取其他免税扣除数据
    //  * @return String
    //  * @author Harryxzy
    //  * @date 2022/10/31 13:42
    //  */
    // @POST
    // @Path("/getData")
    // @Produces(MediaType.APPLICATION_JSON)
    // public String getOtherDeduction(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody OtherDeductionParam otherDeductionParam) {
    //     User user = HrmUserVarify.getUser(request, response);
    //     return new ResponseResult<OtherDeductionParam, OtherDeductionRecordDTO>(user).run(getOtherDeductionWrapper(user)::getOtherDeduction, otherDeductionParam);
    // }
    //
    // /**
    //  * @description 删除所选其他免税扣除
    //  * @return String
    //  * @author Harryxzy
    //  * @date 2022/10/27 14:41
    //  */
    // @POST
    // @Path("/deleteSelectData")
    // @Produces(MediaType.APPLICATION_JSON)
    // public String deleteSelectOtherDeduction(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AddUpDeductionRecordDeleteParam otherDeductionDeleteParam) {
    //     User user = HrmUserVarify.getUser(request, response);
    //     return new ResponseResult<AddUpDeductionRecordDeleteParam, Map<String, Object>>(user).run(getOtherDeductionWrapper(user)::deleteSelectData, otherDeductionDeleteParam);
    // }

}
