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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
import cn.com.ctop.common.module.mapper.UserAllocationMapper;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * @Description: 快手-本地图片保存
 * @Author: jeecg-boot
 * @Date: 2019-08-14
 * @Version: V1.0
 */
@Slf4j

@RestController
@RequestMapping("/kuaishou/getReport")
public class KuaiShouReportController {
    @Autowired
    private UserAllocationMapper userAllocationMapper;
    @Autowired
    private CtopOauthTokenMapper ctopOauthTokenMapper;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    @RequestMapping(value = "/historyData")
    public String queryPageList() throws ParseException {
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_id", 2);
        queryWrapper.groupBy("account_id");
        List<UserAllocation> userAllocations = userAllocationMapper.selectList(queryWrapper);
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        if (!Check.isNull(userAllocations)) {
            for (UserAllocation allocation : userAllocations) {
                String beginDate = null;
                String hourDate = null;
                Long accountId = allocation.getAccountId();
                if (!Check.isNull(accountId)) {
                    QueryWrapper<CtopOauthToken> tokenQueryWrapper = new QueryWrapper<>();
                    tokenQueryWrapper.eq("account_id", accountId);
                    tokenQueryWrapper.eq("media_id", 2);
                    tokenQueryWrapper.orderByDesc("create_time");
                    tokenQueryWrapper.last("limit 1");
                    CtopOauthToken ctopOauthToken = ctopOauthTokenMapper.selectOne(tokenQueryWrapper);
                    if (!Check.isNull(ctopOauthToken)) {
                        String accessToken = ctopOauthToken.getAccessToken();
                        Long advertiserId = ctopOauthToken.getAccountId();
                        for (int i = 0; i < 6; i++) {
                            if (i == 0) {
                                String monthBefore = DateUtils.getMonthBefore("yyyy-MM-dd", nowDate, -1);
                                kuaishouInterfaceService.getAccountDailyReport(accessToken, advertiserId, monthBefore, anotherDay, 500, 1);
                                beginDate = monthBefore;
                            } else if (i > 0) {
                                String startDate = DateUtils.getAnotherDay("yyyy-MM-dd", beginDate, -1);
                                String monthBefore = DateUtils.getMonthBefore("yyyy-MM-dd", beginDate, -1);
                                if (i == 5) {
                                    monthBefore = DateUtils.getAnotherDay("yyyy-MM-dd", monthBefore, 4);
                                }
                                kuaishouInterfaceService.getAccountDailyReport(accessToken, advertiserId, monthBefore, startDate, 500, 1);
                                beginDate = monthBefore;
                            }
                        }
                        for (int i = 0; i < 7; i++) {
                            if (i == 0) {
                                kuaishouInterfaceService.getAccountHourlyReport(accessToken, advertiserId, nowDate, nowDate, 500, 1);
                                hourDate = nowDate;
                            } else if (i > 0) {
                                String hourAnotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", hourDate, -1);
                                kuaishouInterfaceService.getAccountHourlyReport(accessToken, advertiserId, hourAnotherDay, hourAnotherDay, 500, 1);
                                hourDate = hourAnotherDay;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
}
