package com.salpa.subject.service.impl;

import com.salpa.subject.domain.MonitorDocumentTemplateProperty;
import com.salpa.subject.domain.data.DocumentTemplatePropertiesResponse;
import com.salpa.subject.mapper.DocumentTemplatePropertyMapper;
import com.salpa.subject.service.DocumentTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.salpa.subject.domain.data.DocumentTemplatePropertyType.*;

@Service
public class DocumentTemplateServiceImpl implements DocumentTemplateService {

    @Autowired
    private DocumentTemplatePropertyMapper documentTemplatePropertyMapper;

    /**
     * 获取模板属性
     * @return
     */
    @Override
    public DocumentTemplatePropertiesResponse getAllProperties() {
        List<MonitorDocumentTemplateProperty> properties = documentTemplatePropertyMapper.selectAll();
        List<DocumentTemplatePropertiesResponse.DocumentTemplatePropertyResponse> list = properties.stream().map(a ->{
            //在此转把A转换为B
            DocumentTemplatePropertiesResponse.DocumentTemplatePropertyResponse documentTemplatePropertyResponse = new DocumentTemplatePropertiesResponse.DocumentTemplatePropertyResponse();
            documentTemplatePropertyResponse.setKey(a.getKey());
            documentTemplatePropertyResponse.setValue(a.getValue());
            documentTemplatePropertyResponse.setDefaultValue(a.getDefaultValue());
            documentTemplatePropertyResponse.setType(a.getType());
            documentTemplatePropertyResponse.setCreateAt(a.getCreateAt());
            return documentTemplatePropertyResponse;
        }).collect(Collectors.toList());
        var propertiesGroupByType = list
                .stream()
                .collect(Collectors.groupingBy(d -> d.getType()));
        return DocumentTemplatePropertiesResponse.builder()
                .tableFieldNameProperties(propertiesGroupByType.get(TABLE_FIELD_NAME))
                .columnFieldNameProperties(propertiesGroupByType.get(COLUMN_FIELD_NAME))
                .foreignKeyFieldNameProperties(propertiesGroupByType.get(FOREIGN_KEY_FIELD_NAME))
                .indexFieldNameProperties(propertiesGroupByType.get(INDEX_FIELD_NAME))
                .triggerFieldNameProperties(propertiesGroupByType.get(TRIGGER_FIELD_NAME))
                .build();
    }
}
