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.webadmin.back.vo.*;
import com.tourism.webadmin.back.dto.*;
import com.tourism.webadmin.back.model.*;
import com.tourism.webadmin.back.service.*;
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 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/tourUserCommentLikeRelation")
public class TourUserCommentLikeRelationController {

    @Autowired
    private TourUserCommentLikeRelationService tourUserCommentLikeRelationService;

    /**
     * 新增用户与评论的点赞关系表数据。
     *
     * @param tourUserCommentLikeRelationDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"tourUserCommentLikeRelationDto.userId"})
    @SaCheckPermission("tourUserCommentLikeRelation.add")
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(@MyRequestBody TourUserCommentLikeRelationDto tourUserCommentLikeRelationDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourUserCommentLikeRelationDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourUserCommentLikeRelation tourUserCommentLikeRelation = MyModelUtil.copyTo(tourUserCommentLikeRelationDto, TourUserCommentLikeRelation.class);
        tourUserCommentLikeRelation = tourUserCommentLikeRelationService.saveNew(tourUserCommentLikeRelation);
        return ResponseResult.success(tourUserCommentLikeRelation.getUserId());
    }

    /**
     * 更新用户与评论的点赞关系表数据。
     *
     * @param tourUserCommentLikeRelationDto 更新对象。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourUserCommentLikeRelation.update")
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody TourUserCommentLikeRelationDto tourUserCommentLikeRelationDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(tourUserCommentLikeRelationDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        TourUserCommentLikeRelation tourUserCommentLikeRelation = MyModelUtil.copyTo(tourUserCommentLikeRelationDto, TourUserCommentLikeRelation.class);
        TourUserCommentLikeRelation originalTourUserCommentLikeRelation = tourUserCommentLikeRelationService.getById(tourUserCommentLikeRelation.getUserId());
        if (originalTourUserCommentLikeRelation == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!tourUserCommentLikeRelationService.update(tourUserCommentLikeRelation, originalTourUserCommentLikeRelation)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 删除用户与评论的点赞关系表数据。
     *
     * @param userId 删除对象主键Id。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourUserCommentLikeRelation.delete")
    @OperationLog(type = SysOperationLogType.DELETE)
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody Long userId) {
        if (MyCommonUtil.existBlankArgument(userId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(userId);
    }

    /**
     * 批量删除用户与评论的点赞关系表数据。
     *
     * @param userIdList 待删除对象的主键Id列表。
     * @return 应答结果对象。
     */
    @SaCheckPermission("tourUserCommentLikeRelation.delete")
    @OperationLog(type = SysOperationLogType.DELETE_BATCH)
    @PostMapping("/deleteBatch")
    public ResponseResult<Void> deleteBatch(@MyRequestBody List<Long> userIdList) {
        if (MyCommonUtil.existBlankArgument(userIdList)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        for (Long userId : userIdList) {
            ResponseResult<Void> responseResult = this.doDelete(userId);
            if (!responseResult.isSuccess()) {
                return responseResult;
            }
        }
        return ResponseResult.success();
    }

    /**
     * 列出符合过滤条件的用户与评论的点赞关系表列表。
     *
     * @param tourUserCommentLikeRelationDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @SaCheckPermission("tourUserCommentLikeRelation.view")
    @PostMapping("/list")
    public ResponseResult<MyPageData<TourUserCommentLikeRelationVo>> list(
            @MyRequestBody TourUserCommentLikeRelationDto tourUserCommentLikeRelationDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getCount());
        }
        TourUserCommentLikeRelation tourUserCommentLikeRelationFilter = MyModelUtil.copyTo(tourUserCommentLikeRelationDtoFilter, TourUserCommentLikeRelation.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, TourUserCommentLikeRelation.class);
        List<TourUserCommentLikeRelation> tourUserCommentLikeRelationList =
                tourUserCommentLikeRelationService.getTourUserCommentLikeRelationListWithRelation(tourUserCommentLikeRelationFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(tourUserCommentLikeRelationList, TourUserCommentLikeRelationVo.class));
    }

    /**
     * 查看指定用户与评论的点赞关系表对象详情。
     *
     * @param userId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @SaCheckPermission("tourUserCommentLikeRelation.view")
    @GetMapping("/view")
    public ResponseResult<TourUserCommentLikeRelationVo> view(@RequestParam Long userId) {
        TourUserCommentLikeRelation tourUserCommentLikeRelation = tourUserCommentLikeRelationService.getByIdWithRelation(userId, MyRelationParam.full());
        if (tourUserCommentLikeRelation == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        TourUserCommentLikeRelationVo tourUserCommentLikeRelationVo = MyModelUtil.copyTo(tourUserCommentLikeRelation, TourUserCommentLikeRelationVo.class);
        return ResponseResult.success(tourUserCommentLikeRelationVo);
    }

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