package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.dto.DocumentDTO;
import com.loan.system.domain.entity.Document;
import com.loan.system.domain.vo.DocumentVO;
import com.loan.system.repository.DocumentRepository;
import com.loan.system.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class DocumentServiceImpl implements DocumentService {
    private final DocumentRepository documentRepository;

    @Autowired
    public DocumentServiceImpl(DocumentRepository documentRepository) {
        this.documentRepository = documentRepository;
    }
    @Override
    public List<DocumentVO> findByCaseId(Long caseId) {
        return BeanUtil.copyToList(documentRepository.findByCaseId(caseId), DocumentVO.class);
    }

    @Override
    public DocumentVO uploadFile(Document document) {
        return BeanUtil.copyProperties(documentRepository.save(document), DocumentVO.class);
    }

    @Override
    public Document findById(Long signId) {
        return documentRepository.findByDocumentId(signId);
    }

    @Override
    public void updateDocumentByCaseIdAndDicType(Long caseId, String dictType, DocumentDTO documentDTO) {
        Document document = BeanUtil.copyProperties(documentDTO, Document.class);
        document.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        documentRepository.updateByCaseIdAndDictType(caseId, dictType, document);
    }

    @Override
    public void deleteFileByCaseIdAndDictType(Long caseId, String fileType) {
        documentRepository.deleteFileByCaseIdAndDictType(caseId, fileType);
    }
}
