package cn.com.ctop.kuaishou.modules.batch.controller;

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.kuaishou.modules.batch.service.IKuaishouRealTimeDataService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 广告组-app信息
 * @Author: jeecg-boot
 * @Date: 2019-07-23
 * @Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/kuaishou/realTime")
public class KuaiShouRealTimeController {
    @Autowired
    private IKuaishouRealTimeDataService realTimeDataService;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;


    @PostMapping(value = "/getCampaignDate")
    public Result<JSONArray> getCampaignDate(@RequestBody JSONObject requestJson) {
        Result<JSONArray> result = new Result<>();
        try {
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("请传入账户id");
            }
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            Long campaignId = requestJson.getLong("campaignId");
            if (Check.isNull(campaignId)) {
                throw new Exception("请传入计划id");
            }
            String startDate = requestJson.getString("startDate");
            String endDate = requestJson.getString("endDate");
            if (Check.isNull(startDate) || Check.isNull(endDate)) {
                throw new Exception("请选择开始、结束查询时间");
            }
            JSONArray campaignDate = realTimeDataService.getCampaignDate(accountId, oauthToken.getAccessToken(), campaignId, startDate, endDate);
            result.setSuccess(true);
            result.setResult(campaignDate);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;
    }


    @PostMapping(value = "/getUnitDate")
    public Result<JSONArray> getUnitDate(@RequestBody JSONObject requestJson) {
        Result<JSONArray> result = new Result<>();
        try {
            Long accountId = requestJson.getLong("accountId");
            if (Check.isNull(accountId)) {
                throw new Exception("请传入账户id");
            }
            CtopOauthToken oauthToken = oauthTokenService.getTokenByAccountId(accountId);
            if (Check.isNull(oauthToken)) {
                throw new Exception("未获取到账户信息");
            }
            Long unitId = requestJson.getLong("unitId");
            if (Check.isNull(unitId)) {
                throw new Exception("请传入组id");
            }
            String startDate = requestJson.getString("startDate");
            String endDate = requestJson.getString("endDate");
            if (Check.isNull(startDate) || Check.isNull(endDate)) {
                throw new Exception("请选择开始、结束查询时间");
            }
            JSONArray unitData = realTimeDataService.getUnitDate(accountId, oauthToken.getAccessToken(), unitId, startDate, endDate);
            result.setSuccess(true);
            result.setResult(unitData);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;
    }

}
