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

import com.gyee.wisdom.common.data.system.model.DataDictionary;
import com.gyee.wisdom.dao.sql.system.entity.DataDictionaryEntity;
import com.gyee.wisdom.dao.sql.system.entity.MenuEntity;
import com.gyee.wisdom.dao.sql.system.repository.DataDictionaryRepository;
import com.gyee.wisdom.dao.system.IDataDictionaryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class JpaDataDictionaryDao implements IDataDictionaryDao {
    @Autowired
    private DataDictionaryRepository dataDictionaryRepository;

    @Override
    public void add(DataDictionary data) {
        DataDictionaryEntity entity = new DataDictionaryEntity(data);
        dataDictionaryRepository.save(entity);
    }

    @Override
    public void update(DataDictionary data) {
        DataDictionaryEntity entity = new DataDictionaryEntity(data);
        dataDictionaryRepository.save(entity);
    }

    @Override
    public void delete(int id) {
        dataDictionaryRepository.deleteById(id);
    }

    @Override
    public List<DataDictionary> getListByCategory(String category) {
        List<DataDictionaryEntity> entityList = dataDictionaryRepository.findDataDictionaryEntitiesByCategory(category);
        List<DataDictionary> dataList = new ArrayList<DataDictionary>();

        for (DataDictionaryEntity data : entityList) {
            dataList.add(data.toData());
        }
        return dataList;
    }

    @Override
    public DataDictionary getById(int id) {
        return dataDictionaryRepository.findById(id).toData();
    }

    @Override
    public List<DataDictionary> getList() {
        List<DataDictionaryEntity> entityList = dataDictionaryRepository.findAll();
        List<DataDictionary> dataList = new ArrayList<DataDictionary>();
        for (DataDictionaryEntity data : entityList) {
            dataList.add(data.toData());
        }
        return dataList;
    }


}
