package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.graphql.service.IKuaishouWebInterfaceService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.ctop.service.ICreateInternalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author jeecg-boot
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private ICreateInternalService createInternalService;
    @Autowired
    private IKuaishouWebInterfaceService kuaishouWebInterfaceService;

    @PostMapping(value = "/create")
    public Map<String, Object> authorization(@RequestBody JSONObject jsonObject) {
        return createInternalService.createInternal(jsonObject);
    }

    @GetMapping(value = "/t")
    public String test() {
        Result<String> result = new Result<String>();
        try {
            String requestId = UUID.randomUUID().toString();
            String img = kuaishouWebInterfaceService.qrLogin(requestId);
            if (img != null) {
                result.setSuccess(true);
                result.setResult(img);
            } else {
                result.setSuccess(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return "success";
    }


    @Autowired
    private CtopOauthTokenMapper tokenMapper;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    @GetMapping(value = "/getGroupData")
    public String getGroupData() {


        Date endDate = new Date();
        QueryWrapper<CtopOauthToken> tokenQueryWrapper = new QueryWrapper<>();
        tokenQueryWrapper.eq("media_id", 2);
        List<CtopOauthToken> ctopOauthTokens = tokenMapper.selectList(tokenQueryWrapper);
        for (CtopOauthToken token : ctopOauthTokens) {


            for (int i = 0; i < 7; i++) {
                Date getStartDate = DateUtils.addDay(endDate, -i);

                //3：获取广告组信息数据
                kuaishouInterfaceService.getAdvertiserGroupReportHourly(token, getStartDate, getStartDate);

            }
            for (int i = 0; i < 6; i++) {
                Date getStartDate = new Date();
                Date getEndDate = new Date();
                if (i == 0) {
                    getStartDate = DateUtils.addDay(endDate, -175 + i * 30);
                    getEndDate = DateUtils.addDay(endDate, -150 + i * 30);
                } else {
                    getStartDate = DateUtils.addDay(endDate, -180 + i * 30);
                    getEndDate = DateUtils.addDay(endDate, -150 + i * 30);
                }

                //3：获取广告组信息数据
                kuaishouInterfaceService.getAdvertiserGroupReportDaily(token, getStartDate, getEndDate);

            }


        }
        return "Success";
    }
}
