package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.dto.BizRecommenderDTO;
import com.loan.system.domain.entity.BizRecommender;
import com.loan.system.repository.RecommenderRepository;
import com.loan.system.service.RecommenderService;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class RecommenderServiceImpl implements RecommenderService {
    private final RecommenderRepository recommenderRepository;

    @Autowired
    public RecommenderServiceImpl(RecommenderRepository recommenderRepository) {
        this.recommenderRepository = recommenderRepository;
    }

    @Override
    public void addRecommender(BizRecommenderDTO bizRecommenderDTO) {
        BizRecommender bizRecommender = BeanUtil.copyProperties(bizRecommenderDTO, BizRecommender.class);
        bizRecommender.setIsDelete(false);
        bizRecommender.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        bizRecommender.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        recommenderRepository.save(bizRecommender);
    }

    @Override
    public List<BizRecommender> getRecommenders(Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        Page<BizRecommender> page = recommenderRepository.findAll(pageable);

        return page.getContent();
    }

    @Override
    public void updateRecommenderById(BizRecommenderDTO bizRecommenderDTO,Long  id) {
        recommenderRepository.updateRecommenderById(bizRecommenderDTO,id);
    }

    @Override
    public BizRecommender getRecommenderById(Long recommenderId) {
        return recommenderRepository.findByIdAndIsDelete( recommenderId,false);
    }


}
