package com.ruixuan.launch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.common.utils.KuaishouAPIConstant;
import com.ruixuan.launch.entity.AccountToken;
import com.ruixuan.launch.entity.KuaishouLaunchGroupTarget;
import com.ruixuan.launch.mapper.KuaishouLaunchGroupTargetMapper;
import com.ruixuan.launch.service.IAccountTokenService;
import com.ruixuan.launch.service.IKuaishouLaunchGroupTargetService;
import com.ruixuan.launch.unitls.APIUtil;
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;

/**
 * 广告组定向信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-28
 */
@Service
public class KuaishouLaunchGroupTargetServiceImpl implements IKuaishouLaunchGroupTargetService {
    @Autowired
    private KuaishouLaunchGroupTargetMapper kuaishouLaunchGroupTargetMapper;

    @Autowired
    private IAccountTokenService tokenService;

    /**
     * 查询广告组定向信息列表
     *
     * @param kuaishouLaunchGroupTarget 广告组定向信息
     * @return 广告组定向信息
     */
    @Override
    public List<KuaishouLaunchGroupTarget> selectKuaishouLaunchGroupTargetList(KuaishouLaunchGroupTarget kuaishouLaunchGroupTarget) {
        return kuaishouLaunchGroupTargetMapper.selectKuaishouLaunchGroupTargetList(kuaishouLaunchGroupTarget);
    }

    /**
     * 新增广告组定向信息
     *
     * @param kuaishouLaunchGroupTarget 广告组定向信息
     * @return 结果
     */
    @Override
    public int insertKuaishouLaunchGroupTarget(JSONObject request) {
        KuaishouLaunchGroupTarget kuaishouLaunchGroupTarget = JSONObject.parseObject(request.toJSONString(), KuaishouLaunchGroupTarget.class);
        return kuaishouLaunchGroupTargetMapper.insertKuaishouLaunchGroupTarget(kuaishouLaunchGroupTarget);
    }

    @Override
    public ResultResponse getRegion(Long accountId, Integer categoryType) {
        AccountToken token = tokenService.getAccountToken(accountId);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("advertiser_id", accountId);
        param.put("category_type", categoryType);
        JSONObject result = APIUtil.getApiResult(KuaishouAPIConstant.REGION, token.getAccessToken(), param);
        if (Check.isNotNull(result)) {
            return ResultResponse.success(result.getJSONObject("data").getJSONArray("details"));
        }
        return null;
    }

    @Override
    public ResultResponse getPopulation(Long accountId) {
        AccountToken token = tokenService.getAccountToken(accountId);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("advertiser_id", accountId);
        param.put("status", 4);
        param.put("page_size", 500);
        JSONObject result = APIUtil.getApiResult(KuaishouAPIConstant.POPULATION, token.getAccessToken(), param);
        if (Check.isNotNull(result)) {
            return ResultResponse.success(result.getJSONObject("data").getJSONArray("details"));
        }
        return null;
    }

    @Override
    public ResultResponse getCelebrityInfo(Long accountId, String keyword, String secondLabel, Long authorId) {
        AccountToken token = tokenService.getAccountToken(accountId);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("advertiser_id", accountId);
        if (Check.isNotNull(keyword)) {
            param.put("keyword", keyword);
        }
        if (Check.isNotNull(secondLabel)) {
            param.put("second_label", secondLabel);
        }
        if (Check.isNotNull(authorId)) {
            param.put("author_id", authorId);
        }
        JSONObject result = APIUtil.getApiResult(KuaishouAPIConstant.CELEBRITY_QUERY, token.getAccessToken(), param);
        if (Check.isNotNull(result)) {
            return ResultResponse.success(result.getJSONObject("data").getJSONArray("details"));
        }
        return null;
    }

    @Override
    public ResultResponse getCelebrityType(Long accountId) {
        AccountToken token = tokenService.getAccountToken(accountId);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("advertiser_id", accountId);
        JSONObject result = APIUtil.getApiResult(KuaishouAPIConstant.CELEBRITY_TYPE, token.getAccessToken(), param);
        if (Check.isNotNull(result)) {
            return ResultResponse.success(result.getJSONObject("data").getJSONArray("details"));
        }
        return null;
    }

    @Override
    public ResultResponse getPurchaseIntention(Long accountId) {
        AccountToken token = tokenService.getAccountToken(accountId);
        JSONObject param = new JSONObject();
        param.put("advertiser_id", accountId);
        JSONObject result = APIUtil.getApiResultByGet(KuaishouAPIConstant.PURCHASE_INTENTION, token.getAccessToken(), param);
        if (Check.isNotNull(result)) {
            return ResultResponse.success(result.getJSONObject("data").getJSONArray("list"));
        }
        return null;
    }

    @Override
    public ResultResponse getVideos(Long accountId) {
        AccountToken token = tokenService.getAccountToken(accountId);
        JSONObject param = new JSONObject();
        param.put("advertiser_id", accountId);
        JSONObject params = new JSONObject();
        params.put("start_time", DateUtils.timeToStamp(DateUtils.getDate().concat(" 00:00:01")));
        params.put("end_time", new Date().getTime());
        param.put("param", params);
        JSONObject result = APIUtil.getApiResult(KuaishouAPIConstant.VIDEO_QUERY, token.getAccessToken(), param);
        if (Check.isNotNull(result)) {
            return ResultResponse.success(result.getJSONObject("data").getJSONArray("details"));
        }
        return null;
    }

}