package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.push.dto.PushRecordDTO;
import com.engine.salary.entity.push.dto.PushRecordDetailDTO;
import com.engine.salary.entity.push.dto.PushSettingDTO;
import com.engine.salary.entity.push.dto.PushSettingItemDTO;
import com.engine.salary.entity.push.param.*;
import com.engine.salary.entity.push.po.PushSettingItemPO;
import com.engine.salary.entity.push.po.PushSettingPO;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.wrapper.PushWrapper;
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;

/**
 * 数据推送
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class PushController {

    private PushWrapper getPushWrapper(User user) {
        return ServiceUtil.getService(PushWrapper.class, user);
    }

    @POST
    @Path("/setting/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String settingList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PushSettingQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PushSettingQueryParam, PageInfo<PushSettingDTO>>(user).run(getPushWrapper(user)::settingList, param);
    }

    @POST
    @Path("/mode/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String modeList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Object>(user).run(getPushWrapper(user)::modeList);
    }

    @POST
    @Path("/setting/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveSetting(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PushSettingSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PushSettingSaveParam, PushSettingPO>(user).run(getPushWrapper(user)::save, param);
    }

    @GET
    @Path("/setting/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteSetting(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, String>(user).run(getPushWrapper(user)::delete, id);
    }

    @POST
    @Path("/item/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String itemList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PushSettingItemQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PushSettingItemQueryParam, PageInfo<PushSettingItemDTO>>(user).run(getPushWrapper(user)::itemList, param);
    }


    @POST
    @Path("/item/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PushSettingItemSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PushSettingItemSaveParam, PushSettingItemPO>(user).run(getPushWrapper(user)::saveItem, param);
    }

    @GET
    @Path("/item/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, String>(user).run(getPushWrapper(user)::deleteItem, id);
    }

    @POST
    @Path("/record/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String recordList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody RecordListQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<RecordListQueryParam,  PageInfo<PushRecordDTO>>(user).run(getPushWrapper(user)::recordList, param);
    }

    @POST
    @Path("/record/detail")
    @Produces(MediaType.APPLICATION_JSON)
    public String recordDetail(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody RecordDetailListQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<RecordDetailListQueryParam, PageInfo<PushRecordDetailDTO>>(user).run(getPushWrapper(user)::recordDetailList, param);
    }

    @POST
    @Path("/record/create")
    @Produces(MediaType.APPLICATION_JSON)
    public String createPushRecord(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreatePushParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<CreatePushParam, String>(user).run(getPushWrapper(user)::createPushRecord, param);
    }

    @POST
    @Path("/record/push")
    @Produces(MediaType.APPLICATION_JSON)
    public String push(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PushParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<PushParam, String>(user).run(getPushWrapper(user)::push, param);
    }

    @POST
    @Path("/record/withdraw")
    @Produces(MediaType.APPLICATION_JSON)
    public String push(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody WithdrawParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<WithdrawParam, String>(user).run(getPushWrapper(user)::withdraw, param);
    }
}
