package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.dto.ContractDTO;
import com.loan.system.domain.entity.Contract;
import com.loan.system.domain.vo.ContractVO;
import com.loan.system.repository.ContractRepository;
import com.loan.system.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

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

@Service
public class ContractServiceImpl implements ContractService {
    private  final ContractRepository contractRepository;

    @Autowired
    public ContractServiceImpl(ContractRepository contractRepository) {
        this.contractRepository = contractRepository;
    }
    @Override
    public Contract saveContract(ContractDTO contractDTO) {
        Contract contract = BeanUtil.copyProperties(contractDTO, Contract.class);
        contract.setIsDelete( false);
        contract.setSignedByCustomer(false);
        contract.setCreateTime( LocalDateTime.now()
                .format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" ) ) );
        contract.setUpdateTime( LocalDateTime.now()
                .format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" ) ) );
        return contractRepository.save( contract );
    }

    @Override
    public Contract findContractByCaseIdAndBusinessAttr(Long caseId, String businessAttr, boolean b) {
        return contractRepository.findByCaseIdAndBusinessAttrAndIsDelete(caseId,businessAttr,b);
    }

    @Override
    public void updateContractById(Long id, ContractDTO contractDTO, boolean isDelete) {
        Contract contract = BeanUtil.copyProperties(contractDTO, Contract.class);
        contract.setUpdateTime( LocalDateTime.now().format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" )));
        contractRepository.updateContractById(id,contract,isDelete);
    }

    @Override
    public List<ContractVO> findContractByCaseId(Long caseId) {
        List<Contract> contract = contractRepository.findByCaseIdAndIsDelete(caseId, false);
        return BeanUtil.copyToList(contract, ContractVO.class);
    }

    @Override
    public void updateContractById1(Long contractId, Long commitedId,Long signId) {
        Contract contract = new Contract();
        contract.setCommitedId(commitedId);
        contract.setSifnedId(signId);
        contract.setSignedByCustomer(true);
        contract.setSignedTime( LocalDateTime.now().format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" )));
        contract.setUpdateTime( LocalDateTime.now().format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" )));

        contractRepository.updateContractById(contractId,contract,false);
    }

    @Override
    public void updateIsPushById(Long contractId) {
        contractRepository.updateIsPushById(contractId);
    }

    @Override
    public List<ContractVO> findContractByCaseIdAndIsPush(Long caseId, boolean isPush) {
        return BeanUtil.copyToList(contractRepository.findByCaseIdAndIsPush(caseId,isPush, false), ContractVO.class);
    }

    @Override
    public void deleteAllByCaseId(Long caseId) {
        contractRepository.deleteAllByCaseId(caseId);
    }

    @Override
    public void updateContractNoById(Long id, String contractNo) {
        contractRepository.updateContractNoById(id,contractNo);
    }

}
