package com.vetti.hotake.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.hotake.domain.HotakeSysFile;
import com.vetti.hotake.domain.dto.CandidateComplianceDto;
import com.vetti.hotake.service.IHotakeSysFileService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.vetti.hotake.mapper.HotakeCandidateComplianceMapper;
import com.vetti.hotake.domain.HotakeCandidateCompliance;
import com.vetti.hotake.service.IHotakeCandidateComplianceService;

@SuppressWarnings("all")
@Service
public class HotakeCandidateComplianceServiceImpl extends BaseServiceImpl implements IHotakeCandidateComplianceService {
    private static final String MINIO_BUCKET_NAME = "compliance-fs";

    @Autowired
    private HotakeCandidateComplianceMapper hotakeCandidateComplianceMapper;

    @Autowired
    private IHotakeSysFileService hotakeSysFileService;

    @Transactional(readOnly = true)
    @Override
    public CandidateComplianceDto selectHotakeCandidateComplianceById(Long id) {
        HotakeCandidateCompliance data = hotakeCandidateComplianceMapper.selectHotakeCandidateComplianceById(id);
        if (data.getFileId() != null) {
            return CandidateComplianceDto.build(data, hotakeSysFileService.url(data.getFileId()));
        }
        return CandidateComplianceDto.build(data, "");
    }

    @Transactional(readOnly = true)
    @Override
    public List<CandidateComplianceDto> selectHotakeCandidateComplianceList(HotakeCandidateCompliance hotakeCandidateCompliance) {
        List<CandidateComplianceDto> dtoList = new ArrayList<>();
        List<HotakeCandidateCompliance> list = hotakeCandidateComplianceMapper.selectHotakeCandidateComplianceList(hotakeCandidateCompliance);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(e -> {
                if (e.getFileId() != null) {
                    dtoList.add(CandidateComplianceDto.build(e, hotakeSysFileService.url(e.getFileId())));
                }
            });
        }
        return dtoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertHotakeCandidateCompliance(MultipartFile file, HotakeCandidateCompliance hotakeCandidateCompliance) {
        if (file != null) {
            HotakeSysFile sysFile = hotakeSysFileService.uploadFile(file, MINIO_BUCKET_NAME);
            hotakeCandidateCompliance.setFileId(sysFile.getId());
        }
        fill(FillTypeEnum.INSERT.getCode(), hotakeCandidateCompliance);
        return hotakeCandidateComplianceMapper.insertHotakeCandidateCompliance(hotakeCandidateCompliance);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateHotakeCandidateCompliance(MultipartFile file, HotakeCandidateCompliance hotakeCandidateCompliance) {
        if (file != null) {
            HotakeSysFile sysFile = hotakeSysFileService.uploadFile(file, MINIO_BUCKET_NAME);
            hotakeCandidateCompliance.setFileId(sysFile.getId());
        }
        fill(FillTypeEnum.UPDATE.getCode(), hotakeCandidateCompliance);
        return hotakeCandidateComplianceMapper.updateHotakeCandidateCompliance(hotakeCandidateCompliance);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeCandidateComplianceByIds(Long[] ids) {
        return hotakeCandidateComplianceMapper.deleteHotakeCandidateComplianceByIds(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeCandidateComplianceById(Long id) {
        return hotakeCandidateComplianceMapper.deleteHotakeCandidateComplianceById(id);
    }
}
