package com.fs.watch.service.impl;

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

import java.util.*;

@Service
public class WatchDeviceBeginnerGuideServiceImpl implements WatchDeviceBeginnerGuideService {
    @Autowired
    private WatchConfig sysConfig;

    private final static MyHttpUtils httpUtils = new MyHttpUtils();
    /**
     * 查询新手引导
     *
     * @param id 新手引导主键
     * @return 新手引导
     */
    @Override
    public WatchDeviceBeginnerGuide selectDeviceBeginnerGuideById(Long id)
    {

        WatchDeviceBeginnerGuide watchDeviceBeginnerGuide = null;
        Map res = httpUtils.getRequest2(sysConfig.getWatchUrl(), "/watch/guide/" + id);
        if (res!=null){
            Object data = res.get("data");
            if (data !=null){
                watchDeviceBeginnerGuide = JSONObject.parseObject((String) data, WatchDeviceBeginnerGuide.class);
            }
        }
        return watchDeviceBeginnerGuide;
    }

    /**
     * 查询新手引导列表
     *
     * @param deviceBeginnerGuide 新手引导
     * @return 新手引导
     */
    @Override
    public List<WatchDeviceBeginnerGuide> selectDeviceBeginnerGuideList(WatchDeviceBeginnerGuide deviceBeginnerGuide)
    {
        List<WatchDeviceBeginnerGuide>  watchDeviceBeginnerGuides = new ArrayList<>();
        Map rse = httpUtils.getRequest3(sysConfig.getWatchUrl(),
                "/watch/guide/getList",
                Objects.requireNonNull(getguideMap(deviceBeginnerGuide)));
        if (rse!=null){
            Object data = rse.get("data");
            if (data !=null){
                List list = JSONObject.parseObject(JSONObject.toJSONString(data), List.class);
                if (list != null && !list.isEmpty()){
                    for (Object o : list) {
                        WatchDeviceBeginnerGuide guide = JSONObject.parseObject(JSONObject.toJSONString(o), WatchDeviceBeginnerGuide.class);
                        watchDeviceBeginnerGuides.add(guide);
                    }
                }
            }
        }
        return watchDeviceBeginnerGuides;
    }

    private Map<String, Object> getguideMap(WatchDeviceBeginnerGuide deviceBeginnerGuide) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", deviceBeginnerGuide.getId());
        map.put("title", deviceBeginnerGuide.getTitle());
        map.put("url", deviceBeginnerGuide.getUrl());
        map.put("type", deviceBeginnerGuide.getType());
        map.put("isDel", deviceBeginnerGuide.getIsDel());
        map.put("createTime", deviceBeginnerGuide.getCreateTime());
        map.put("belong", deviceBeginnerGuide.getBelong());
        return map;
    }
}
