package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.context.BaseContext;
import com.loan.system.domain.dto.DisbursementRecordDTO;
import com.loan.system.domain.entity.DisbursementRecord;
import com.loan.system.domain.vo.DisbursementRecordVO;
import com.loan.system.repository.DisbursementRecordRepository;
import com.loan.system.service.DisbursementRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class DisbursementRecordServiceImpl implements DisbursementRecordService {
    private final DisbursementRecordRepository  disbursementRecordRepository;

    @Autowired
    public DisbursementRecordServiceImpl(DisbursementRecordRepository disbursementRecordRepository) {
        this.disbursementRecordRepository = disbursementRecordRepository;
    }
    @Override
    public void addDisbursementRecord(DisbursementRecordDTO disbursementRecordDTO) {
        DisbursementRecord disbursementRecord = BeanUtil.copyProperties(disbursementRecordDTO, DisbursementRecord.class);
        disbursementRecord.setFinanceId(BaseContext.getCurrentId());
        disbursementRecord.setIsDelete(false);
        disbursementRecord.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        disbursementRecordRepository.save(disbursementRecord);
    }

    @Override
    public List<DisbursementRecordVO> findRecordById(Long id) {
        return BeanUtil.copyToList(disbursementRecordRepository.findByDisbursementIdAndIsDelete(id,false), DisbursementRecordVO.class);
    }
}
