package cn.com.ctop.toutiao.modules.batch.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.toutiao.modules.batch.entity.BytedanceIndustryInfo;
import cn.com.ctop.toutiao.modules.batch.mapper.BytedanceIndustryInfoMapper;
import cn.com.ctop.toutiao.modules.batch.service.IBytedanceIndustryInfoService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 头条行业类别
 * @author jeecg-boot
 * @date   2020-07-30
 * @version V1.0
 */
@Service
public class BytedanceIndustryInfoServiceImpl extends ServiceImpl<BytedanceIndustryInfoMapper, BytedanceIndustryInfo> implements IBytedanceIndustryInfoService {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Override
    public Map<String, Object> getBytedanceIndustryList(Long accountId,Integer level) {
        CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
        // 请求地址
        String url = "https://ad.oceanengine.com/open_api/2/tools/industry/get/";
        // 请求参数
        JSONObject data = new JSONObject();
        if(null!=level&&level!=0){
            data.put("level",level);
        }
        JSONObject result = HttpUtils.bytedanceGetRequest(token.getAccessToken(), url, data);
        JSONArray arrays = result.getJSONObject("data").getJSONArray("list");
        for(int i=0;i<arrays.size();i++){
            JSONObject getData = arrays.getJSONObject(i);
            BytedanceIndustryInfo industryInfo = new BytedanceIndustryInfo();
            Integer getLevel = getData.getInteger("level");
            industryInfo.setLevel(getLevel);
            industryInfo.setName(getData.getString("industry_name"));
            industryInfo.setCode(getData.getLong("industry_id")+"");
            industryInfo.setId(getData.getLong("industry_id"));
            if(getLevel == 1){
                industryInfo.setParentCode("0");
            }
            if(getLevel == 2){
                industryInfo.setParentCode(getData.getLong("first_industry_id")+"");
            }
            if(getLevel == 3){
                industryInfo.setParentCode(getData.getLong("second_industry_id")+"");
            }
            industryInfo.setUpdateTime(new Date());
            this.saveOrUpdate(industryInfo);
        }
        return result;
    }

    @Override
    public Map<String, Object> getIndustryList() {
        Map<String,Object>result = new HashMap<>();
        List<BytedanceIndustryInfo>firstLevelIndustry = this.getListByParams(1,"0");
        JSONArray array = new JSONArray();
        for (BytedanceIndustryInfo firstInfo:firstLevelIndustry) {
            JSONObject firstData = new JSONObject();
            firstData.put("id",firstInfo.getId());
            firstData.put("name",firstInfo.getName());
            List<BytedanceIndustryInfo>secondLevelIndustry = this.getListByParams(2,firstInfo.getCode());
            if(null!=secondLevelIndustry&&!secondLevelIndustry.isEmpty()){
                JSONArray firstChildrenArray = new JSONArray();
                for(BytedanceIndustryInfo secendInfo:secondLevelIndustry){
                    JSONObject secondData = new JSONObject();
                    secondData.put("id",secendInfo.getId());
                    secondData.put("name",secendInfo.getName());
                    List<BytedanceIndustryInfo>thirdLevelIndustry = this.getListByParams(3,secendInfo.getCode());
                    if(null!=thirdLevelIndustry&&!thirdLevelIndustry.isEmpty()){
                        JSONArray secondChildrenArray = new JSONArray();
                        for (BytedanceIndustryInfo thirdInfo:thirdLevelIndustry) {
                            JSONObject thirdChData = new JSONObject();
                            thirdChData.put("id",thirdInfo.getId());
                            thirdChData.put("name",thirdInfo.getName());
                            secondChildrenArray.add(thirdChData);
                        }
                        secondData.put("children",secondChildrenArray);
                    }
                    firstChildrenArray.add(secondData);
                }
                firstData.put("children",firstChildrenArray);
            }
            array.add(firstData);
        }
        result.put("data",array);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    public List<BytedanceIndustryInfo>getListByParams(Integer level,String parentCode){
        QueryWrapper<BytedanceIndustryInfo>queryWrapper = new QueryWrapper<>();
        if(null!=level&&level!=0){
            queryWrapper.eq("level",level);
        }
        if(null!=parentCode&&!"".equals(parentCode.trim())){
            queryWrapper.eq("parent_code",parentCode);
        }
       return this.list(queryWrapper);
    }
}
