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.DataeyeCompany;
import cn.com.ctop.crawler.modules.dataeye.mapper.DataeyeCompanyMapper;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeCompanyService;
import cn.com.ctop.crawler.modules.dataeye.service.DataeyeProductService;
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 DataeyeCompanyServiceImpl extends ServiceImpl<DataeyeCompanyMapper, DataeyeCompany> implements DataeyeCompanyService {
    @Autowired
    private DataeyeProductService dataeyeProductService;
    @Override
    public void getCompanyInfo(Integer companyId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("companyId",companyId);
        String response = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/company/getCompanyInfo",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();
                DataeyeCompany company = mapper.readValue(content,DataeyeCompany.class);
                if(company != null && company.getCompanyName() != null){
                    this.saveOrUpdate(company);
                    String result = HttpUtils.httpPostFormRequest("https://blzs.dataeye.com/company/listProductDistribution?companyId="+companyId,null,null);
                    rootNode = mapper.readTree(result);
                    statusCode = rootNode.get("statusCode").asInt();
                    if (statusCode == 200) {
                        Iterator<JsonNode> nodes = rootNode.get("content").elements();
                        while (nodes.hasNext()){
                            JsonNode node = nodes.next();
                            Long productId = node.get("id").asLong();
                            dataeyeProductService.getProductInfo(companyId.longValue(),productId);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args){
        DataeyeCompanyServiceImpl i = new DataeyeCompanyServiceImpl();
        i.getCompanyInfo(1);
//        Date date = new Date(1582598505897L);


    }
}
