package cn.com.ctop.kuaishou.modules.batch.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCampaign;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCampaignMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCampaignService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;
    }

    @Override
    public List<Long> queryCampaignIdByAccountId(Long accountId) {
        return campaignMapper.queryCampaignIdByAccountId(accountId);
    }

    @Override
    public KuaiShouCampaign getCampaignByAccountAndCampaign(Long accountId, Long campaignId) {
        QueryWrapper<KuaiShouCampaign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("campaign_id", campaignId);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        KuaiShouCampaign campaign = campaignMapper.selectOne(queryWrapper);
        return campaign;
    }

    @Override
    public Integer getNewDataByAccountIdAndDate(Long accountId, String statDate) {
        return campaignMapper.getNewDataByAccountIdAndDate(accountId, statDate);
    }

    @Override
    public Long getCampaignIdByAccountAndName(Long accountId, String replaceString) {
        return campaignMapper.getCampaignIdByAccountAndName(accountId, replaceString);
    }

    @Override
    public Long getCampaignIdByAccountAndNameAndDate(Long accountId, String replaceString, String nowDate) {
        return campaignMapper.getCampaignIdByAccountAndNameAndDate(accountId, replaceString, nowDate);
    }

    @Override
    public List<JSONObject> getCampaignIdsByParameters(Long accountId, String typeName, String nowDate) {
        return campaignMapper.getCampaignIdsByParameters(accountId, typeName, nowDate);
    }

}
