package com.engine.salary.process.salaryArchive;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryarchive.dto.SalaryArchiveFormDTO;
import com.engine.salary.entity.salaryarchive.param.SalaryArchiveImportActionParam;
import com.engine.salary.entity.salaryarchive.po.SalaryArchivePO;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.wrapper.SalaryArchiveWrapper;
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;
import java.util.Map;


/**
 * 薪资档案流程使用
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author liuliang
 * @version 1.0
 **/
@Slf4j
public class SalaryArchiveActionAPI {

    private SalaryArchiveWrapper getSalaryArchiveWrapper(User user) {
        return ServiceUtil.getService(SalaryArchiveWrapper.class, user);
    }

    /**
     * 前置校验、列表添加接口
     * @param importData
     * @return
     */
    @POST
    @Path("/checkImportSalaryArchiveListAdd")
    @Produces(MediaType.APPLICATION_JSON)
    public String importSalaryArchiveInit(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryArchiveImportActionParam importData) {
        User user = HrmUserVarify.getUser(request, response);
        importData.setImportType("init");
        importData.setAddData(false);
        return new ResponseResult<SalaryArchiveImportActionParam, Map<String, Object>>(user).run(getSalaryArchiveWrapper(user)::checkSalaryArchiveInit, importData);
    }

    /**
     * 初始化档案
     * @param importData
     * @return
     */
    @POST
    @Path("/initSalaryArchive")
    @Produces(MediaType.APPLICATION_JSON)
    public String salaryArchiveListInit(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryArchiveImportActionParam importData) {
        User user = HrmUserVarify.getUser(request, response);
        importData.setImportType("init");
        importData.setAddData(true);
        return new ResponseResult<SalaryArchiveImportActionParam, Map<String, Object>>(user).run(getSalaryArchiveWrapper(user)::importSalaryArchiveInit, importData);
    }

    @POST
    @Path("/checkAdjustmentSalaryArchive")
    @Produces(MediaType.APPLICATION_JSON)
    public String checkAdjustmentSalaryArchive(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryArchiveImportActionParam importData) {
        User user = HrmUserVarify.getUser(request, response);
        importData.setImportType("salaryItemAdjust");
        importData.setAddData(true);
        return new ResponseResult<SalaryArchiveImportActionParam, Map<String, Object>>(user).run(getSalaryArchiveWrapper(user)::checkAdjustmentSalaryArchive, importData);
    }

    @POST
    @Path("/adjustmentSalaryArchive")
    @Produces(MediaType.APPLICATION_JSON)
    public String adjustmentSalaryArchive(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryArchiveImportActionParam importData) {
        User user = HrmUserVarify.getUser(request, response);
        importData.setImportType("salaryItemAdjust");
        importData.setAddData(true);
        return new ResponseResult<SalaryArchiveImportActionParam, Map<String, Object>>(user).run(getSalaryArchiveWrapper(user)::adjustmentSalaryArchive, importData);
    }

    /**
     * 获取档案信息，默认需要传人员和扣缴义务人id，若只传人员id，发现多个发薪档案会抛出异常提示
     *
     * 废弃，该接口仅返回基础信息。前端调用较为复杂，以后才有获取生效数据接口
     * @param request
     * @param response
     * @param param
     * @return 档案基础信息
     */
    @POST
    @Path("/salaryArchiveInfo")
    @Produces(MediaType.APPLICATION_JSON)
    @Deprecated
    public String adjustmentSalaryArchive(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryArchiveProcessQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryArchiveProcessQueryParam, SalaryArchivePO>(user).run(getSalaryArchiveWrapper(user)::getSalaryArchiveInfo, param);
    }

    /**
     * 获取档案信息，默认需要传人员和扣缴义务人id，若只传人员id，发现多个发薪档案会抛出异常提示
     *
     * 废弃，该接口仅返回基础信息。前端调用较为复杂，以后才有获取生效数据接口
     * @param request
     * @param response
     * @param param
     * @return 档案基础信息
     */
    @POST
    @Path("/v2/salaryArchiveInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public String salaryArchiveInfo(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryArchiveProcessQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryArchiveProcessQueryParam, List<SalaryArchiveFormDTO>>(user).run(getSalaryArchiveWrapper(user)::getSalaryArchiveInfoV2, param);
    }



}
