package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.dto.DisbursementDTO;
import com.loan.system.domain.entity.Disbursement;
import com.loan.system.domain.vo.DisbursementVO;
import com.loan.system.repository.DisbursementRepository;
import com.loan.system.service.DisbursementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class DisbursementServiceImpl implements DisbursementService {
    private final DisbursementRepository disbursementRepository;

    @Autowired
    public DisbursementServiceImpl(DisbursementRepository disbursementRepository) {
        this.disbursementRepository = disbursementRepository;
    }
    @Override
    public DisbursementVO addDisbursement(DisbursementDTO disbursementDTO) {
        Disbursement disbursement = BeanUtil.copyProperties(disbursementDTO, Disbursement.class);
        disbursement.setApplyAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        disbursement.setCreateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        disbursement.setUpdateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        disbursement.setIsDelete(false);

        return  BeanUtil.copyProperties(disbursementRepository.save(disbursement), DisbursementVO.class);
    }

    @Override
    public DisbursementVO findDisbursementDetailsById(Long caseId) {
        return BeanUtil.copyProperties(disbursementRepository.findDisbursementDetailsById(caseId), DisbursementVO.class);
    }
}
