package com.docus.server.service.impl;

import com.docus.infrastructure.redis.service.IdService;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.convert.SchCollectErrorLogConvert;
import com.docus.server.dto.scheduling.management.schcollecterrorlog.AddSchCollectErrorLogDTO;
import com.docus.server.dto.scheduling.management.schcollecterrorlog.DeleteSchCollectErrorLogDTO;
import com.docus.server.dto.scheduling.management.schcollecterrorlog.EditSchCollectErrorLogDTO;
import com.docus.server.entity.scheduling.management.SchCollectErrorLog;
import com.docus.server.infrastructure.dao.ISchCollectErrorLogDao;
import com.docus.server.service.IFileUploadService;
import com.docus.server.service.ISchCollectErrorLogService;
import com.docus.server.vo.scheduling.management.schcollecterrorlog.SchCollectErrorLogVO;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采集器异常日志 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Service
public class SchCollectErrorLogServiceImpl implements ISchCollectErrorLogService {
    @Resource
    private ISchCollectErrorLogDao iSchCollectErrorLogDao;
    @Resource
    private IFileUploadService iFileUploadService;
    @Resource
    private IdService idService;

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public SchCollectErrorLogVO findById(String id) {
        return SchCollectErrorLogConvert.INSTANCE.convertVO(iSchCollectErrorLogDao.findById(id));
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<SchCollectErrorLogVO> search(SearchDTO searchDTO) {
        return SchCollectErrorLogConvert.INSTANCE.convertVO(iSchCollectErrorLogDao.search(searchDTO));
    }

    /**
     * 新增
     *
     * @param addSchCollectErrorLogDTO 编辑参数
     * @param multipartFiles           采集器异常任务截图
     * @return 成功或失败
     */
    @Override
    public boolean add(AddSchCollectErrorLogDTO addSchCollectErrorLogDTO, MultipartFile[] multipartFiles) throws Exception {

        List<UploadFileVO> uploadFileVOList = iFileUploadService.uploadFile(multipartFiles, "COLLECTOR_TASK_ERROR_LOG");

        SchCollectErrorLog schCollectErrorLog = SchCollectErrorLogConvert.INSTANCE.convertDO(addSchCollectErrorLogDTO);
        if (!CollectionUtils.isEmpty(uploadFileVOList)) {
            schCollectErrorLog.setScreenPath(uploadFileVOList.get(0).getFilePath());
        }
        schCollectErrorLog.setId(idService.getDateSeq());
        return iSchCollectErrorLogDao.add(schCollectErrorLog);
    }

    /**
     * 编辑
     *
     * @param editSchCollectErrorLogDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean edit(EditSchCollectErrorLogDTO editSchCollectErrorLogDTO) {
        SchCollectErrorLog schCollectErrorLog = SchCollectErrorLogConvert.INSTANCE.convertDO(editSchCollectErrorLogDTO);
        return iSchCollectErrorLogDao.edit(schCollectErrorLog);
    }

    /**
     * 批量删除
     *
     * @param deleteSchCollectErrorLogDTO 删除参数
     * @return 成功或失败
     */
    @Override
    public int delete(DeleteSchCollectErrorLogDTO deleteSchCollectErrorLogDTO) {
        return iSchCollectErrorLogDao.delete(deleteSchCollectErrorLogDTO.getIds());
    }
}

