package com.yihu.hos.resource.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.common.Services;
import com.yihu.hos.core.datatype.CollectionUtil;
import com.yihu.hos.standard.model.adapter.AdapterDatasetModel;
import com.yihu.hos.standard.model.adapter.AdapterDictEntryModel;
import com.yihu.hos.standard.model.adapter.resultModel.AdapterMetadataResultDetailModel;
import com.yihu.hos.standard.model.adapter.resultModel.AdapterSchemeVersionResultDetailModel;
import com.yihu.hos.standard.model.standard.StdDataSetModel;
import com.yihu.hos.standard.model.standard.StdMetaDataModel;
import com.yihu.hos.standard.service.adapter.AdapterDatasetService;
import com.yihu.hos.standard.service.adapter.AdapterDictEntryService;
import com.yihu.hos.standard.service.adapter.AdapterMetadataService;
import com.yihu.hos.standard.service.adapter.AdapterSchemeVersionService;
import com.yihu.hos.standard.service.bo.AdapterVersion;
import com.yihu.hos.standard.service.standard.StdDatasetService;
import com.yihu.hos.standard.service.standard.StdMetadataService;
import com.yihu.hos.web.framework.model.DictItem;
import com.yihu.hos.web.framework.model.DictionaryResult;
import org.springframework.beans.factory.annotation.Autowired;
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(Services.StdService)
public class StdService  {

    public static final String BEAN_ID = Services.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;
    @Autowired
    private ObjectMapper objectMapper;


    /**************************** 适配方案 **************************************************/
    /**
     * 获取适配方案列表
     */
    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);
        String conditon = null;
        try {
            conditon = objectMapper.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);
        String condition = objectMapper.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);
        String condition = null;
        try {
            condition = objectMapper.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);
        String condition = objectMapper.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);
        String condition = objectMapper.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);
    }

    /**
     * 根据数据集code得到数据元
     *
     * @param params
     * @return
     * @throws Exception
     */
    public List<StdMetaDataModel> metadatasAllVersionList(Map<String, Object> params) {
        ObjectMapper mapper = new ObjectMapper();
        String condition = null;
        try {
            condition = mapper.writeValueAsString(params);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return stdMetadataService.getAllVersionMetadataList(condition);
    }

    /**
     * 得到全部版本的数据元
     *
     * @return
     * @throws Exception
     */
    public List<StdDataSetModel> datasetsAllVersionList() throws Exception {
        return stdDatasetService.getAllVersionDatasetList();
    }

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

}
