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.*;
import cn.com.ctop.crawler.modules.dataeye.mapper.DataeyeProductMapper;
import cn.com.ctop.crawler.modules.dataeye.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
@Primary

public class DataeyeProductServiceImpl extends ServiceImpl<DataeyeProductMapper, DataeyeProduct> implements DataeyeProductService {
    @Autowired
    private DataeyeProductThemeService dataeyeProductThemeService;
    @Autowired
    private DataeyeProductPlayRuleService dataeyeProductPlayRuleService;
    @Autowired
    private DataeyeProductAdArtStyleService dataeyeProductAdArtStyleService;
    @Autowired
    private DataeyeCompanyProductService dataeyeCompanyProductService;

    @Override
    public void getProductInfo(Long companyId, Long productId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("productId",productId);
        String response = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/product/getProductInfo",paramMap,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 content = rootNode.get("content").toString();
                DataeyeProduct product = mapper.readValue(content,DataeyeProduct.class);
                if(product != null && product.getProductName() != null && !"".equals(product.getProductName())){
                    this.saveOrUpdate(product);
                }
                QueryWrapper<DataeyeCompanyProduct> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("company_id",companyId);
                queryWrapper1.eq("product_id",productId);
                DataeyeCompanyProduct dataeyeCompanyProduct = dataeyeCompanyProductService.getOne(queryWrapper1);
                if(dataeyeCompanyProduct == null){
                    dataeyeCompanyProduct = new DataeyeCompanyProduct();
                    dataeyeCompanyProduct.setCompanyId(companyId);
                    dataeyeCompanyProduct.setProductId(productId);
                    dataeyeCompanyProductService.save(dataeyeCompanyProduct);
                }
                Iterator<JsonNode> themeNodes = rootNode.get("content").get("themes").elements();
                Iterator<JsonNode> ruleNodes = rootNode.get("content").get("playRules").elements();
                Iterator<JsonNode> styleNodes = rootNode.get("content").get("adArtStyles").elements();
                while (themeNodes.hasNext()){
                    JsonNode node = themeNodes.next();
                    Long themeId = node.get("id").asLong();
                    QueryWrapper<DataeyeProductTheme> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("product_id",productId);
                    queryWrapper.eq("theme_id",themeId);
                    DataeyeProductTheme theme = dataeyeProductThemeService.getOne(queryWrapper);
                    if (theme == null){
                        theme = new DataeyeProductTheme();
                        theme.setProductId(productId.longValue());
                        theme.setThemeId(themeId);
                        dataeyeProductThemeService.save(theme);
                    }
                }
                while (ruleNodes.hasNext()){
                    JsonNode node = ruleNodes.next();
                    Long ruleId = node.get("id").asLong();
                    QueryWrapper<DataeyeProductPlayRule> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("product_id",productId);
                    queryWrapper.eq("rule_id",ruleId);
                    DataeyeProductPlayRule rule = dataeyeProductPlayRuleService.getOne(queryWrapper);
                    if (rule == null){
                        rule = new DataeyeProductPlayRule();
                        rule.setProductId(productId.longValue());
                        rule.setRuleId(ruleId);
                        dataeyeProductPlayRuleService.save(rule);
                    }
                }
                while (styleNodes.hasNext()){
                    JsonNode node = styleNodes.next();
                    Long styleId = node.get("id").asLong();
                    QueryWrapper<DataeyeProductAdArtStyle> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("product_id",productId);
                    queryWrapper.eq("style_id",styleId);
                    DataeyeProductAdArtStyle style = dataeyeProductAdArtStyleService.getOne(queryWrapper);
                    if (style == null){
                        style = new DataeyeProductAdArtStyle();
                        style.setProductId(productId.longValue());
                        style.setStyleId(styleId);
                        dataeyeProductAdArtStyleService.save(style);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
