package cn.com.ctop.job.bytedance.handler;

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IRefreshTokenService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.HttpUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Component
public class ByteDanceRefreshTokenJob {

    @Autowired
    private IRefreshTokenService refreshTokenService;
    private static final String REFRESH_AUTHNAME_URL = "http://api.bjhcstszkj.cn/jeecg-boot/ctop/userAllocation/updateAuthName?";
    /**
     * 刷新token
     * @throws Exception
     */
    @XxlJob("byteDanceRefreshTokenJob")
    public void execute() throws Exception {
        refreshTokenService.byteDanceRefresh(null);
    }


    /**
     * 刷新代理商token
     * @throws Exception
     */
    @XxlJob("bytedanceRefreshAgentTokenJob")
    public void bytedanceRefreshAgentTokenJob() throws Exception {
        refreshTokenService.bytedanceRefreshAgentToken(null);
    }

    @Autowired
    private IUserAllocationService userAllocationService;

    /**
     * 刷新账户名称
     * @throws Exception
     */
    @XxlJob("refreshAuthName")
    public void refreshAuthName() throws Exception {
        List<UserAllocation> userAllocationList = userAllocationService.listByMediaId(null,0);
        if(null!=userAllocationList&&!userAllocationList.isEmpty()){
            userAllocationList.forEach(userAllocation -> {
                String url = REFRESH_AUTHNAME_URL+"accountId="+userAllocation.getAccountId()+"&mediaId="+userAllocation.getMediaId();
                HttpUtils.httpGet(url,new HashMap<>(),new HashMap<>());
            });
        }
    }

    /**
     * 定时关停头条账户
     */
    @XxlJob("closeExpiredByteDanceAccountJob")
    public void closeExpiredByteDanceAccountJob() {
        LocalDateTime now = LocalDateTime.now();
        String createTimeLimit = now.minusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int statDatetimeLimit = Integer.parseInt(now.minusDays(7).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        int i = userAllocationService.closeExpiredByteDanceAccounts(createTimeLimit, statDatetimeLimit);
        log.info("关停头条账户数：{}",i);
    }



    /**
     * 定时关停头条账户
     */
    @XxlJob("closeExpiredKuaiShouAccountJob")
    public void closeExpiredKuaiShouAccountJob() {
        LocalDateTime now = LocalDateTime.now();
        String createTimeLimit = now.minusDays(7).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int statDatetimeLimit = Integer.parseInt(now.minusDays(15).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        int i = userAllocationService.closeExpiredKuaiShouAccounts(createTimeLimit, statDatetimeLimit);
        log.info("关停快手账户数：{}",i);
    }


    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        String createTimeLimit = now.minusDays(7).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int statDatetimeLimit = Integer.parseInt(now.minusDays(15).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        System.err.println(createTimeLimit);
        System.err.println(statDatetimeLimit);
    }




}
