package com.yihu.hos.rest.services.standard;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.core.datatype.CollectionUtil;
import com.yihu.hos.rest.models.standard.adapter.AdapterDatasetModel;
import com.yihu.hos.rest.models.standard.adapter.AdapterDictEntryModel;
import com.yihu.hos.rest.models.standard.adapter.resultModel.AdapterMetadataResultDetailModel;
import com.yihu.hos.rest.models.standard.adapter.resultModel.AdapterSchemeVersionResultDetailModel;
import com.yihu.hos.rest.models.standard.bo.AdapterVersion;
import com.yihu.hos.rest.models.standard.standard.StdDataSetModel;
import com.yihu.hos.rest.models.standard.standard.StdMetaDataModel;
import com.yihu.hos.rest.services.standard.adapter.AdapterDatasetService;
import com.yihu.hos.rest.services.standard.adapter.AdapterDictEntryService;
import com.yihu.hos.rest.services.standard.adapter.AdapterMetadataService;
import com.yihu.hos.rest.services.standard.adapter.AdapterSchemeVersionService;
import com.yihu.hos.rest.services.standard.standard.StdDatasetService;
import com.yihu.hos.rest.services.standard.standard.StdMetadataService;
import com.yihu.hos.web.framework.model.DictItem;
import com.yihu.hos.web.framework.model.DictionaryResult;
import org.springframework.stereotype.Service;

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

/**
 * Created by hzp on 2016/1/20.
 */
@Service
public class StdService {

    @Resource(name = StdDatasetService.BEAN_ID)
    private StdDatasetService stdDatasetService;
    @Resource(name = StdMetadataService.BEAN_ID)
    private StdMetadataService stdMetadataService;
    @Resource(name = AdapterDictEntryService.BEAN_ID)
    private AdapterDictEntryService dictentryService;
    @Resource(name = AdapterMetadataService.BEAN_ID)
    private AdapterMetadataService metadataService;
    @Resource(name = AdapterDatasetService.BEAN_ID)
    private AdapterDatasetService datasetService;
    @Resource(name = AdapterSchemeVersionService.BEAN_ID)
    private AdapterSchemeVersionService adapterSchemeVersion;


    /**************************** 适配方案 **************************************************/
    /**
     * 获取适配方案列表
     */
    public List<AdapterSchemeVersionResultDetailModel> getSchemeVersion() {
        return adapterSchemeVersion.getPublishedList();
    }

    /**
     * 通过适配方案获取数据集列表
     */
    public List<AdapterDatasetModel> getDatasetByScheme(String schemeVersion) {
        AdapterVersion version = new AdapterVersion(schemeVersion);
        return datasetService.getList(AdapterDatasetModel.class, version.getDataSetTableName(), null, null, null, null);
    }

    /**
     * 通过适配方案+应用数据集ID获取数据集列表
     */
    public List<AdapterDatasetModel> getDatasetByScheme(String schemeVersion, String adapterDatasetId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("adapterDatasetId", adapterDatasetId);
        AdapterVersion version = new AdapterVersion(schemeVersion);
        ObjectMapper mapper = new ObjectMapper();
        String conditon = null;
        try {
            conditon = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return datasetService.getList(AdapterDatasetModel.class, version.getDataSetTableName(), conditon, null, null, null);
    }


    /**
     * 通过适配方案获取字段列表
     */
    public List<AdapterMetadataResultDetailModel> getDatacolByScheme(String schemeVersion, String datasetId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("adapterDatasetId", datasetId);
        ObjectMapper mapper = new ObjectMapper();
        String condition = mapper.writeValueAsString(map);
        return metadataService.getInfoList(schemeVersion, condition, null, null, null);
    }

    /**
     * 通过适配方案获取字典列表
     */
    public List<AdapterDictEntryModel> getDictByScheme(String schemeVersion, String dictId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stdDictId", dictId);
        AdapterVersion version = new AdapterVersion(schemeVersion);
        ObjectMapper mapper = new ObjectMapper();
        String condition = null;
        try {
            condition = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return dictentryService.getList(AdapterDictEntryModel.class, version.getDictEntryTableName(), condition, null, null, null);
    }

    /**
     * 通过适配方案获取字典列表
     */
    public DictionaryResult getDictResultByVersion(String schemeVersion, String dictId) throws Exception {
        DictionaryResult re = new DictionaryResult();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stdDictId", dictId);
        AdapterVersion version = new AdapterVersion(schemeVersion);
        ObjectMapper mapper = new ObjectMapper();
        String condition = mapper.writeValueAsString(map);

        List<AdapterDictEntryModel> dictentryList = dictentryService.getList(AdapterDictEntryModel.class, version.getDictEntryTableName(), condition, null, null, null);

        List<DictItem> list = new ArrayList<>();
        if (!CollectionUtil.isEmpty(dictentryList)) {
            for (AdapterDictEntryModel adapterDictEntryModel : dictentryList) {
                String code = adapterDictEntryModel.getAdapterEntryCode();
                if (code != null) {
                    DictItem dict = new DictItem();
                    dict.setCode(code);
                    dict.setValue(adapterDictEntryModel.getAdapterEntryValue());
                    list.add(dict);
                }
            }
        }
        re.setDetailModelList(list);

        return re;
    }


    /**
     * 通过适配方案+标准数据集CODE获取数据集
     *
     * @return
     */
    public AdapterDatasetModel getDatasetByCode(String schemeVersion, String stdDatasetCode) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stdDatasetCode", stdDatasetCode);
        AdapterVersion version = new AdapterVersion(schemeVersion);
        ObjectMapper mapper = new ObjectMapper();
        String condition = mapper.writeValueAsString(map);

        List<AdapterDatasetModel> list = datasetService.getList(AdapterDatasetModel.class, version.getDataSetTableName(), condition, null, null, null);

        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /******************************** 标准版本 *****************************************************/
    /**
     * 通过标准版本获取数据集列表
     */
    public List<StdDataSetModel> getDatasetByVersion(String stdVersion) {
        return stdDatasetService.getList(stdVersion, null, null, null, null);
    }

    public List datasetVersionListByName(String rsResourceImportDatasetDialogName) throws Exception {
        return stdDatasetService.datasetVersionListByName(rsResourceImportDatasetDialogName);
    }

}
