package com.yice.webadmin.app.controller;

import com.github.pagehelper.page.PageMethod;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yice.common.core.annotation.MyRequestBody;
import com.yice.common.core.constant.ErrorCodeEnum;
import com.yice.common.core.object.*;
import com.yice.common.core.util.MyCommonUtil;
import com.yice.common.core.util.MyModelUtil;
import com.yice.common.core.util.MyPageUtil;
import com.yice.common.log.annotation.OperationLog;
import com.yice.common.log.model.constant.SysOperationLogType;
import com.yice.webadmin.app.dto.PromptOptimizeBatchDto;
import com.yice.webadmin.app.model.PromptOptimizeBatch;
import com.yice.webadmin.app.service.PromptOptimizeBatchService;
import com.yice.webadmin.app.vo.PromptOptimizeBatchVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 提示词批量优化操作控制器类。
 *
 * @author linking
 * @date 2023-04-13
 */
@Api(tags = "提示词批量优化管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/app/promptOptimizeBatch")
public class PromptOptimizeBatchController {

    @Autowired
    private PromptOptimizeBatchService promptOptimizeBatchService;

    /**
     * 新增提示词批量优化数据。
     *
     * @param promptOptimizeBatchDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"promptOptimizeBatchDto.batchTaskId", "promptOptimizeBatchDto.searchString"})
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(@MyRequestBody PromptOptimizeBatchDto promptOptimizeBatchDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(promptOptimizeBatchDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        PromptOptimizeBatch promptOptimizeBatch = MyModelUtil.copyTo(promptOptimizeBatchDto, PromptOptimizeBatch.class);
        promptOptimizeBatch = promptOptimizeBatchService.saveNew(promptOptimizeBatch);
        return ResponseResult.success(promptOptimizeBatch.getBatchTaskId());
    }

    /**
     * 更新提示词批量优化数据。
     *
     * @param promptOptimizeBatchDto 更新对象。
     * @return 应答结果对象。
     */
    @ApiOperationSupport(ignoreParameters = {"promptOptimizeBatchDto.searchString"})
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody PromptOptimizeBatchDto promptOptimizeBatchDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(promptOptimizeBatchDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        PromptOptimizeBatch promptOptimizeBatch = MyModelUtil.copyTo(promptOptimizeBatchDto, PromptOptimizeBatch.class);
        PromptOptimizeBatch originalPromptOptimizeBatch = promptOptimizeBatchService.getById(promptOptimizeBatch.getBatchTaskId());
        if (originalPromptOptimizeBatch == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!promptOptimizeBatchService.update(promptOptimizeBatch, originalPromptOptimizeBatch)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 删除提示词批量优化数据。
     *
     * @param batchTaskId 删除对象主键Id。
     * @return 应答结果对象。
     */
    @OperationLog(type = SysOperationLogType.DELETE)
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody Long batchTaskId) {
        if (MyCommonUtil.existBlankArgument(batchTaskId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(batchTaskId);
    }

    /**
     * 列出符合过滤条件的提示词批量优化列表。
     *
     * @param promptOptimizeBatchDtoFilter 过滤对象。
     * @param orderParam                   排序参数。
     * @param pageParam                    分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/list")
    public ResponseResult<MyPageData<PromptOptimizeBatchVo>> list(
            @MyRequestBody PromptOptimizeBatchDto promptOptimizeBatchDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        PromptOptimizeBatch promptOptimizeBatchFilter = MyModelUtil.copyTo(promptOptimizeBatchDtoFilter, PromptOptimizeBatch.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, PromptOptimizeBatch.class);
        List<PromptOptimizeBatch> promptOptimizeBatchList =
                promptOptimizeBatchService.getPromptOptimizeBatchListWithRelation(promptOptimizeBatchFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(promptOptimizeBatchList, PromptOptimizeBatch.INSTANCE));
    }

    /**
     * 查看指定提示词批量优化对象详情。
     *
     * @param batchTaskId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @GetMapping("/view")
    public ResponseResult<PromptOptimizeBatchVo> view(@RequestParam Long batchTaskId) {
        PromptOptimizeBatch promptOptimizeBatch = promptOptimizeBatchService.getByIdWithRelation(batchTaskId, MyRelationParam.full());
        if (promptOptimizeBatch == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        PromptOptimizeBatchVo promptOptimizeBatchVo = PromptOptimizeBatch.INSTANCE.fromModel(promptOptimizeBatch);
        return ResponseResult.success(promptOptimizeBatchVo);
    }

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