package com.tourism.webadmin.back.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tourism.common.log.annotation.OperationLog;
import com.tourism.common.log.model.constant.SysOperationLogType;
import com.github.pagehelper.page.PageMethod;
import com.tourism.common.core.object.*;
import com.tourism.common.core.util.*;
import com.tourism.common.core.constant.*;
import com.tourism.common.core.annotation.MyRequestBody;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tourism.webadmin.back.dto.TourTourismSearchRecordsDto;
import com.tourism.webadmin.back.model.TourTourismProjectSearchRecords;
import com.tourism.webadmin.back.service.TourTourismProjectSearchRecordsService;
import com.tourism.webadmin.back.vo.TourTourismSearchRecordsVo;
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.*;

/**
 * 游记列表搜索操作控制器类。
 *
 * @author 奇幻之旅
 * @date 2024-09-06
 */
@Tag(name = "游记列表搜索管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/app/tourTourismSearchRecords")
public class TourTourismProjectSearchRecordsController {

    @Autowired
    private TourTourismProjectSearchRecordsService tourTourismSearchRecordsService;

    /**
     * 新增游记列表搜索数据。
     *
     * @param tourTourismSearchRecordsDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {
            "tourTourismSearchRecordsDto.id",
            "tourTourismSearchRecordsDto.searchResultCountStart",
            "tourTourismSearchRecordsDto.searchResultCountEnd",
            "tourTourismSearchRecordsDto.searchTimeStart",
            "tourTourismSearchRecordsDto.searchTimeEnd"})
    @SaCheckPermission("tourTourismSearchRecords.add")
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(@MyRequestBody TourTourismSearchRecordsDto tourTourismSearchRecordsDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourTourismSearchRecordsDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourTourismProjectSearchRecords tourTourismSearchRecords = MyModelUtil.copyTo(tourTourismSearchRecordsDto, TourTourismProjectSearchRecords.class);
        tourTourismSearchRecords = tourTourismSearchRecordsService.saveNew(tourTourismSearchRecords);
        return ResponseResult.success(tourTourismSearchRecords.getId());
    }

    /**
     * 更新游记列表搜索数据。
     *
     * @param tourTourismSearchRecordsDto 更新对象。
     * @return 应答结果对象。
     */
    @ApiOperationSupport(ignoreParameters = {
            "tourTourismSearchRecordsDto.searchResultCountStart",
            "tourTourismSearchRecordsDto.searchResultCountEnd",
            "tourTourismSearchRecordsDto.searchTimeStart",
            "tourTourismSearchRecordsDto.searchTimeEnd"})
    @SaCheckPermission("tourTourismSearchRecords.update")
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody TourTourismSearchRecordsDto tourTourismSearchRecordsDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourTourismSearchRecordsDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourTourismProjectSearchRecords tourTourismSearchRecords = MyModelUtil.copyTo(tourTourismSearchRecordsDto, TourTourismProjectSearchRecords.class);
        TourTourismProjectSearchRecords originalTourTourismSearchRecords = tourTourismSearchRecordsService.getById(tourTourismSearchRecords.getId());
        if (originalTourTourismSearchRecords == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!tourTourismSearchRecordsService.update(tourTourismSearchRecords, originalTourTourismSearchRecords)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 删除游记列表搜索数据。
     *
     * @param id 删除对象主键Id。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismSearchRecords.delete")
    @OperationLog(type = SysOperationLogType.DELETE)
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody Long id) {
        if (MyCommonUtil.existBlankArgument(id)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(id);
    }

    /**
     * 批量删除游记列表搜索数据。
     *
     * @param idList 待删除对象的主键Id列表。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourTourismSearchRecords.delete")
    @OperationLog(type = SysOperationLogType.DELETE_BATCH)
    @PostMapping("/deleteBatch")
    public ResponseResult<Void> deleteBatch(@MyRequestBody List<Long> idList) {
        if (MyCommonUtil.existBlankArgument(idList)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        for (Long id : idList) {
            ResponseResult<Void> responseResult = this.doDelete(id);
            if (!responseResult.isSuccess()) {
                return responseResult;
            }
        }
        return ResponseResult.success();
    }

    /**
     * 列出符合过滤条件的游记列表搜索列表。
     *
     * @param tourTourismSearchRecordsDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaCheckPermission("tourTourismSearchRecords.view")
    @PostMapping("/list")
    public ResponseResult<MyPageData<TourTourismSearchRecordsVo>> list(
            @MyRequestBody TourTourismSearchRecordsDto tourTourismSearchRecordsDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getCount());
        }
        TourTourismProjectSearchRecords tourTourismSearchRecordsFilter = MyModelUtil.copyTo(tourTourismSearchRecordsDtoFilter, TourTourismProjectSearchRecords.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourTourismProjectSearchRecords.class);
        List<TourTourismProjectSearchRecords> tourTourismSearchRecordsList =
                tourTourismSearchRecordsService.getTourTourismSearchRecordsListWithRelation(tourTourismSearchRecordsFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(tourTourismSearchRecordsList, TourTourismSearchRecordsVo.class));
    }

    /**
     * 查看指定游记列表搜索对象详情。
     *
     * @param id 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaCheckPermission("tourTourismSearchRecords.view")
    @GetMapping("/view")
    public ResponseResult<TourTourismSearchRecordsVo> view(@RequestParam Long id) {
        TourTourismProjectSearchRecords tourTourismSearchRecords = tourTourismSearchRecordsService.getByIdWithRelation(id, MyRelationParam.full());
        if (tourTourismSearchRecords == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        TourTourismSearchRecordsVo tourTourismSearchRecordsVo = MyModelUtil.copyTo(tourTourismSearchRecords, TourTourismSearchRecordsVo.class);
        return ResponseResult.success(tourTourismSearchRecordsVo);
    }

    private ResponseResult<Void> doDelete(Long id) {
        String errorMessage;
        // 验证关联Id的数据合法性
        TourTourismProjectSearchRecords originalTourTourismSearchRecords = tourTourismSearchRecordsService.getById(id);
        if (originalTourTourismSearchRecords == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [对象] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!tourTourismSearchRecordsService.remove(id)) {
            errorMessage = "数据操作失败，删除的对象不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }
}
