package com.sw.mes.fb.checkout.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.fb.checkout.dao.FieldExtensionTableDao;
import com.sw.mes.fb.checkout.dto.FieldExtensionShowDto;
import com.sw.mes.fb.checkout.dto.FieldShowDto;
import com.sw.mes.fb.checkout.entity.FieldExtensionTable;
import com.sw.mes.fb.checkout.service.FieldExtensionTableService;
import com.sw.mes.fb.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class FieldExtensionTableServiceImpl extends BaseServiceImpl<FieldExtensionTable> implements FieldExtensionTableService {
    FieldExtensionTableDao dao;
	@Autowired
    public void setDao(FieldExtensionTableDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Override
    public FieldExtensionTable findByEnglishName(String englishName) {
        return dao.findByEnglishName(englishName);
    }

    @Override
    public List<FieldExtensionShowDto> findByExtendTable(String extendTable) {

        List<FieldExtensionTable> byExtendTable = dao.findByExtendTable(extendTable);
        List<FieldExtensionShowDto> fieldExtensionShowDtoList = new ArrayList<>();
        String topicName = "";
        FieldExtensionShowDto fieldExtensionShowDto = null;
        for (FieldExtensionTable fieldExtensionTable : byExtendTable){
            FieldShowDto fieldShowDto = new FieldShowDto();
            fieldShowDto.setName(fieldExtensionTable.getName());
            fieldShowDto.setOrderNo(1);
            fieldShowDto.setEnglishName(fieldExtensionTable.getEnglishName());
            if (StringUtils.equals(topicName,fieldExtensionTable.getTopicName())){
                List<FieldShowDto> fieldShowDtoList = fieldExtensionShowDto.getFieldShowDtoList();
                fieldShowDtoList.add(fieldShowDto);
                fieldExtensionShowDto.setFieldShowDtoList(fieldShowDtoList);
            }else {
                topicName = fieldExtensionTable.getTopicName();
                fieldExtensionShowDto = new FieldExtensionShowDto();
                List<FieldShowDto> fieldShowDtoList = new ArrayList<>();
                fieldShowDtoList.add(fieldShowDto);
                fieldExtensionShowDto.setFieldShowDtoList(fieldShowDtoList);
                fieldExtensionShowDto.setOrderNo(fieldExtensionTable.getOrderNo());
                fieldExtensionShowDto.setTopicName(topicName);
                fieldExtensionShowDtoList.add(fieldExtensionShowDto);
            }

        }
        if (fieldExtensionShowDtoList.size() != 0){
            fieldExtensionShowDtoList.sort(Comparator.comparing(FieldExtensionShowDto::getOrderNo));
        }

        return fieldExtensionShowDtoList;
    }

    @Override
    public Map<String, FieldExtensionTable> findMapByExtendTable(String extendTable) {

	    if (StringUtils.isBlank(extendTable)){
	        return null;
        }
        List<FieldExtensionTable> byExtendTable = dao.findByExtendTable(extendTable);
        Map<String, FieldExtensionTable> resultMap = new HashMap<>();
        for (FieldExtensionTable fieldExtensionTable : byExtendTable){
            resultMap.put(fieldExtensionTable.getEnglishName(),fieldExtensionTable);
        }
        return resultMap;
    }
}
