package com.fs.watch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.config.WatchConfig;
import com.fs.watch.domain.FsMonitorDataType;
import com.fs.watch.service.WatchMonitorDataTypeService;
import com.fs.watch.utils.MyHttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WatchMonitorDataTypeServiceImpl implements WatchMonitorDataTypeService {
    @Autowired
    private WatchConfig sysConfig;

    private static final MyHttpUtils httpUtils = new MyHttpUtils();
    /**
     * 查询app-健康监测数据类型
     *
     * @param id app-健康监测数据类型主键
     * @return app-健康监测数据类型
     */
    @Override
    public FsMonitorDataType selectFsMonitorDataTypeById(String id)
    {
        FsMonitorDataType fsMonitorDataType = null;
        Map getTypeRes = httpUtils.getRequest2(sysConfig.getWatchUrl(), "/monitor/data/type/getById/" + id);
        if (getTypeRes!=null){
            Object data = getTypeRes.get("data");
            if (data !=null){
                fsMonitorDataType = JSONObject.parseObject(JSONObject.toJSONString(data), FsMonitorDataType.class);
            }
        }
        return fsMonitorDataType;
    }

    /**
     * 查询app-健康监测数据类型列表
     *
     * @param fsMonitorDataType app-健康监测数据类型
     * @return app-健康监测数据类型
     */
    @Override
    public List<FsMonitorDataType> selectFsMonitorDataTypeList(FsMonitorDataType fsMonitorDataType)
    {
        List<FsMonitorDataType>  fsMonitorDataTypes = new ArrayList<>();
        Map getTypeRes = httpUtils.getRequest3(sysConfig.getWatchUrl(), "/monitor/data/type/list",getTypeMap(fsMonitorDataType));
        if (getTypeRes!=null){
            if ((int)getTypeRes.get("code") == 200){
                Object data = getTypeRes.get("rows");
                if (data !=null){
                    List<Object> list = JSONObject.parseObject(JSONObject.toJSONString(data), List.class);
                    if (list != null && !list.isEmpty()){
                        for (Object o : list) {
                            FsMonitorDataType type = JSONObject.parseObject(o.toString(), FsMonitorDataType.class);
                            fsMonitorDataTypes.add(type);
                        }
                    }
                }
            }

        }
        return fsMonitorDataTypes;
    }

    private Map<String, Object> getTypeMap(FsMonitorDataType fsMonitorDataType) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", fsMonitorDataType.getId());
        map.put("type", fsMonitorDataType.getType());
        map.put("title", fsMonitorDataType.getTitle());
        map.put("icon", fsMonitorDataType.getIcon());
        map.put("createTime", fsMonitorDataType.getCreateTime());
        return map;
    }



    @Override
    public List<FsMonitorDataType> selectFsMonitorDataTypeByIds(String ids) {
        List<FsMonitorDataType>  fsMonitorDataTypes = new ArrayList<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        Map getTypeRes = httpUtils.getRequest3(sysConfig.getWatchUrl(), "/monitor/data/type/getBatchData",params);
        if (getTypeRes!=null){
            Object data = getTypeRes.get("data");
            if (data !=null){
                List list = JSONObject.parseObject(JSONObject.toJSONString(data) , List.class);
                if (list != null && !list.isEmpty()){
                    for (Object o : list) {
                        FsMonitorDataType fsMonitorDataType = JSONObject.parseObject(o.toString(), FsMonitorDataType.class);
                        fsMonitorDataTypes.add(fsMonitorDataType);
                    }
                }
            }
        }
        return fsMonitorDataTypes;
    }
}
