package com.tzld.piaoquan.recommend.server.xxl;

import com.aliyun.odps.data.Record;
import com.tzld.piaoquan.recommend.server.common.RedisKeyConstants;
import com.tzld.piaoquan.recommend.server.service.odps.ODPSManager;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author sunxy
 */
@Component
@Slf4j
public class RickUserCacheJob {

    @Autowired
    private ODPSManager odpsManager;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @XxlJob("updateRiskUserCache")
    public ReturnT<String> upldateRiskUserCache(String params) {
        // 执行 ODPS Sql，获取数据
        Pair<List<String>, List<String>> pairs = queryRiskUser();
        if (pairs == null) {
            XxlJobLogger.log("updateRiskUserCache, data is empty");
            return ReturnT.SUCCESS;
        }
        List<String> uids = pairs.getLeft();
        if (CollectionUtils.isNotEmpty(uids)) {
            redisTemplate.executePipelined(new SessionCallback<String>() {
                @Override
                public <K, V> String execute(RedisOperations<K, V> redisOperations) throws DataAccessException {
                    redisOperations.delete((K) RedisKeyConstants.Recommend.riskUserUid);
                    ((SetOperations<String, String>) redisOperations.opsForSet()).add(RedisKeyConstants.Recommend.riskUserUid,
                            uids.toArray(new java.lang.String[uids.size()]));
                    return null;
                }
            });
        } else {
            redisTemplate.delete(RedisKeyConstants.Recommend.riskUserUid);
        }

        List<String> mids = pairs.getRight();
        if (CollectionUtils.isNotEmpty(mids)) {
            redisTemplate.executePipelined(new SessionCallback<String>() {
                @Override
                public <K, V> String execute(RedisOperations<K, V> redisOperations) throws DataAccessException {
                    redisOperations.delete((K) RedisKeyConstants.Recommend.riskUserMid);
                    ((SetOperations<String, String>) redisOperations.opsForSet()).add(RedisKeyConstants.Recommend.riskUserMid,
                            mids.toArray(new java.lang.String[mids.size()]));
                    return null;
                }
            });
        } else {
            redisTemplate.delete(RedisKeyConstants.Recommend.riskUserMid);
        }
        XxlJobLogger.log("upldateRiskUserCache finish");
        return ReturnT.SUCCESS;
    }

    private Pair<List<String>, List<String>> queryRiskUser() {
        String sql = "SELECT uid,mid FROM loghubods.wx_review_staff_list WHERE dt = MAX_PT('loghubods" +
                ".wx_review_staff_list');";
        List<Record> records = odpsManager.query(sql);
        if (records == null || records.size() == 0) {
            return null;
        }

        List<String> uids = new ArrayList<>();
        List<String> mids = new ArrayList<>();
        for (Record record : records) {
            String uid = String.valueOf(record.getBigint(0));
            if (StringUtils.isNotBlank(uid)) {
                uids.add(uid);
            }

            if (StringUtils.isNotBlank(record.getString(1))) {
                mids.add(record.getString(1));
            }
        }
        return Pair.of(uids, mids);
    }


}
