package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryitem.dto.SalaryItemFormDTO;
import com.engine.salary.entity.salaryitem.dto.SalaryItemListDTO;
import com.engine.salary.entity.salaryitem.param.SalaryItemSaveParam;
import com.engine.salary.entity.salaryitem.param.SalaryItemSearchParam;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.wrapper.SalaryFieldWrapper;
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.List;

/**
 * 薪资字段管理
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryFieldController {

    private SalaryFieldWrapper getSalaryFieldWrapper(User user) {
        return ServiceUtil.getService(SalaryFieldWrapper.class, user);
    }

    /**
     * 字段管理列表
     * @see
     */
    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String listSalaryItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryItemSearchParam searchParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryItemSearchParam, PageInfo<SalaryItemListDTO>>(user).run(getSalaryFieldWrapper(user)::listPage, searchParam);
    }

    /**
     * 字段管理列表保存顺序
     * @see
     */
    @POST
    @Path("/saveSortedIndex")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveSortedIndex(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody List<SalaryItemPO> saveList) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<SalaryItemPO>, String>(user).run(getSalaryFieldWrapper(user)::saveSortedIndex, saveList);
    }


    /**
     * 字段管理详情
     * @see
     */
    @GET
    @Path("/getSalaryForm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSalaryItemForm(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, SalaryItemFormDTO>(user).run(getSalaryFieldWrapper(user)::getForm, id);
    }

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

    /**
     * 保存字段管理
     * @see
     */
    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveSalaryItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryItemSaveParam saveParam) {
        User user = HrmUserVarify.getUser(request, response);
        if (saveParam.getId() == null || saveParam.getId() <= 0) {
            return new ResponseResult<SalaryItemSaveParam, String>(user).run(getSalaryFieldWrapper(user)::save, saveParam);
        } else {
            return new ResponseResult<SalaryItemSaveParam, String>(user).run(getSalaryFieldWrapper(user)::update, saveParam);
        }
    }
}
