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.data.DatasetDataClean;
import com.yice.webadmin.app.data.DatasetDataDeduplicate;
import com.yice.webadmin.app.data.DatasetDataDesensitive;
import com.yice.webadmin.app.data.DatasetDataFilter;
import com.yice.webadmin.app.dto.DatasetCleanConfigDto;
import com.yice.webadmin.app.dto.DatasetDataCleanDto;
import com.yice.webadmin.app.dto.DatasetDataDesensitiveDto;
import com.yice.webadmin.app.model.DatasetCleanConfig;
import com.yice.webadmin.app.service.DatasetCleanConfigService;
import com.yice.webadmin.app.vo.DatasetCleanConfigVo;
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/datasetCleanConfig")
public class DatasetCleanConfigController {

    @Autowired
    private DatasetCleanConfigService datasetCleanConfigService;

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

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

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

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

    /**
     * 列出符合过滤条件的数据集异常清洗列表。
     *
     * @param cleanDtoFilter 过滤对象。
     * @param pageParam      分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/getCleanList")
    public ResponseResult<MyPageData<DatasetDataClean>> getCleanList(
            @MyRequestBody DatasetDataCleanDto cleanDtoFilter,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        DatasetDataClean filter = MyModelUtil.copyTo(cleanDtoFilter, DatasetDataClean.class);
        List<DatasetDataClean> datasetDataCleanList =
                datasetCleanConfigService.getDatasetCleanList(filter, pageParam);
        Long count = datasetCleanConfigService.getCleanCount(filter);
        return ResponseResult.success(MyPageUtil.makeResponseData(datasetDataCleanList,count));
    }

    /**
     * 列出符合过滤条件的数据集清洗过滤列表。
     *
     * @param filterDtoFilter 过滤对象。
     * @param pageParam              分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/getFilterList")
    public ResponseResult<MyPageData<DatasetDataFilter>> getFilterList(
            @MyRequestBody DatasetDataCleanDto filterDtoFilter,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        DatasetDataFilter filter = MyModelUtil.copyTo(filterDtoFilter, DatasetDataFilter.class);
        List<DatasetDataFilter> datasetDataFilterList =
                datasetCleanConfigService.getDatasetFilterList(filter, pageParam);
        Long count = datasetCleanConfigService.getFilterCount(filter);
        return ResponseResult.success(MyPageUtil.makeResponseData(datasetDataFilterList,count));
    }

    /**
     * 列出符合过滤条件的数据集清洗重复列表。
     *
     * @param deduplicateDtoFilter 过滤对象。
     * @param pageParam              分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/getDeduplicateList")
    public ResponseResult<MyPageData<DatasetDataDeduplicate>> getDeduplicateList(
            @MyRequestBody DatasetDataCleanDto deduplicateDtoFilter,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        DatasetDataDeduplicate filter = MyModelUtil.copyTo(deduplicateDtoFilter, DatasetDataDeduplicate.class);
        List<DatasetDataDeduplicate> datasetDataDeduplicateList =
                datasetCleanConfigService.getDatasetDeduplicateList(filter, pageParam);
        Long count = datasetCleanConfigService.getDeduplicateCount(filter);
        return ResponseResult.success(MyPageUtil.makeResponseData(datasetDataDeduplicateList,count));
    }

    /**
     * 列出符合过滤条件的数据集清洗重复列表。
     *
     * @param desensitiveDtoFilter 过滤对象。
     * @param pageParam              分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/getDesensitiveList")
    public ResponseResult<MyPageData<DatasetDataDesensitive>> getDesensitiveList(
            @MyRequestBody DatasetDataDesensitiveDto desensitiveDtoFilter,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        DatasetDataDesensitive filter = MyModelUtil.copyTo(desensitiveDtoFilter, DatasetDataDesensitive.class);
        List<DatasetDataDesensitive> datasetDataDesensitiveList =
                datasetCleanConfigService.getDatasetDesensitiveList(filter, pageParam);
        Long count = datasetCleanConfigService.getDesensitiveCount(filter);
        return ResponseResult.success(MyPageUtil.makeResponseData(datasetDataDesensitiveList,count));
    }

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

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