package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.context.BaseContext;
import com.loan.system.domain.dto.DisbursementDTO;
import com.loan.system.domain.entity.Disbursement;
import com.loan.system.domain.vo.*;
import com.loan.system.repository.DisbursementRepository;
import com.loan.system.service.CustomerService;
import com.loan.system.service.DisbursementService;
import com.loan.system.service.LoanService;
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;
    private final CustomerService customerSrevice;
    private final LoanService loanService;

    @Autowired
    public DisbursementServiceImpl(DisbursementRepository disbursementRepository, CustomerService customerSrevice, LoanService loanService) {
        this.disbursementRepository = disbursementRepository;
        this.customerSrevice = customerSrevice;
        this.loanService = loanService;
    }
    @Override
    public DisbursementVO addDisbursement(DisbursementDTO disbursementDTO) {
        Disbursement disbursement = BeanUtil.copyProperties(disbursementDTO, Disbursement.class);
        disbursement.setApplyBy(BaseContext.getCurrentId());
        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);
    }

    @Override
    public DisbursementDetailVO getLoanCaseAndCustomerByCaseId(Long caseId) {
        LoanCaseSimpleVO loanCaseSimpleVO = loanService.findLoanCaseSimpleByIdAndIsDelete(caseId, false);
        CustomerVO customerVO = customerSrevice.findByCustomerIdAndIsDelete(loanCaseSimpleVO.getCustomerId(), false);

        DisbursementDetailVO disbursementDetailVO =new DisbursementDetailVO();
        disbursementDetailVO.setCustomer(customerVO);
        disbursementDetailVO.setLoanCase(loanCaseSimpleVO);
        return disbursementDetailVO;
    }

    @Override
    public Disbursement getDisbursementByCaseId(Long caseId) {
        return disbursementRepository.getDisbursementByCaseIdAndIsDelete(caseId, false);
    }

    @Override
    public void updateApprovalUserById(Long currentId,Long id) {
        disbursementRepository.updateApprovalUserById(currentId,id);
    }

    @Override
    public void updateOperationUserById(Long currentId, Long id) {
        disbursementRepository.updateOperationUserById(currentId,id);
    }

    @Override
    public void updateCommentsById(String comments, Long id) {
        disbursementRepository.updateCommentsById(comments,id);
    }
}
