package com.engine.salary.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.taxrate.param.TaxRateSaveParam;
import com.engine.salary.service.TaxRateBaseService;
import com.engine.salary.service.impl.TaxRateBaseServiceImpl;
import com.engine.salary.util.ResponseResult;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import weaver.general.BaseBean;
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.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.Collection;
import java.util.Map;

public class TaxRateController {

    private BaseBean logger = new BaseBean();

    private TaxRateBaseService getService(User user) {
        return (TaxRateBaseService) ServiceUtil.getService(TaxRateBaseServiceImpl.class, user);
    }


    //税率表列表
    @GET
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String list(@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)::listPage,ParamUtil.request2Map(request));
    }

    //税率表列表
    @GET
    @Path("/getForm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getForm(@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)::getForm,ParamUtil.request2Map(request));
    }


    /**
     * 新建税率表
     */
    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String save(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody TaxRateSaveParam taxRateSaveParam) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("taxRateSaveParam",taxRateSaveParam);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::save,map);
    }

    /**
     * 编辑税率表
     */
    @POST
    @Path("/update")
    @Produces(MediaType.APPLICATION_JSON)
    public String update(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody TaxRateSaveParam taxRateSaveParam) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("taxRateSaveParam",taxRateSaveParam);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::update,map);
    }

    /**
     * 删除税率表
     */
    @POST
    @Path("/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteTaxRate(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("ids",ids);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::delete,map);
    }

}
