package com.docus.server.controller;

import cn.hutool.core.util.StrUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.web.exception.ApiException;
import com.docus.infrastructure.web.exception.ExceptionCode;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.api.scheduling.management.SchCollectorVersionFileApi;
import com.docus.server.dto.scheduling.management.schcollectorversionfile.AddSchCollectorVersionFileDTO;
import com.docus.server.dto.scheduling.management.schcollectorversionfile.DeleteSchCollectorVersionFileDTO;
import com.docus.server.dto.scheduling.management.schcollectorversionfile.EditSchCollectorVersionFileDTO;
import com.docus.server.entity.scheduling.management.SchCollectorVersion;
import com.docus.server.service.ISchCollectorVersionFileService;
import com.docus.server.service.ISchCollectorVersionService;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.SchCollectorVersionFileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 采集器版本列表更新包管理 控制器类
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Api(value = "采集器版本列表更新包管理接口", tags = "采集器版本列表更新包管理接口")
@RestController
@Slf4j
public class SchCollectorVersionFileController implements SchCollectorVersionFileApi {
    @Resource
    private ISchCollectorVersionFileService iSchCollectorVersionFileService;
    @Resource
    private ISchCollectorVersionService iSchCollectorVersionService;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @ApiOperation("按主键查询")
    @Override
    public SchCollectorVersionFileVO findById(String id) {
        return iSchCollectorVersionFileService.findById(id);
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @ApiOperation("关键字搜索")
    @Override
    public PageResult<SchCollectorVersionFileVO> search(SearchDTO searchDTO) {
        return iSchCollectorVersionFileService.search(searchDTO);
    }

    /**
     * 新增
     *
     * @param addDTO 编辑参数
     * @return 成功或失败
     */
    @ApiOperation("新增")
    @Override
    public boolean add(AddSchCollectorVersionFileDTO addDTO, MultipartFile[] multipartFiles) throws Exception {
        if (fileValid(multipartFiles, addDTO)) {
            throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "请选择文件和填写上传文件信息！");
        }

        SchCollectorVersion schCollectorVersion = iSchCollectorVersionService.findByVersion(addDTO.getCollectorId(), addDTO.getCollectorVersionNo());

        if (null != schCollectorVersion) {
            throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "当前采集器版本号已存在！");
        }

        iSchCollectorVersionFileService.addVersionAndFile(addDTO, multipartFiles);

        return true;
    }

    private boolean fileValid(MultipartFile[] multipartFiles, AddSchCollectorVersionFileDTO addSchCollectorVersionFileDTO) {
        return multipartFiles == null
            || multipartFiles.length == 0
            || StrUtil.isBlank(addSchCollectorVersionFileDTO.getCollectorVersionNo())
            || Func.isEmpty(addSchCollectorVersionFileDTO.getCollectorId());
    }

    public boolean add(AddSchCollectorVersionFileDTO addSchCollectorVersionFileDTO) {
        return iSchCollectorVersionFileService.add(addSchCollectorVersionFileDTO);
    }

    /**
     * 编辑
     *
     * @param editSchCollectorVersionFileDTO 编辑参数
     * @return 成功或失败
     */
    @ApiOperation("编辑")
    @Override
    public boolean edit(EditSchCollectorVersionFileDTO editSchCollectorVersionFileDTO) {
        return iSchCollectorVersionFileService.edit(editSchCollectorVersionFileDTO);
    }

    /**
     * 批量删除
     *
     * @param deleteSchCollectorVersionFileDTO 删除参数
     * @return 成功或失败
     */
    @ApiOperation("批量删除")
    @Override
    public int delete(DeleteSchCollectorVersionFileDTO deleteSchCollectorVersionFileDTO) {
        return iSchCollectorVersionFileService.delete(deleteSchCollectorVersionFileDTO);
    }
}
