package com.loan.system.service.Impl;

import com.loan.system.domain.entity.ContractAndCollateral;
import com.loan.system.repository.ContractAndCollateralRepository;
import com.loan.system.service.ContractAndCollateralService;
import org.springframework.stereotype.Service;

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

@Service
public class ContractAndCollateralServiceImpl implements ContractAndCollateralService {
    private final ContractAndCollateralRepository contractAndCollateralRepository;

    public ContractAndCollateralServiceImpl(ContractAndCollateralRepository contractAndCollateralRepository) {
        this.contractAndCollateralRepository = contractAndCollateralRepository;
    }
    @Override
    public void addContractAndCollateral(Long contractId, Long collateralId) {
        ContractAndCollateral contractAndCollateral = new ContractAndCollateral();
        contractAndCollateral.setContractId(contractId);
        contractAndCollateral.setCollateralId(collateralId);
        contractAndCollateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        contractAndCollateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        contractAndCollateral.setIsDelete(false);

        contractAndCollateralRepository.save(contractAndCollateral);
    }

    @Override
    public void deleteAllByCaseId(Long caseId) {
        contractAndCollateralRepository.deleteAllByCaseId(caseId);
    }

    @Override
    public List<ContractAndCollateral> findContractAndCollateralByCaseId(Long caseId) {
        return contractAndCollateralRepository.findByCaseIdAndIsDelete(caseId,false);
    }
}
