package com.bocloud.ims.service.utils;

import com.bocloud.common.utils.ListTool;
import com.bocloud.ims.entity.topology.Instance;
import com.bocloud.ims.entity.topology.InstanceValue;
import com.bocloud.ims.entity.topology.Property;
import com.bocloud.ims.entity.topology.PropertyItem;
import com.bocloud.ims.repository.topology.InstanceGridRepository;
import com.bocloud.ims.repository.topology.InstanceRepository;
import com.bocloud.ims.repository.topology.InstanceValueRepository;
import com.bocloud.ims.repository.topology.PropertyItemRepository;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author tangcq
 * @since 2018-02-01
 */
public class InstanceHelper {
    private PropertyItemRepository propertyItemRepository;
    private InstanceRepository instanceRepository;
    private InstanceGridRepository instanceGridRepository;
    private InstanceValueRepository instanceValueRepository;

    public InstanceHelper(PropertyItemRepository propertyItemRepository, InstanceRepository instanceRepository, InstanceGridRepository instanceGridRepository, InstanceValueRepository instanceValueRepository) {
        this.propertyItemRepository = propertyItemRepository;
        this.instanceRepository = instanceRepository;
        this.instanceGridRepository = instanceGridRepository;
        this.instanceValueRepository = instanceValueRepository;
    }

    /**
     * 对每一条数据进行翻译 对下拉 引用等属性做优化，显示其名称
     *
     * @param propertiesAll
     * @param list
     * @throws Exception
     */
    public void converInstanceValue(List<Property> propertiesAll, List<Map<String, Object>> list) throws Exception {
        if (ListTool.isEmpty(list)) {
            return;
        }
        Map<String, List<PropertyItem>> propertyItemListCache = Maps.newHashMap();

        Map<String, Property> propertyMap = Maps.newHashMap();
        for (Property property : propertiesAll) {
            propertyMap.put(property.getCode(), property);
        }
        // 循环每一条数据
        for (Map<String, Object> map : list) {
            // 循环每个字段
            for (String key : map.keySet()) {
                // 当前字段为属性的话 才继续
                if (null == propertyMap.get(key)) {
                    continue;
                }
                Property property = propertyMap.get(key);
                // 判断属性类型 是否需要转换
                if (property.isSelect() || property.isRedio()) {
                    if (StringUtils.isNotEmpty((CharSequence) map.get(key))) {
                        List<PropertyItem> ciPropertyItemList = propertyItemListCache
                                .get("property_" + property.getId());
                        if (ciPropertyItemList == null) {
                            ciPropertyItemList = propertyItemRepository.queryByPropertyId(property.getId());
                            propertyItemListCache.put("property_" + property.getId(), ciPropertyItemList);
                        }
                        for (PropertyItem propertyItem : ciPropertyItemList) {
                            if (map.get(key).toString().equals(propertyItem.getValue())) {
                                map.put(key, propertyItem.getName());
                                break;
                            }
                        }
                    }
                } else if (property.isCheckBox()) {
                    if (StringUtils.isNotEmpty((CharSequence) map.get(key))) {
                        List<PropertyItem> ciPropertyItemList = propertyItemListCache
                                .get("property_" + property.getId());
                        if (ciPropertyItemList == null) {
                            ciPropertyItemList = propertyItemRepository.queryByPropertyId(property.getId());
                            propertyItemListCache.put("property_" + property.getId(), ciPropertyItemList);
                        }
                        String[] values = map.get(key).toString().split(",");
                        String names = "";
                        if (values.length > 1) {
                            for (String value : values) {
                                for (PropertyItem propertyItem : ciPropertyItemList) {
                                    if (value.equals(propertyItem.getValue())) {
                                        names = names + "," + propertyItem.getName();
                                        break;
                                    }
                                }
                            }
                            names = names.substring(1, names.length());
                        } else {
                            for (PropertyItem propertyItem : ciPropertyItemList) {
                                if (values[0].equals(propertyItem.getValue())) {
                                    names = propertyItem.getName();
                                    break;
                                }
                            }
                        }
                        map.put(key, names);
                    }
                } else if (property.isQuote()) {
                    if (StringUtils.isNotEmpty((CharSequence) map.get(key))) {
                        Instance instanceTemp = instanceRepository.query(Long.parseLong(map.get(key).toString()));
                        if (instanceTemp == null) {
                            map.put(key, "配置已删除");
                        } else {
                            InstanceValue instanceValue = instanceValueRepository.queryTitleByInstanceId(instanceTemp.getId());
                            if (instanceValue != null && !"".equals(instanceValue.getPropertyValue())) {
                                instanceTemp.setBname(instanceValue.getPropertyValue());
                            }
                            map.put(key, instanceTemp.getBname());
                        }
                    }
                }
                if (StringUtils.isNotEmpty(property.getUnit()) && StringUtils.isNotEmpty((CharSequence) map.get(key))) {
                    map.put(key, map.get(key) + property.getUnit());
                }
            }
        }
    }

    /**
     * 把一条数据的 属性进行翻译
     *
     * @param ciPropertyLists
     * @throws Exception
     */
    public void converDetailValue(Long instanceId, List<Property> ciPropertyLists) throws Exception {
        for (Property property : ciPropertyLists) {
            //下拉菜单和单选
            if (property.isSelect() || property.isRedio()) {
                //根据属性id查询下拉菜单的值或单选的所有可选值
                List<PropertyItem> ciPropertyItemList = propertyItemRepository.queryByPropertyId(property.getId());
                property.setItem(ciPropertyItemList);
                if (StringUtils.isNotEmpty(property.getPropertyValue())) {
                    for (PropertyItem propertyItem : ciPropertyItemList) {
                        if (property.getPropertyValue().equals(propertyItem.getValue())) {
                            property.setPropertyName(propertyItem.getName());
                            break;
                        }
                    }
                }
                //多选
            } else if (property.isCheckBox()) {
                List<PropertyItem> ciPropertyItemList = propertyItemRepository.queryByPropertyId(property.getId());
                property.setItem(ciPropertyItemList);
                if (StringUtils.isNotEmpty(property.getPropertyValue())) {
                    String[] values = property.getPropertyValue().split(",");
                    String names = "";
                    for (String value : values) {
                        for (PropertyItem propertyItem : ciPropertyItemList) {
                            if (value.equals(propertyItem.getValue())) {
                                names = names + "," + propertyItem.getName();
                                break;
                            }
                        }
                    }
                    names = names.substring(1, names.length());
                    property.setPropertyName(names);
                }
                //引用
            } else if (property.isQuote()) {
                if (StringUtils.isNotEmpty(property.getPropertyValue())) {
                    Instance instanceTemp = instanceRepository.query(Long.parseLong(property.getPropertyValue()));
                    if (instanceTemp == null) {
                        property.setPropertyName("配置已删除");
                    } else {
                        InstanceValue instanceValue = instanceValueRepository.queryTitleByInstanceId(instanceTemp.getId());
                        if (instanceValue != null && !"".equals(instanceValue.getPropertyValue())) {
                            instanceTemp.setBname(instanceValue.getPropertyValue());
                        }
                        property.setPropertyName(instanceTemp.getBname());
                    }
                }
                //表格
            } else if (property.isTable()) {
                List<PropertyItem> ciPropertyItemList = propertyItemRepository.queryByPropertyId(property.getId());
                property.setItem(ciPropertyItemList);
                property.setGrid(instanceGridRepository.queryByPropertyCode(instanceId, property.getCode()));
            } else {
                // 商定好 前台就用propertyname显示，所以都放到propertyname 里
                property.setPropertyName(property.getPropertyValue());
            }
        }
    }
}
