package com.tourism.webadmin.app.website.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.tourism.common.core.annotation.MyRequestBody;
import com.tourism.common.core.constant.ErrorCodeEnum;
import com.tourism.common.core.object.*;
import com.tourism.common.core.util.MyCommonUtil;
import com.tourism.common.core.validator.UpdateGroup;
import com.tourism.webadmin.app.website.dto.TourismBookProjectDto;
import com.tourism.webadmin.app.website.dto.TourismProjectToWebDto;
import com.tourism.webadmin.app.website.service.TourismProjectToWebService;
import com.tourism.webadmin.app.website.vo.WebSiteProjectDatePriceVo;
import com.tourism.webadmin.back.model.*;
import com.tourism.webadmin.back.service.TourismProjectService;
import com.tourism.webadmin.back.vo.TourismProjectVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 旅游项目管理操作控制器类。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Tag(name = "门户旅游项目管理接口")
@Slf4j
@RestController
@RequestMapping("/website/tourism/project")
@Validated
public class TourismProjectToWebController {

    @Autowired
    private TourismProjectService tourismProjectService;

    @Autowired
    private TourismProjectToWebService tourismProjectToWebService;

    /**
     * 列出符合过滤条件的旅游项目管理列表。
     *
     * @param tourismProjectDtoFilter 过滤对象。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaIgnore
    @GetMapping("/list")
    public ResponseResult<MyPageData<TourismProjectVo>> list(
            @Valid @ModelAttribute TourismProjectToWebDto tourismProjectDtoFilter) {
        MyPageData<TourismProjectVo> tourismProjectVoMyPageData = tourismProjectToWebService.list(tourismProjectDtoFilter);
        return ResponseResult.success(tourismProjectVoMyPageData);
    }

    /**
     * 查看指定旅游项目管理对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaIgnore
    @GetMapping("/detail")
    public ResponseResult<TourismProjectVo> detail(@NotNull(message = "所属目录(id)不能为空") String id) {
        TourismProject tourismProject = tourismProjectService.getByIdWithRelation(id, MyRelationParam.full());
        if (tourismProject == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        TourismProjectVo tourismProjectVo = tourismProjectToWebService.detail(tourismProject);
        return ResponseResult.success(tourismProjectVo);
    }
    /**
     * 查看指定旅游项目管理对象的日历价格。
     *
     * @param projectId 项目id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaIgnore
    @GetMapping("/viewDatePrice")
    public ResponseResult<WebSiteProjectDatePriceVo> view(@RequestParam String projectId) {
        WebSiteProjectDatePriceVo webSiteProjectDatePriceVo = tourismProjectToWebService.view(projectId);
        return ResponseResult.success(webSiteProjectDatePriceVo);
    }

    /**
    * 项目详情页面的预定接口
    *
    * @param tourBookInfoDto 预定项目的Dto
    * @return 应答结果对象，包含对象详情。
    */
    @PostMapping("/bookProject")
    public ResponseResult<Integer> bookProject(HttpServletRequest request,@MyRequestBody TourismBookProjectDto tourBookInfoDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourBookInfoDto, Default.class, UpdateGroup.class);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        Boolean state = tourismProjectToWebService.bookProject(request, tourBookInfoDto);
        if(!state){
            return ResponseResult.success(2);
        }
        return ResponseResult.success(1);
    }

}
