package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.context.BaseContext;
import com.loan.system.domain.dto.ApprovalDTO;
import com.loan.system.domain.dto.ApprovalRecordDTO;
import com.loan.system.domain.dto.RepaymentRecordDTO;
import com.loan.system.domain.entity.Repayment;
import com.loan.system.domain.entity.RepaymentRecord;
import com.loan.system.domain.vo.RepaymentRecordVO;
import com.loan.system.repository.RepaymentRecordRepository;
import com.loan.system.repository.RepaymentRepository;
import com.loan.system.service.RepaymentRecordService;
import io.swagger.annotations.Api;
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 RepaymentRecordServiceImpl implements RepaymentRecordService {
    private final RepaymentRecordRepository repaymentRecordRepository;

    @Autowired
    public RepaymentRecordServiceImpl(RepaymentRecordRepository repaymentRepository) {
        this.repaymentRecordRepository = repaymentRepository;
    }
    @Override
    public void addRepaymentRecord(RepaymentRecordDTO repaymentRecordDTO, Long id) {
        RepaymentRecord repaymentRecord = BeanUtil.copyProperties(repaymentRecordDTO, RepaymentRecord.class);
        repaymentRecord.setRepaymentId(id);
        repaymentRecord.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        repaymentRecord.setIsDelete( false);

        repaymentRecordRepository.save(repaymentRecord);
    }

    @Override
    public List<RepaymentRecordVO> findByRepaymentIdAndIsDelete(Long repaymentId, boolean b) {
        return BeanUtil.copyToList(repaymentRecordRepository.findByRepaymentIdAndIsDelete(repaymentId, b), RepaymentRecordVO.class);
    }

    @Override
    public void updateApprovalById(String comments, String decision, Long id) {
        repaymentRecordRepository.updateApprovalByRepaymentId(comments, decision, BaseContext.getCurrentId(),id);
    }

    @Override
    public void updateRepaymentRecordById(RepaymentRecordDTO repaymentRecordDTO, Long recordId) {
        repaymentRecordRepository.updateRepaymentRecordById(repaymentRecordDTO.getAmount(), repaymentRecordDTO.getRepayBank(), repaymentRecordDTO.getRepayLocation(), repaymentRecordDTO.getRepayAccount(), recordId);
    }

}
