package com.yuchen.saas.park.service.controller.resource.client;

import com.thinkunion.park.service.common.constant.GroupAction;
import com.yuchen.saas.park.api.dto.resource.CancelSubscribeReqDTO;
import com.yuchen.saas.park.api.dto.resource.ResourceSubscribeInfoReqDTO;
import com.yuchen.saas.park.api.dto.resource.ResourceSubscribeSubmitReqDTO;
import com.yuchen.saas.park.api.vo.resource.ResourceSubscribeBillVO;
import com.yuchen.saas.park.api.vo.resource.ResourceSubscribeInfoVO;
import com.yuchen.saas.park.service.service.resource.ISaasParkResourceSubscribeInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 资源预定表接口
 *
 * @author zhouhanwei
 * @since 2024-10-17
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/clientSubscribe")
public class ResourceSubscribeClient {

    private final ISaasParkResourceSubscribeInfoService baseService;

    /**
     * 查询用户资源预定分页列表
     */
    @PostMapping("/findUserSubscribeInfoPage")
    @ApiOperation(value = "资源预定分页列表", notes = "资源预定分页列表")
    public R<NestPage<ResourceSubscribeInfoVO>> findResourceSubscribeInfoPage(@RequestBody ResourceSubscribeInfoReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        reqDTO.setUserDetailId(contextUser.getUserDetailId());
        reqDTO.setCustomerId(null);
        return R.data(baseService.findResourceSubscribeInfoPage(reqDTO));
    }

    /**
     * 用户提交预定
     */
    @PostMapping("/userSubmitSubscribe")
    @ApiOperation(value = "资源预定", notes = "资源预定")
    public R<ResourceSubscribeBillVO> addResourceSubscribe(@Validated(GroupAction.InsertAction.class) @RequestBody ResourceSubscribeSubmitReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        reqDTO.setUserDetailId(contextUser.getUserDetailId());
        return R.data(baseService.submitResourceSubscribe(reqDTO));
    }

    /**
     * 用户取消预约
     */
    @PostMapping("/userCancelSubscribe")
    @ApiOperation(value = "用户取消预约", notes = "用户取消预约")
    public R cancelSubscribe(@Validated @RequestBody CancelSubscribeReqDTO reqDTO) {
        reqDTO.setCancelType("1");
        return R.status(baseService.cancelSubscribe(reqDTO));
    }
}
