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.DatasetCleanDto;
import com.yice.webadmin.app.model.DatasetClean;
import com.yice.webadmin.app.service.DatasetCleanService;
import com.yice.webadmin.app.vo.DatasetCleanVo;
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/datasetClean")
public class DatasetCleanController {

    @Autowired
    private DatasetCleanService datasetCleanService;

    /**
     * 新增数据集清洗数据。
     *
     * @param datasetCleanDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"datasetCleanDto.outputId"})
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<DatasetClean> add(@MyRequestBody DatasetCleanDto datasetCleanDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(datasetCleanDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        DatasetClean datasetClean = MyModelUtil.copyTo(datasetCleanDto, DatasetClean.class);
        datasetClean = datasetCleanService.saveNew(datasetClean);
        return ResponseResult.success(datasetClean);
    }

    /**
     * 新增数据集清洗数据。
     *
     * @param datasetCleanDto 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"datasetCleanDto.cleanId"})
    @OperationLog(type = SysOperationLogType.START_CLEAN)
    @PostMapping("/startClean")
    public ResponseResult<DatasetClean> startClean(@MyRequestBody DatasetCleanDto datasetCleanDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(datasetCleanDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        DatasetClean datasetClean = MyModelUtil.copyTo(datasetCleanDto, DatasetClean.class);
        datasetClean = datasetCleanService.addNew(datasetClean);
        return ResponseResult.success(datasetClean);
    }

    /**
     * 停止数据集清洗数据。
     *
     * @param cleanId 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @OperationLog(type = SysOperationLogType.STOP_CLEAN)
    @GetMapping("/stopClean")
    public ResponseResult<Void> stopClean(@RequestParam Long cleanId) {
        if (MyCommonUtil.existBlankArgument(cleanId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        datasetCleanService.stopCleanTask(cleanId);
        return ResponseResult.success();
    }

    /**
     * 重启数据集清洗数据。
     *
     * @param cleanId 新增对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @OperationLog(type = SysOperationLogType.RESTART_CLEAN)
    @GetMapping("/restartClean")
    public ResponseResult<Void> restartClean(@RequestParam Long cleanId) {
        if (MyCommonUtil.existBlankArgument(cleanId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        datasetCleanService.restartCleanTask(cleanId);
        return ResponseResult.success();
    }

    /**
     * 更新数据集清洗数据。
     *
     * @param datasetCleanDto 更新对象。
     * @return 应答结果对象。
     */
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Void> update(@MyRequestBody DatasetCleanDto datasetCleanDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(datasetCleanDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        DatasetClean datasetClean = MyModelUtil.copyTo(datasetCleanDto, DatasetClean.class);
        DatasetClean originaldatasetClean = datasetCleanService.getById(datasetClean.getCleanId());
        if (originaldatasetClean == null) {
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!datasetCleanService.update(datasetClean, originaldatasetClean)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

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

    /**
     * 列出符合过滤条件的数据集清洗列表。
     *
     * @param datasetCleanDtoFilter 过滤对象。
     * @param orderParam             排序参数。
     * @param pageParam              分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/list")
    public ResponseResult<MyPageData<DatasetCleanVo>> list(
            @MyRequestBody DatasetCleanDto datasetCleanDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        DatasetClean datasetCleanFilter = MyModelUtil.copyTo(datasetCleanDtoFilter, DatasetClean.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, DatasetClean.class);
        List<DatasetClean> datasetCleanList =
                datasetCleanService.getDatasetCleanListWithRelation(datasetCleanFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(datasetCleanList, DatasetClean.INSTANCE));
    }

    /**
     * 查看指定数据集清洗对象清洗。
     *
     * @param cleanId 指定对象主键Id。
     * @return 应答结果对象，包含对象清洗。
     */
    @GetMapping("/view")
    public ResponseResult<DatasetCleanVo> view(@RequestParam Long cleanId) {
        DatasetClean datasetClean = datasetCleanService.getByIdWithRelation(cleanId, MyRelationParam.full());
        if (datasetClean == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        DatasetCleanVo datasetCleanVo = DatasetClean.INSTANCE.fromModel(datasetClean);
        return ResponseResult.success(datasetCleanVo);
    }

    private ResponseResult<Void> doDelete(Long cleanId) {
        String errorMessage;
        // 验证关联Id的数据合法性
        DatasetClean originaldatasetClean = datasetCleanService.getById(cleanId);
        if (originaldatasetClean == null) {
            errorMessage = "数据验证失败，当前 [对象] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!datasetCleanService.remove(cleanId)) {
            errorMessage = "数据操作失败，删除的对象不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }
}
