package com.engine.salary.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.sicategory.dto.ICategoryDTO;
import com.engine.salary.entity.sicategory.dto.ICategoryFormDTO;
import com.engine.salary.entity.sicategory.dto.ICategoryListDTO;
import com.engine.salary.entity.sicategory.param.UpdateStatusParam;
import com.engine.salary.entity.sischeme.param.InsuranceSchemeParam;
import com.engine.salary.service.SICategoryService;
import com.engine.salary.service.impl.SICategoryServiceImpl;
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.Map;

/**
 * @Author weaver_cl
 * @Description:  社保自定义福利控制器
 * @Date 2022/3/9
 * @Version V1.0
 **/
public class SICategoryController {

    private SICategoryService getService (User user) {
        return ServiceUtil.getService(SICategoryServiceImpl.class,user);
    }


    /**
     * 福利表单
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/customCategoryForm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getForm(@Context HttpServletRequest request, @Context HttpServletResponse response,
                               @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("id",id);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::getForm, map);
    }

    /**
     * 查询自定义福利列表
    */
    @POST
    @Path("/customCategoryList")
    @Produces(MediaType.APPLICATION_JSON)
    public String listPage(@Context HttpServletRequest request, @Context HttpServletResponse response,
                           @RequestBody InsuranceSchemeParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult< InsuranceSchemeParam, PageInfo<ICategoryListDTO>>(user).run(getService(user)::listPage, queryParam);
    }


    /**
     * 新增
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/createSICategory")
    @Produces(MediaType.APPLICATION_JSON)
    public String insert(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ICategoryFormDTO iCategoryFormDTO) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("iCategoryFormDTO",iCategoryFormDTO);
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::insert, map);
    }



    /**
     * @description 获取指定自定义福利信息
     * @return String
     * @author Harryxzy
     * @date 2022/10/14 11:34
     */
    @POST
    @Path("/getCustomCategoryByID")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCustomCategoryByID(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody ICategoryFormDTO iCategoryFormDTO) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult< Long, ICategoryDTO>(user).run(getService(user)::getByID, iCategoryFormDTO.getId());
    }


    /**
     * @description 编辑自定义福利名称
     * @return String
     * @author Harryxzy
     * @date 2022/10/14 11:37
     */
    @POST
    @Path("/updateCustomCategoryName")
    @Produces(MediaType.APPLICATION_JSON)
    public String updateCustomCategoryName(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody ICategoryFormDTO iCategoryFormDTO) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
//        return new ResponseResult< ICategoryFormDTO,  Map<String, Object>>(user).run(getService(user)::updateCategoryName, iCategoryFormDTO);
        return new ResponseResult< ICategoryFormDTO,  Map<String, Object>>(user).run(getService(user)::updateCategoryNameAndPayScope, iCategoryFormDTO);
    }


//    @POST
//    @Path("/updateCustomCategory")
//    @Produces(MediaType.APPLICATION_JSON)
//    public String update(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody ICategoryFormDTO iCategoryFormDTO) {
//        User user = HrmUserVarify.getUser(request, response);
//        Map<String, Object> map = ParamUtil.request2Map(request);
//        map.put("iCategoryFormDTO",iCategoryFormDTO);
//        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::update, map);
//    }


    /**
     * 删除自定义福利项
     * 必要条件：
     * 1-福利项未启用，
     * 2-福利核算数据中不能存在该福利项的核算数据
     */

    @POST
    @Path("/deleteCustomCategory")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteCustomCategory(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ICategoryFormDTO iCategoryFormDTO) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult< ICategoryFormDTO,  Map<String, Object>>(user).run(getService(user)::deleteCustomCategory, iCategoryFormDTO);
    }


    /**
     * 更新自定义福利的启用停用状态
     * @param request
     * @param response
     * @param updateStatusParam
     * @return
     */
    @POST
    @Path("/updateCustomCategoryStatus")
    @Produces(MediaType.APPLICATION_JSON)
    public String updateStatusById(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody UpdateStatusParam updateStatusParam) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        map.put("id",updateStatusParam.getId());
        map.put("isUse",updateStatusParam.getIsUse());
        return new ResponseResult< Map<String, Object>,  Map<String, Object>>(user).run(getService(user)::updateStatusById, map);
    }

}
