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

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.pagehelper.page.PageMethod;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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.util.MyModelUtil;
import com.tourism.common.core.util.MyPageUtil;
import com.tourism.common.log.annotation.OperationLog;
import com.tourism.common.log.model.constant.SysOperationLogType;
import com.tourism.webadmin.back.dto.TourProjectGroupPurchaseApplyDto;
import com.tourism.webadmin.back.model.TourProjectGroupPurchaseApply;
import com.tourism.webadmin.back.service.TourProjectGroupPurchaseApplyService;
import com.tourism.webadmin.back.vo.TourProjectGroupPurchaseApplyVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 开团申请接口。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Tag(name = "开团申请接口")
@Slf4j
@RestController
@RequestMapping("/website/app/tourProjectGroupPurchaseApply")
public class WebsiteProjectGroupPurchaseApplyController {

    @Autowired
    private TourProjectGroupPurchaseApplyService tourProjectGroupPurchaseApplyService;

    /**
     * 新增开团申请列表数据。
     *
     * @param tourProjectGroupPurchaseApplyDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(@RequestBody TourProjectGroupPurchaseApplyDto tourProjectGroupPurchaseApplyDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourProjectGroupPurchaseApplyDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        tourProjectGroupPurchaseApplyDto.setTourUserId(TokenData.takeFromRequest().getUserId());
        tourProjectGroupPurchaseApplyDto.setApplyTime(new Date());
        TourProjectGroupPurchaseApply tourProjectGroupPurchaseApply = MyModelUtil.copyTo(tourProjectGroupPurchaseApplyDto, TourProjectGroupPurchaseApply.class);
        // 验证关联Id的数据合法性
        CallResult callResult = tourProjectGroupPurchaseApplyService.verifyRelatedData(tourProjectGroupPurchaseApply, null);
        if (!callResult.isSuccess()) {
            return ResponseResult.errorFrom(callResult);
        }
        tourProjectGroupPurchaseApply = tourProjectGroupPurchaseApplyService.saveNew(tourProjectGroupPurchaseApply);
        return ResponseResult.success(tourProjectGroupPurchaseApply.getId());
    }
}
