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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouAccountCreativeOverrunInfo;
import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouAccountCreativeOverrunInfoMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouAccountCreativeOverrunInfoService;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCreative;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouVideoGetMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreativeService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouUpdateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxl.job.core.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 快手账户创意创建超限记录信息
 * @author jeecg-boot
 * @date   2021-01-30
 * @version V1.0
 */
@Slf4j
@Service
public class KuaishouAccountCreativeOverrunInfoServiceImpl extends ServiceImpl<KuaishouAccountCreativeOverrunInfoMapper, KuaishouAccountCreativeOverrunInfo> implements IKuaishouAccountCreativeOverrunInfoService {
    @Resource
    private KuaiShouVideoGetMapper videoGetMapper;
    @Autowired
    private IKuaiShouCreativeService kuaiShouCreativeService;
    @Autowired
    private IKuaiShouUpdateService kuaiShouUpdateService;
    @Autowired
    private ICtopOauthTokenService tokenService;

    @Override
    public void stopCreativeBySignature(Long accountId, String signature) {
        List<KuaiShouVideoGet> videoGets =videoGetMapper.getPhotoIdsByParams(accountId,signature);
        if(null==videoGets||videoGets.isEmpty()){
            log.info("该账户下不存在此视频>>accountId:{},视频编码:{}",accountId,signature);
            return;
        }
        CtopOauthToken token = tokenService.getTokenByAccountId(accountId);
        videoGets.forEach(videoGet -> {
            String photoId = videoGet.getPhotoId();
            Integer putStatus = 52;
            List<KuaiShouCreative> creatives = kuaiShouCreativeService.getByParams(accountId,photoId,putStatus);
            if(null!=creatives&&!creatives.isEmpty()){
                creatives.forEach(creative->{
                    kuaiShouUpdateService.updateCreativeStatus(token.getAccessToken(),token.getAccountId(),creative.getCreativeId(),2,"");
                });
            }
        });
    }

    @Override
    public void updateOverrunInfo(Long advertiserId) {
        String date = DateUtil.formatDate(new Date());
        String dateTime = DateUtil.formatDateTime(new Date());
        KuaishouAccountCreativeOverrunInfo overrunInfo = getByParams(advertiserId,date);
        if(null == overrunInfo){
            overrunInfo = new KuaishouAccountCreativeOverrunInfo(advertiserId,date,dateTime);
            this.save(overrunInfo);
        }
    }

    @Override
    public KuaishouAccountCreativeOverrunInfo getByParams(Long accountId,String date){
        QueryWrapper<KuaishouAccountCreativeOverrunInfo> queryWrapper = new QueryWrapper<>();
        if(null!=accountId&&accountId!=0){
            queryWrapper.eq("account_id",accountId);
        }
        if(null!=date&&!"".equals(date.trim())){
            queryWrapper.eq("state_date",date);
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
