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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouPopulationPackageRel;
import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouPopulationPackageMapper;
import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouPopulationPackageRelMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouPopulationPackageRelService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouPopulationService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class CrowdListQueryJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouPopulationService iKuaishouPopulationService;
    @Autowired
    private KuaishouPopulationPackageMapper kuaishouPopulationPackageMapper;
    @Autowired
    private KuaishouPopulationPackageRelMapper relMapper;
    @Autowired
    private IKuaishouPopulationPackageRelService relService;
    static ExecutorService executorService = Executors.newFixedThreadPool(3);

    /**
     * 定时更新人群包状态
     */
    @XxlJob("crowdListQueryJob")
    public void execute() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <= 7; i++) {
            if (i != 2) {
                list.add(i);
            }
        }
        List<JSONObject> objs = kuaishouPopulationPackageMapper.queryListByStatus();
        objs.forEach(o -> executorService.submit(() -> {
            Long accountId = o.getLong("account_id");
            Long projectId = o.getLong("project_id");
            Hashtable newIds = new Hashtable();
            CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
            if (!Check.isNull(token)) {
                for (Integer status : list) {
                    JSONObject jsonObject = iKuaishouPopulationService.getPopulationList(accountId, token.getAccessToken(), status);
                    if (jsonObject.getInteger("code") == 0) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        if (!Check.isNull(data)) {
                            JSONArray details = data.getJSONArray("details");
                            if (details.size() > 0) {
                                List<KuaishouPopulationPackageRel> rellist = new ArrayList<>();
                                for (int j = 0; j < details.size(); j++) {
                                    JSONObject obj = details.getJSONObject(j);
                                    KuaishouPopulationPackageRel rel = JSONObject.parseObject(obj.toJSONString(), KuaishouPopulationPackageRel.class);
                                    newIds.put(rel.getOrientationId(), rel.getOrientationId());
                                    if (Check.isNull(relMapper.queryByOrientationId(rel.getOrientationId(), rel.getAccountId()))) {
                                        rel.setProjectId(projectId);
                                        rel.setDataStatus(1);
                                        rel.setStatDate(DateUtils.formatDate(new Date()));
                                        rellist.add(rel);
                                    } else {
                                        rel.setDataStatus(1);
                                        relMapper.updateStatus(rel);
                                    }
                                }
                                if (!Check.isNull(rellist)) {
                                    relMapper.replaceBatch(rellist);
                                }
                            }
                        }
                    }
                }
                //查询现有的人群包ID集
                List<Long> oldOrientationIds = relMapper.queryByOrientationId(null, accountId);
                if (oldOrientationIds.size() != newIds.size()) {
                    for (Long id : oldOrientationIds) {
                        if (!newIds.contains(id)) {
                            KuaishouPopulationPackageRel rel = new KuaishouPopulationPackageRel();
                            rel.setDataStatus(0);
                            rel.setOrientationId(id);
                            rel.setAccountId(accountId);
                            rel.setMessage("人群包已从媒体后台删除");
                            relMapper.updateStatus(rel);
                        }
                    }
                }
            }
        }));
        XxlJobHelper.log("快手人群报表更新成功");
    }
}
