package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.entity.Customer;
import com.loan.system.domain.vo.CustomerVO;
import com.loan.system.repository.CustomerRepository;
import com.loan.system.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Override
    public Customer addCustomer(Customer customer) {
        // 可以在这里添加业务逻辑，如校验等
        return customerRepository.save(customer);
    }

    @Override
    public void updateMarriedStatusById(Long customerId, String marriedStatus) {
        customerRepository.updateMarriedStatusById(customerId,marriedStatus);
    }

    @Override
    public CustomerVO findByCustomerIdAndIsDelete(Long customerId, Boolean isDelete) {

        return BeanUtil.copyProperties(customerRepository.findByCustomerId(customerId,isDelete),CustomerVO.class);
    }

    @Override
    public List<CustomerVO> getAllCustomers(Integer pageNum,Integer pageSize,boolean isDelete) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        Page<Customer> customerPage = customerRepository.findByIsDelete(isDelete,pageable);
        List<Customer> customerList = customerPage.getContent();
        return BeanUtil.copyToList(customerList,CustomerVO.class);
    }

    @Override
    public List<CustomerVO> getCustomerByKey(String key, boolean isDelete,Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        key ="%"+key+"%";
        Page<Customer> customerPage=customerRepository.findByKey(key,isDelete,pageable);
        return BeanUtil.copyToList(customerPage.getContent(),CustomerVO.class);
    }

    @Override
    public CustomerVO getCustomerByMobile(String tel) {
        return  BeanUtil.copyProperties(customerRepository.findByMobile(tel, false),CustomerVO.class);
    }

    @Override
    public boolean existsBymobileAndIsDelete(String phoneNumber){
        return customerRepository.existsByMobileAndIsDelete(phoneNumber,false);
    }
    @Override
    public Customer findBymobileAndIsDelete(String phoneNumber){
        return customerRepository.findByMobile(phoneNumber,false);
    }
    @Override
    public Customer findByIdAndIsDelete(Long id){
        return customerRepository.findByIdAndIsDelete(id,false);
    }

    @Override
    public boolean existsByIdAndIsDelete(Long customerId) {
        return customerRepository.existsByIdAndIsDelete(customerId,false);
    }
}