package com.engine.salary.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.salary.common.SalaryContext;
import com.engine.salary.entity.siarchives.dto.InsuranceArchivesBaseHistoryDTO;
import com.engine.salary.entity.siarchives.param.InsuranceArchivesListParam;
import com.engine.salary.entity.siarchives.param.InsuranceArchivesSaveParam;
import com.engine.salary.entity.siarchives.param.SIArchiveBaseHistoryListParam;
import com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO;
import com.engine.salary.enums.sicategory.WelfareTypeEnum;
import com.engine.salary.service.SIArchivesService;
import com.engine.salary.service.impl.SIArchivesServiceImpl;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.page.PageInfo;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
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 java.util.Collection;
import java.util.Map;

/**
 * @Author weaver_cl
 * @Description:
 * @Date 2022/3/11
 * @Version V1.0
 **/
public class SIArchivesController  {

    private SIArchivesService getService(User user) {
        SalaryContext.get().setValue("user",user);
        return ServiceUtil.getService(SIArchivesServiceImpl.class,user);
    }


    /**
     * 提示信息
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/tips")
    @Produces(MediaType.APPLICATION_JSON)
    public String tips(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::getTips, ParamUtil.request2Map(request));
    }


    /**
     * 高级搜索
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::getSearchCondition, ParamUtil.request2Map(request));

    }



    /**
     * 查询档案基本信息表单
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getBaseForm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBaseForm(@Context HttpServletRequest request, @Context HttpServletResponse response,
                              @DefaultValue("") @QueryParam(value = "welfareTypeEnum") WelfareTypeEnum welfareTypeEnum) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("welfareTypeEnum",welfareTypeEnum);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::getBaseForm, map);
    }

    /**
     * 查询档案缴纳基数表单
     * @param request
     * @param response
     * @param welfareTypeEnum
     * @return
     */
    @GET
    @Path("/getPaymentForm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getPaymentForm(@Context HttpServletRequest request, @Context HttpServletResponse response,
                              @DefaultValue("") @QueryParam(value = "welfareTypeEnum") WelfareTypeEnum welfareTypeEnum) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("welfareTypeEnum",welfareTypeEnum);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::getPaymentForm, map);
    }

    /**
     * 保存档案
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String insert(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody InsuranceArchivesSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<InsuranceArchivesSaveParam,String>(user).run(getService(user)::insert, param);
    }

    /**
     * 查询档案列表
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/getTable")
    @Produces(MediaType.APPLICATION_JSON)
    public String getTable(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody InsuranceArchivesListParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<InsuranceArchivesListParam,Map<String,Object>>(user).run(getService(user)::listPage, param);
    }

    /**
     * 获取福利档案各tab总人数
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/queryInsuranceTabTotal")
    @Produces(MediaType.APPLICATION_JSON)
    public String queryInsuranceTabTotal(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Map<String, Long>>(user).run(getService(user)::queryInsuranceTabTotal);
    }

    /**
     * 待减员页面的删除待办
     */
    @POST
    @Path("/cancelStayDel")
    @Produces(MediaType.APPLICATION_JSON)
    public String cancelStayDel(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody InsuranceArchivesBaseInfoPO po) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<InsuranceArchivesBaseInfoPO, String>(user).run(getService(user)::cancelStayDel, po);
    }

    /**
     * 待增员页面的删除待办
     */
    @POST
    @Path("/updateRunStatus")
    @Produces(MediaType.APPLICATION_JSON)
    public String updateRunStatus(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody InsuranceArchivesBaseInfoPO po) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<InsuranceArchivesBaseInfoPO, String>(user).run(getService(user)::updateRunStatus, po);
    }

    /**
     * 批量取消停缴
     */
    @POST
    @Path("/cancelStopPayment")
    @Produces(MediaType.APPLICATION_JSON)
    public String cancelStopPayment(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Collection<Long> , String>(user).run(getService(user)::cancelStopPayment, ids);
    }

    /**
     * 批量减员
     */
    @POST
    @Path("/stayDelToStop")
    @Produces(MediaType.APPLICATION_JSON)
    public String stayDelToStop(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Collection<Long>, Map<String, Object>>(user).run(getService(user)::stayDelToStop, ids);
    }

    /**
     * 全量减员
     */
    @GET
    @Path("/allStayDelToStop")
    @Produces(MediaType.APPLICATION_JSON)
    public String allStayDelToStop(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Map<String, Object>>(user).run(getService(user)::allStayDelToStop);
    }

    /**
     * 批量增员
     */
    @POST
    @Path("/stayAddToPay")
    @Produces(MediaType.APPLICATION_JSON)
    public String stayAddToPay(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Collection<Long>, Map<String, Object>>(user).run(getService(user)::stayAddToPay, ids);
    }

    /**
     * 全量增员
     */
    @GET
    @Path("/allStayAddToPay")
    @Produces(MediaType.APPLICATION_JSON)
    public String allStayAddToPay(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Map<String, Object>>(user).run(getService(user)::allStayAddToPay);
    }

    /**
     * 删除档案
     */
    @POST
    @Path("/deleteArchive")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteArchive(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Collection<Long>, Map<String, Object>>(user).run(getService(user)::deleteArchive, ids);
    }

    /**
     * 查询非系统人员福利档案列表
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/getExtTable")
    @Produces(MediaType.APPLICATION_JSON)
    public String getExtTable(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody InsuranceArchivesListParam param) {
        User user = HrmUserVarify.getUser(request, response);
        param.setExtWelArchiveList(true);
        return new ResponseResult<InsuranceArchivesListParam,Map<String,Object>>(user).run(getService(user)::listPage, param);
    }

    /**
     * 查询档案基数调整记录列表
     */
    @POST
    @Path("/getAdjustHistoryList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAdjustHistoryList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SIArchiveBaseHistoryListParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SIArchiveBaseHistoryListParam, PageInfo<InsuranceArchivesBaseHistoryDTO>>(user).run(getService(user)::historyListByEmployeeIdAndOperator, param);
    }
}
