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.SchCollectorVersionFileConvert;
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.entity.scheduling.management.SchCollectorVersionFile;
import com.docus.server.enums.CancelEnum;
import com.docus.server.infrastructure.dao.ISchCollectorVersionDao;
import com.docus.server.infrastructure.dao.ISchCollectorVersionFileDao;
import com.docus.server.service.IFileUploadService;
import com.docus.server.service.ISchCollectorVersionFileService;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.SchCollectorVersionFileVO;
import com.docus.server.vo.scheduling.management.schcollectorversionfile.UploadFileVO;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 采集器版本列表更新包管理 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Service
public class SchCollectorVersionFileServiceImpl implements ISchCollectorVersionFileService {
    @Resource
    private ISchCollectorVersionFileDao iSchCollectorVersionFileDao;
    @Resource
    private ISchCollectorVersionDao iSchCollectorVersionDao;
    @Resource
    private IdService idService;
    @Resource
    private IFileUploadService iFileUploadService;

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

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addVersionAndFile(AddSchCollectorVersionFileDTO addDTO, MultipartFile[] multipartFiles) throws Exception {
        List<UploadFileVO> uploadFileVOList = iFileUploadService.uploadFile(multipartFiles, null);

        SchCollectorVersion schCollectorVersion = new SchCollectorVersion();
        schCollectorVersion.setId(idService.getDateSeq());
        schCollectorVersion.setCollectorId(addDTO.getCollectorId());
        schCollectorVersion.setCollectVersion(addDTO.getCollectorVersionNo());
        iSchCollectorVersionDao.save(schCollectorVersion);

        List<SchCollectorVersionFile> schCollectorVersionFiles = Lists.newArrayList();

        uploadFileVOList.forEach(p -> {
            SchCollectorVersionFile schCollectorVersionFile = new SchCollectorVersionFile();
            schCollectorVersionFile.setId(idService.getDateSeq());
            schCollectorVersionFile.setCollectorId(addDTO.getCollectorId());
            schCollectorVersionFile.setCollectorVersionId(schCollectorVersion.getId());
            schCollectorVersionFile.setFileTitle(p.getFileTitle());
            schCollectorVersionFile.setFilePath(p.getFilePath());
            schCollectorVersionFile.setFileName(p.getFileName());
            schCollectorVersionFile.setFileSize(p.getFileSize());
            schCollectorVersionFile.setFileType(p.getFileType());
            schCollectorVersionFile.setState(CancelEnum.NO_CANCEL);
            schCollectorVersionFile.setProcessName(addDTO.getProcessName());
            schCollectorVersionFile.setDeployPath(addDTO.getDeployPath());
            schCollectorVersionFiles.add(schCollectorVersionFile);
        });

        iSchCollectorVersionFileDao.saveBatch(schCollectorVersionFiles, 500);
    }

    @Override
    public SchCollectorVersionFile findByCollectorIdAndVersionId(Long collectorId, Long collectorVersionId) {
        return iSchCollectorVersionFileDao.findByVersionIdAndCollectorId(collectorId, collectorVersionId);
    }

    /**
     * 新增
     *
     * @param addSchCollectorVersionFileDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean add(AddSchCollectorVersionFileDTO addSchCollectorVersionFileDTO) {
        SchCollectorVersionFile schCollectorVersionFile = SchCollectorVersionFileConvert.INSTANCE.convertDO(addSchCollectorVersionFileDTO);
        return iSchCollectorVersionFileDao.add(schCollectorVersionFile);
    }

    /**
     * 编辑
     *
     * @param editSchCollectorVersionFileDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean edit(EditSchCollectorVersionFileDTO editSchCollectorVersionFileDTO) {
        SchCollectorVersionFile schCollectorVersionFile = SchCollectorVersionFileConvert.INSTANCE.convertDO(editSchCollectorVersionFileDTO);
        return iSchCollectorVersionFileDao.edit(schCollectorVersionFile);
    }

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

