package cn.com.ctop.kuaishou.modules.report.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.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouGroupExplore;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouGroupExploreMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouGroupExploreService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 广告组的加速探索报表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-02-02
 */
@Slf4j
@Service
public class KuaishouGroupExploreServiceImpl extends ServiceImpl<KuaishouGroupExploreMapper, KuaishouGroupExplore> implements IKuaishouGroupExploreService {
    @Autowired
    private ICtopOauthTokenService tokenService;


    /**
     * 查询加速探索信息
     *
     * @param
     * @throws
     * @author ZHAOXA
     */
    @Override
    public Result<Object> unitExploreInfoGet(JSONObject data) {
        try {
            Long accountId = data.getLong("accountId");
            if (Check.isNull(accountId)) {
                return Result.error("请选择广告主");
            }
            Long unitId = data.getLong("unitId");
            if (Check.isNull(unitId)) {
                return Result.error("请选择广告组");
            }
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.UNIT_EXPLORE_INFO_GET;
            Map<String, String> header = new HashMap<>();
            header.put("Access-Token", token.getAccessToken());
            header.put("Content-Type", "application/json");
            JSONObject params = new JSONObject();
            params.put("advertiser_id", accountId);
            params.put("unit_id", unitId);
            String result = HttpUtils.httpPostRequest(url, params, header);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    return Result.ok(resultJson.getJSONObject("data"));
                } else {
                    log.info("查询加速探索信息失败，accountId：{}，返回信息：{}", accountId, resultJson);
                    return Result.error("查询加速探索信息失败，" + resultJson.getString("message"));
                }
            }
        } catch (Exception e) {
            log.error("查询加速探索信息失败", e);
        }
        return Result.error("查询加速探索信息失败");
    }

    /**
     * 广告组的加速探索预算设置
     *
     * @param
     * @throws
     * @author ZHAOXA
     */
    @Override
    public Result<Object> unitExploreBudgetUpdate(JSONObject data) {
        try {
            Long accountId = data.getLong("accountId");
            if (Check.isNull(accountId)) {
                return Result.error("请选择广告主");
            }
            Long unitId = data.getLong("unitId");
            if (Check.isNull(unitId)) {
                return Result.error("请选择广告组");
            }
            Long exploreBudget = data.getLong("exploreBudget");
            if (Check.isNull(exploreBudget)) {
                return Result.error("请填写加速探索预算");
            }
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.UNIT_EXPLORE_BUDGET_UPDATE;
            Map<String, String> header = new HashMap<>();
            header.put("Access-Token", token.getAccessToken());
            header.put("Content-Type", "application/json");
            JSONObject params = new JSONObject();
            params.put("advertiser_id", accountId);
            params.put("unit_id", unitId);
            params.put("explore_budget", exploreBudget);
            String result = HttpUtils.httpPostRequest(url, params, header);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    return Result.ok(resultJson.getJSONObject("data"));
                } else {
                    log.info("广告组的加速探索预算设置失败，accountId：{}，返回信息：{}", accountId, resultJson);
                    return Result.error("广告组的加速探索预算设置失败，" + resultJson.getString("message"));
                }
            }
        } catch (Exception e) {
            log.error("广告组的加速探索预算设置失败", e);
        }
        return Result.error("广告组的加速探索预算设置失败");
    }

    /**
     * 暂停广告组的加速探索预算接口
     *
     * @param
     * @throws
     * @author ZHAOXA
     */
    @Override
    public Result<Object> unitExploreStatusPause(JSONObject data) {
        try {
            Long accountId = data.getLong("accountId");
            if (Check.isNull(accountId)) {
                return Result.error("请选择广告主");
            }
            Long unitId = data.getLong("unitId");
            if (Check.isNull(unitId)) {
                return Result.error("请选择广告组");
            }
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.UNIT_EXPLORE_STATUS_PAUSE;
            Map<String, String> header = new HashMap<>();
            header.put("Access-Token", token.getAccessToken());
            header.put("Content-Type", "application/json");
            JSONObject params = new JSONObject();
            params.put("advertiser_id", accountId);
            params.put("unit_id", unitId);
            String result = HttpUtils.httpPostRequest(url, params, header);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    return Result.ok(resultJson.getJSONObject("data"));
                } else {
                    log.info("暂停广告组的加速探索预算失败，accountId：{}，返回信息：{}", accountId, resultJson);
                    return Result.error("暂停广告组的加速探索预算失败，" + resultJson.getString("message"));
                }
            }
        } catch (Exception e) {
            log.error("暂停广告组的加速探索预算失败", e);
        }
        return Result.error("暂停广告组的加速探索预算失败");
    }

    /**
     * 查询广告组的加速探索报表接口
     *
     * @param
     * @throws
     * @author ZHAOXA
     */
    @Override
    public Result<Object> querySpeedExploreReport(JSONObject data) {
        try {
            Long accountId = data.getLong("accountId");
            if (Check.isNull(accountId)) {
                return Result.error("请选择广告主");
            }
            Long unitId = data.getLong("unitId");
            if (Check.isNull(unitId)) {
                return Result.error("请选择广告组");
            }
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.QUERY_SPEED_EXPLORE_REPORT;
            Map<String, String> header = new HashMap<>();
            header.put("Access-Token", token.getAccessToken());
            header.put("Content-Type", "application/json");
            JSONObject params = new JSONObject();
            params.put("advertiser_id", accountId);
            params.put("unit_id", unitId);
            String result = HttpUtils.httpPostRequest(url, params, header);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    JSONObject exploreJson = resultJson.getJSONObject("data");
                    KuaishouGroupExplore kuaishouGroupExplore = JSONObject.toJavaObject(exploreJson, KuaishouGroupExplore.class);
                    if (Check.isNull(kuaishouGroupExplore)) {
                        return Result.error("查询失败，结果为空");
                    }
                    QueryWrapper<KuaishouGroupExplore> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("unit_id", kuaishouGroupExplore.getUnitId());
                    KuaishouGroupExplore one = this.getOne(queryWrapper);
                    if (!Check.isNull(one)) {
                        kuaishouGroupExplore.setId(one.getId());
                    }
                    kuaishouGroupExplore.setAccountId(accountId);
                    this.saveOrUpdate(kuaishouGroupExplore);
                    return Result.ok("添加成功");
                } else {
                    log.info("查询广告组的加速探索报表失败，accountId：{}，返回信息：{}", accountId, resultJson);
                    return Result.error("查询广告组的加速探索报表失败，" + resultJson.getString("message"));
                }
            }
        } catch (Exception e) {
            log.error("查询广告组的加速探索报表失败", e);
        }
        return Result.error("查询广告组的加速探索报表失败");
    }


}
