package com.yice.webadmin.app.controller;

import com.yice.common.log.annotation.OperationLog;
import com.yice.common.log.model.constant.SysOperationLogType;
import com.github.pagehelper.page.PageMethod;
import com.yice.webadmin.app.vo.*;
import com.yice.webadmin.app.dto.*;
import com.yice.webadmin.app.model.*;
import com.yice.webadmin.app.service.*;
import com.yice.common.core.object.*;
import com.yice.common.core.util.*;
import com.yice.common.core.constant.*;
import com.yice.common.core.annotation.MyRequestBody;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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.*;

/**
 * 模型部署操作控制器类。
 *
 * @author linking
 * @date 2023-04-13
 */
@Api(tags = "模型部署管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/app/modelDeploy")
public class ModelDeployController {

    @Autowired
    private ModelDeployService modelDeployService;

    /**
     * 新增模型部署数据。
     *
     * @param modelDeployDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"modelDeployDto.deployId"})
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(@MyRequestBody ModelDeployDto modelDeployDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(modelDeployDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        ModelDeploy modelDeploy = MyModelUtil.copyTo(modelDeployDto, ModelDeploy.class);
        modelDeploy = modelDeployService.saveNew(modelDeploy);
        return ResponseResult.success(modelDeploy.getDeployId());
    }

    /**
     * 更新模型部署数据。
     *
     * @param modelDeployDto 更新对象。
     * @return 应答结果对象。
     */
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody ModelDeployDto modelDeployDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(modelDeployDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        ModelDeploy modelDeploy = MyModelUtil.copyTo(modelDeployDto, ModelDeploy.class);
        ModelDeploy originalModelDeploy = modelDeployService.getById(modelDeploy.getDeployId());
        if (originalModelDeploy == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!modelDeployService.update(modelDeploy, originalModelDeploy)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 删除模型部署数据。
     *
     * @param deployId 删除对象主键Id。
     * @return 应答结果对象。
     */
    @OperationLog(type = SysOperationLogType.DELETE)
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody Long deployId) {
        if (MyCommonUtil.existBlankArgument(deployId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(deployId);
    }

    /**
     * 列出符合过滤条件的模型部署列表。
     *
     * @param modelDeployDtoFilter 过滤对象。
     * @param orderParam 排序参数。
     * @param pageParam 分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/list")
    public ResponseResult<MyPageData<ModelDeployVo>> list(
            @MyRequestBody ModelDeployDto modelDeployDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        ModelDeploy modelDeployFilter = MyModelUtil.copyTo(modelDeployDtoFilter, ModelDeploy.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, ModelDeploy.class);
        List<ModelDeploy> modelDeployList =
                modelDeployService.getModelDeployListWithRelation(modelDeployFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(modelDeployList, ModelDeploy.INSTANCE));
    }

    /**
     * 查看指定模型部署对象详情。
     *
     * @param deployId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @GetMapping("/view")
    public ResponseResult<ModelDeployVo> view(@RequestParam Long deployId) {
        ModelDeploy modelDeploy = modelDeployService.getByIdWithRelation(deployId, MyRelationParam.full());
        if (modelDeploy == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        ModelDeployVo modelDeployVo = ModelDeploy.INSTANCE.fromModel(modelDeploy);
        return ResponseResult.success(modelDeployVo);
    }

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