package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.dto.CustomersOtherDTO;
import com.loan.system.domain.dto.LoanCaseDTO;
import com.loan.system.domain.entity.*;
import com.loan.system.domain.vo.*;
import com.loan.system.repository.CustomerOtherRepository;
import com.loan.system.repository.CustomerRepository;
import com.loan.system.repository.LoanRepository;
import com.loan.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class LoanServiceImpl implements LoanService {

    private final CustomerOtherService customerOtherService;
    private final LoanRepository loanRepository;
    private final CustomerService customerService;
    private final DocumentService documentService;
    private final CollateralService collateralService;
    private final ContractService contractService;
    private final StepService stepService;
    private final ContractAndCollateralService contractAndCollateralService;

    @Autowired
    public LoanServiceImpl(LoanRepository loanRepository, CustomerOtherService customerOtherService,CustomerService customerRepository ,
                           DocumentService documentService, CollateralService collateralService, ContractService contractService,
                           StepService stepService, ContractAndCollateralService contractAndCollateralService) {
        this.loanRepository = loanRepository;
        this.customerOtherService = customerOtherService;
        this.customerService = customerRepository;
        this.documentService = documentService;
        this.collateralService = collateralService;
        this.contractService = contractService;
        this.stepService = stepService;
        this.contractAndCollateralService = contractAndCollateralService;
    }

    @Override
    public List<LoanCaseVO> findLoanCaseByIsComplete(Boolean isComplete, boolean isDelete) {
        return BeanUtil.copyToList(loanRepository.findByIsCompleteAndIsDelete(isComplete,isDelete), LoanCaseVO.class);
    }

    @Override
    public LoanCaseVO addLoanCaseByCustomerId(Long customerId) {
        LoanCase loanCase = new LoanCase();
        loanCase.setCustomerId(customerId);
        loanCase.setIsComplete(false);
        loanCase.setCreateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        loanCase.setUpdateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        loanCase.setIsDelete(false);

        return BeanUtil.copyProperties(loanRepository.save(loanCase), LoanCaseVO.class);
    }

    @Override
    public void updateLoanCaseById(LoanCaseDTO loanCaseDTO, Long caseId) {
        LoanCase loanCase = BeanUtil.copyProperties(loanCaseDTO, LoanCase.class);

        loanCase.setIsComplete(false);
        loanCase.setUpdateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        loanRepository.updateLoanCaseById(loanCase,caseId);
    }

    @Override
    public LoanCaseVO findLoanCaseDetailsById(Long caseId) {
        //1.获取业务信息
        LoanCase loanCase = loanRepository.findLoanCaseById(caseId, false);
        LoanCaseVO loanCaseVO = BeanUtil.copyProperties(loanCase, LoanCaseVO.class);
        //2.获取客户信息
        loanCaseVO.setCustomer(customerService.findByCustomerIdAndIsDelete(loanCase.getCustomerId(),false));
        //3.获取合同信息
        List<ContractVO> contracts = contractService.findContractByCaseId(caseId);
        loanCaseVO.setContracts(contracts);
        //4.获取押品信息
        List<CollateralVO> collaterals = collateralService.findByCaseId(caseId);
        loanCaseVO.setCollateral(collaterals);
        //5.获取合同押品关联信息
        List<ContractAndCollateral> contractAndCollaterals = contractAndCollateralService.findContractAndCollateralByCaseId(caseId);
        List<Map<Long,List<Long>>> relations = new ArrayList<>();

        Map<Long,List<Long>> relation = new java.util.HashMap<>();
        List<Long> collateralIds = new ArrayList<>();
        for(int i=0 ,j=0; i < contractAndCollaterals.size() && j < contracts.size(); i++) {
            if (contractAndCollaterals.get(i).getContractId().equals(contracts.get(j).getId())) {
                collateralIds.add(contractAndCollaterals.get(i).getCollateralId());
            }else{
                relation.put(contracts.get(j).getId(),collateralIds);
                relations.add(relation);
                j++;
                relation = new java.util.HashMap<>();
                collateralIds = new ArrayList<>();
                i--;
            }
        }
        loanCaseVO.setCollateralAndContract(relations);

        //5.获取其它客户信息
        List<CustomersOtherVO> customersOtherVOS = customerOtherService.findByCaseId(caseId);
        for (int i =0;i< customersOtherVOS.size();i++){
            if(i==0)
                loanCaseVO.setCustomers1(customersOtherVOS.get(i));
            else
                loanCaseVO.setCustomers2(customersOtherVOS.get(i));
        }
        //6.获取附件信息
        loanCaseVO.setDocuments(BeanUtil.copyToList(documentService.findByCaseId(caseId), DocumentVO.class));
//        //7.获取步骤信息
//        loanCaseVO.setSteps(stepService.getStepByCaseId(caseId));

        return loanCaseVO;
    }

    @Override
    public LoanCaseVO findLoanCaseByIdAndIsDelete(Long caseId, boolean isDelete) {
        return BeanUtil.copyProperties(loanRepository.findLoanCaseById(caseId, isDelete), LoanCaseVO.class);
    }

    @Override
    public void updateUpdatetimeByIdAndIsDeleted(Long id,boolean isDelete){
        loanRepository.updateUpdatetimeByIdAndIsDeleted(id,false, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
    @Override
    public void logic_delete(Long id){
        loanRepository.logic_delete(id, true, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}
