package com.gyee.wisdom.dao.sql.topic;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.topic.Topic;
import com.gyee.wisdom.dao.sql.topic.entity.PointEntity;
import com.gyee.wisdom.dao.sql.topic.entity.TopicEntity;
import com.gyee.wisdom.dao.sql.topic.repository.PointRepository;
import com.gyee.wisdom.dao.sql.topic.repository.TopicRepository;
import com.gyee.wisdom.dao.topic.ITopicDao;
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.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @descrition:
 * @author:Wanghs
 * @date:2018-04-25
 */
@Component
public class JpaTopicDao implements ITopicDao {

    @Autowired
    private TopicRepository topicRepository;

    @Autowired
    private PointRepository pointRepository;

    @Override
    public List<Topic> getAll() {
        List<TopicEntity> entityList = topicRepository.findAll();
        return DataUtil.convertDataList(entityList);
    }

    @Override
    public Topic getByTopic(String topicName) {
        TopicEntity entity = topicRepository.findByTopic(topicName);
        if (entity != null)
            return entity.toData();
        else
            return null;
    }

    @Override
    public List<Topic> getListByPage(int pageIndex, int pageSize) {
        Pageable pageable =  PageRequest.of(pageIndex, pageSize);
        Page<TopicEntity> entityPage = topicRepository.findAll(pageable);
        List<Topic> list = DataUtil.convertDataList(entityPage.getContent());
        return list;
    }

    @Override
    public Topic getById(String id) {
        Optional<TopicEntity> entityOptional = topicRepository.findById(id);
        if (entityOptional.isPresent()) {
            return entityOptional.get().toData();
        } else {
            return null;
        }

    }

    @Override
    public void save(Topic newTopic) {
        TopicEntity entity = new TopicEntity(newTopic);
        topicRepository.save(entity);
    }

    @Override
    public void delete(String id) {
        Optional<TopicEntity> topicOptional = topicRepository.findById(id);
        if (topicOptional.isPresent()) {
            TopicEntity entity = topicOptional.get();
            entity.setPointEntityList(new ArrayList<>());
            topicRepository.saveAndFlush(entity);
            topicRepository.deleteById(id);
        } else
            return;

    }

    @Override
    public void update(Topic data) {
        TopicEntity entity = new TopicEntity(data);
        topicRepository.save(entity);
    }

    @Override
    public void updatePoints(String topicId, List<String> pointIdList) {
        TopicEntity topic = topicRepository.findById(topicId).get();

        //执行删除topic_point操作
        topic.setPointEntityList(new ArrayList<PointEntity>());
        topicRepository.save(topic);

        //新增topic_point操作
        List<PointEntity> pointList = pointRepository.findAllById(pointIdList);

        topic.setPointEntityList(pointList);

        topicRepository.save(topic);
    }

    //@Override
    public void deleteByTopic(String topic){
        topicRepository.deleteByTopic(topic);
    }
}
