package cn.com.ctop.crawler.modules.dataeye.service.impl;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.crawler.modules.dataeye.entity.DataeyeMedia;
import cn.com.ctop.crawler.modules.dataeye.mapper.DataeyeMediaMapper;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeCompanyService;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
@Primary

public class DataeyeMediaServiceImpl extends ServiceImpl<DataeyeMediaMapper, DataeyeMedia> implements DataeyeMediaService {
    @Autowired
    private DataeyeCompanyService dataeyeCompanyService;
    @Override
    public void getMediaList(){
        String response = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/common/listMedia",null,null);
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        try {
            JsonNode rootNode = mapper.readTree(response);
            int statusCode = rootNode.get("statusCode").asInt();
            if (statusCode == 200){
                Iterator<JsonNode> nodes = rootNode.get("content").elements();
                List<Integer> ids = new ArrayList<>();
                while (nodes.hasNext()){
                    JsonNode node =  nodes.next();
                    Integer mediaId = node.get("k").asInt();
                    ids.add(mediaId);
                    response = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/media/getMediaInfo?mediaId="+mediaId,null,null);
                    rootNode = mapper.readTree(response);
                    statusCode = rootNode.get("statusCode").asInt();
                    if (statusCode == 200){
                        String content = rootNode.get("content").toString();
                        DataeyeMedia media = mapper.readValue(content,DataeyeMedia.class);
                        if(media != null && media.getMediaId() != null){
                            this.saveOrUpdate(media);

                        }
                    }
                }

                for (Integer mediaId :ids){
                    response = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/media/listCompanyDistribution?mediaId="+mediaId,null,null);
                    rootNode = mapper.readTree(response);
                    statusCode = rootNode.get("statusCode").asInt();
                    if (statusCode == 200){
                        Iterator<JsonNode> companyNodes = rootNode.get("content").elements();
                        while (companyNodes.hasNext()){
                            JsonNode companyNode = companyNodes.next();
                            Integer companyId = companyNode.get("id").asInt();
                            dataeyeCompanyService.getCompanyInfo(companyId);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
