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.DataeyeAdArtStyle;
import cn.com.ctop.crawler.modules.dataeye.entity.DataeyePlayRules;
import cn.com.ctop.crawler.modules.dataeye.entity.DataeyeThemes;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeAdArtStyleService;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeLabelService;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyePlayRulesService;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeThemesService;
import com.fasterxml.jackson.core.type.TypeReference;
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.List;

@Service
@Primary

public class DataeyeLabelServiceImpl implements DataeyeLabelService {
    @Autowired
    private DataeyeAdArtStyleService dataeyeAdArtStyleService;
    @Autowired
    private DataeyeThemesService dataeyeThemesService;
    @Autowired
    private DataeyePlayRulesService dataeyePlayRulesService;

    public void getLabelList(){
        String response = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/common/getNewLabelMenu",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){
                String adThemeList = rootNode.get("content").get("adThemeList").toString();
                String adPlayRuleList = rootNode.get("content").get("adPlayRuleList").toString();
                String adArtStyleList = rootNode.get("content").get("adArtStyleList").toString();
                List<DataeyeAdArtStyle> adArtStyles = mapper.readValue(adArtStyleList, new TypeReference<List<DataeyeAdArtStyle>>(){});
                List<DataeyeThemes> themes = mapper.readValue(adThemeList,new TypeReference<List<DataeyeThemes>>(){});
                List<DataeyePlayRules> playRules = mapper.readValue(adPlayRuleList,new TypeReference<List<DataeyePlayRules>>(){});
                dataeyeAdArtStyleService.saveOrUpdateBatch(adArtStyles);
                dataeyeThemesService.saveOrUpdateBatch(themes);
                dataeyePlayRulesService.saveOrUpdateBatch(playRules);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
