package com.engine.salary.maintainer;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.maintainer.datacollection.AddUpSituationManager;
import com.engine.salary.maintainer.salaryacct.SalaryAcctManager;
import com.engine.salary.maintainer.salaryacct.SalaryAcctSupplementParam;
import com.engine.salary.maintainer.salaryarchive.SalaryArchiveManager;
import com.engine.salary.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 累计专项附加扣除
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class MaintainerController {

    private SalaryAcctManager getSalaryAcctManager(User user) {
        return ServiceUtil.getService(SalaryAcctManager.class, user);
    }

    private AddUpSituationManager getAddUpSituationManager(User user) {
        return ServiceUtil.getService(AddUpSituationManager.class, user);
    }

    private SalaryArchiveManager getSalaryArchiveManager(User user) {
        return ServiceUtil.getService(SalaryArchiveManager.class, user);
    }

    //---------------------------薪资核算 start ------------------------------------

    /**
     * 补充核算
     *
     * @return
     */
    @GET
    @Path("/acct/supplement")
    @Produces(MediaType.APPLICATION_JSON)
    public String supplementAcctRecord(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String type = request.getParameter("type");
        String salaryAcctRecordStrIds = request.getParameter("salaryAcctRecordStrIds");
        String salarySobStrIds = request.getParameter("salarySobStrIds");
        String salaryItemStrIds = request.getParameter("salaryItemStrIds");
        SalaryAcctSupplementParam queryParam = SalaryAcctSupplementParam.builder()
                .type(type)
                .salaryAcctRecordStrIds(salaryAcctRecordStrIds)
                .salarySobStrIds(salarySobStrIds)
                .salaryItemStrIds(salaryItemStrIds)
                .build();
        if (StringUtils.isNotBlank(salaryAcctRecordStrIds)) {
            queryParam.setSalaryAcctRecordIds(Arrays.stream(salaryAcctRecordStrIds.split(",")).map(Long::valueOf).collect(Collectors.toList()));
        }
        if (StringUtils.isNotBlank(salarySobStrIds)) {
            queryParam.setSalarySobIds(Arrays.stream(salarySobStrIds.split(",")).map(Long::valueOf).collect(Collectors.toList()));
        }
        if (StringUtils.isNotBlank(salaryItemStrIds)) {
            queryParam.setSalaryItemIds(Arrays.stream(salaryItemStrIds.split(",")).map(Long::valueOf).collect(Collectors.toList()));
        }
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryAcctSupplementParam, Map<String, Object>>(user).run(getSalaryAcctManager(user)::supplementAcctRecord, queryParam);
    }


    //---------------------------薪资核算 end ------------------------------------


    //---------------------------数据采集 往期累计情况 start ------------------------------------

    /**
     * 恢复所有已申报的往期累计数据
     *
     * @return
     */
    @GET
    @Path("/addupsituation/recover")
    @Produces(MediaType.APPLICATION_JSON)
    public String recoverAddUpSituationHistoryData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Map<String, Object>, Boolean>(user).run(getAddUpSituationManager(user)::recoverAddUpHistoryData);
    }
    //---------------------------数据采集 往期累计情况 end   ------------------------------------


    @GET
    @Path("/salaryArchive/initPayStartDate")
    @Produces(MediaType.APPLICATION_JSON)
    public String initPayDate(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Boolean>(user).run(getSalaryArchiveManager(user)::initPayStartDate);
    }

}
