package com.jsc.oscm.service;

import com.jsc.oscm.dao.MetaDataRepository;
import com.jsc.oscm.dao.MetaDictRepository;
import com.jsc.oscm.entity.MetaData;
import com.jsc.oscm.entity.MetaDict;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MetaDataService {

    @Resource
    MetaDataRepository metaDataRepository;

    @Resource
    MetaDictRepository metaDictRepository;

    public List<MetaData> queryByTableName(String tableName) {
        List<MetaData> metaDataList;
        MetaData metaData = new MetaData();
        metaData.setTableName(tableName);
        metaData.setColumnVisible("1");
        Example<MetaData> example = Example.of(metaData);
        metaDataList = metaDataRepository.findAll(example);
        MetaDict metaDict = new MetaDict();
        metaDict.setTableName(tableName);
        Example<MetaDict> dictExample = Example.of(metaDict);
        List<MetaDict> metaDictList = metaDictRepository.findAll(dictExample);
        metaDataList.forEach(m -> metaDictList.forEach(d -> {
            if (d.getTableName().equals(m.getTableName()) && d.getColumnName().equals(m.getColumnProp())) {
                if (m.getDicts() != null) {
                    m.getDicts().add(d);
                }
            }
        }));
        return metaDataList;
    }

    public List<MetaData> queryAll() {
        MetaData metaData = new MetaData();
        metaData.setColumnVisible("1");
        Example<MetaData> example = Example.of(metaData);
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.ASC, "tableName"));
        orders.add(new Sort.Order(Sort.Direction.ASC, "columnIndex"));
        List<MetaData> metaDataList = metaDataRepository.findAll(example, Sort.by(orders));
        List<MetaDict> metaDictList = metaDictRepository.findAll();
        metaDataList.forEach(m -> metaDictList.forEach(d -> {
            if (d.getTableName().equals(m.getTableName()) && d.getColumnName().equals(m.getColumnProp())) {
                if (m.getDicts() == null) {
                    List<MetaDict> tempList = new ArrayList<>();
                    tempList.add(d);
                    m.setDicts(tempList);
                } else {
                    m.getDicts().add(d);
                }
            }
        }));
        return metaDataList;
    }

    public MetaData save(MetaData metaData) {
        return metaDataRepository.save(metaData);
    }

    public void delete(MetaData metaData) {
        metaDataRepository.delete(metaData);
    }

    public void deleteAll(List<MetaData> metaDataList) {
        metaDataRepository.deleteAll(metaDataList);
    }

    public List<MetaData> queryPageable(MetaData metaData, int pageIndex, int pageSize) {
        Pageable pageable = PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Order.desc("id")));
        Example<MetaData> example = Example.of(metaData);
        return metaDataRepository.findAll(example, pageable).getContent();
    }

    public List<MetaData> queryAll(MetaData metaData) {
        Example<MetaData> example = Example.of(metaData);
        return metaDataRepository.findAll(example);
    }

    public Long queryCount(MetaData metaData) {
        Example<MetaData> example = Example.of(metaData);
        return metaDataRepository.count(example);
    }

    public List<Object> getTableNameList() {
        return metaDataRepository.getTableNameList();
    }

}
