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

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.*;

import java.util.Objects;

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

    private final ISaasParkResourceSubscribeInfoService baseService;

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

    /**
     * 资源预定提交
     */
    @PostMapping("/submitResourceSubscribe")
    @ApiOperation(value = "资源预定", notes = "资源预定")
    public R<ResourceSubscribeBillVO> addResourceSubscribe(@Validated(GroupAction.InsertAction.class) @RequestBody ResourceSubscribeSubmitReqDTO reqDTO) {
        reqDTO.setUserDetailId(null);
        return R.data(baseService.submitResourceSubscribe(reqDTO));
    }

    /**
     * 无需付费
     */
    @PostMapping("/noPaySubscribe/{subscribeId}")
    @ApiOperation(value = "无需付费", notes = "无需付费")
    public R noPaySubscribe(@PathVariable Long subscribeId) {
        return R.status(baseService.noPaySubscribe(subscribeId));
    }

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