package com.loan.system.service.Impl;

import com.loan.system.context.BaseContext;
import com.loan.system.domain.entity.Repayment;
import com.loan.system.repository.RepaymentRepository;
import com.loan.system.service.RepaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.Context;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class RepaymentServiceImpl implements RepaymentService {
    private final RepaymentRepository repaymentRepository;

    @Autowired
    public RepaymentServiceImpl(RepaymentRepository repaymentRepository) {
        this.repaymentRepository = repaymentRepository;
    }
    @Override
    public void addRepayment(Repayment repayment) {
        repayment.setIsCleared( false);
        repayment.setIsDelete(false);
        repayment.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        repayment.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        repaymentRepository.save(repayment);
    }

    @Override
    public Repayment findByCaseIdAndIsDelete(Long caseId, boolean isDelete) {
        return repaymentRepository.findByCaseIdAndIsDelete(caseId,isDelete);
    }

    @Override
    public void updateApprovalUserByCaseId(Long currentId, Long caseId) {
        repaymentRepository.updateApprovalUserByCaseId(currentId,caseId);
    }

    @Override
    public void updateIsClearedByCaseId(boolean isClear, Long caseId) {
        repaymentRepository.updateIsClearedByCaseId(isClear,caseId);
    }

    @Override
    public void updateIntersetsByCaseId(Double interest , Long caseId) {
        repaymentRepository.updateIntersetsByCaseId(interest, BaseContext.getCurrentId(),
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),caseId);
    }

    @Override
    public void updateRepayWayByCaseId(String repayWay, Long caseId) {
        repaymentRepository.updateRepayWayByCaseId(repayWay,caseId);
    }
}
