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.kuaishou.modules.batch.service.IKuaishouInterfaceService2;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Slf4j
@Api(tags = "快手对接接口类")
@RestController
@RequestMapping("/kuaishou/interface")
public class KuaiShouInterfaceController {
    @Autowired
    private IKuaishouInterfaceService2 kuaishouInterfaceService2;
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = Executors.newFixedThreadPool(5);

    @GetMapping(value = "/getOperationRecord")
    public Result getOperationRecord(@Param("accountId") Long accountId, @Param("operationTarget") Integer operationTarget, @Param("startDate") String startDate, @Param("endDate") String endDate) {
        Result result = new Result();
        try {
            if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
                result.setSuccess(false);
                result.setMessage("时间参数不能为空");
                return result;

            }

            if (operationTarget == null) {
                result.setSuccess(false);
                result.setMessage("类型不能为空");
                return result;
            }
            if (accountId == null) {
                //1:查询当日数据
                QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("media_id", 2);
                queryWrapper.orderByDesc("create_time");
                List<CtopOauthToken> tokens = tokenService.list(queryWrapper);
                if (null == tokens || tokens.size() <= 0) {
                    log.error("定时获取快手操作记录数据异常：未获取到可用的token");
                    result.setSuccess(false);
                    result.setMessage("定时获取快手操作记录数据异常：未获取到可用的toke");
                    return result;
                }

                executorService = Executors.newFixedThreadPool(8);
                tokens.forEach(token -> {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            log.info("获取操作记录开始:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
                            kuaishouInterfaceService2.getOperationRecord(token.getAccountId(), token.getAccessToken(), operationTarget, startDate, endDate);
                            log.info("获取操作记录结束:accountId:{}", token.getAccountId());
                        }
                    });
                });
            } else {
                QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("account_id", accountId);
                CtopOauthToken token = tokenService.getOauthTokenByAccountId(String.valueOf(accountId));
                if (null == token) {
                    log.error("定时获取快手操作记录数据异常：未获取到可用的token");
                    result.setSuccess(false);
                    result.setMessage("定时获取快手操作记录数据异常：未获取到可用的toke");
                    return result;
                }
                log.info("获取快手操作记录开始:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
                kuaishouInterfaceService2.getOperationRecord(token.getAccountId(), token.getAccessToken(), operationTarget, startDate, endDate);
                log.info("获取快手操作记录结束:accountId:{},startDate:{},endDate:{}", token.getAccountId(), startDate, endDate);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


}
