package com.xxl.job.executor.batch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.executor.batch.entity.KuaiShouCampaign;
import com.xxl.job.executor.batch.mapper.KuaiShouCampaignMapper;
import com.xxl.job.executor.batch.service.IKuaiShouCampaignService;
import com.xxl.job.executor.utils.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 快手-获取广告计划信息
 * @Author: jeecg-boot
 * @Date: 2019-07-23
 * @Version: V1.0
 */
@Service
public class KuaiShouCampaignServiceImpl extends ServiceImpl<KuaiShouCampaignMapper, KuaiShouCampaign> implements IKuaiShouCampaignService {
    @Autowired
    private KuaiShouCampaignMapper campaignMapper;

    @Override
    public void replaceBatch(List<KuaiShouCampaign> campaigns) {
        campaignMapper.replaceBatch(campaigns);
    }


    /**
     * 获取创意
     *
     * @param accountId
     * @return
     */
    @Override
    public List<JSONArray> getJsonArrByAccount(Long accountId, int idSize) {
        QueryWrapper<KuaiShouCampaign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        List<KuaiShouCampaign> list = this.list(queryWrapper);
        List<JSONArray> campaignArr = new ArrayList<>();
        if (!Check.isNull(list)) {
            int arrSize = list.size() % idSize == 0 ? list.size() / idSize : list.size() / idSize + 1;
            for (int i = 0; i < arrSize; i++) {
                JSONArray arr = new JSONArray();
                for (int j = i * idSize; j <= idSize * (i + 1) - 1; j++) {
                    if (j <= list.size() - 1) {
                        KuaiShouCampaign campaign = list.get(i);
                        arr.add(campaign.getCampaignId());
                    }
                }
                campaignArr.add(arr);
            }
            return campaignArr;
        }

        return null;
    }

}
