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.KuaiShouCreative;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCreativeMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouVideoGetMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreativeService;
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.List;

/**
 * @Description: 快手-创意信息
 * @Author: jeecg-boot
 * @Date: 2019-07-23
 * @Version: V1.0
 */
@Service
public class KuaiShouCreativeServiceImpl extends ServiceImpl<KuaiShouCreativeMapper, KuaiShouCreative> implements IKuaiShouCreativeService {
    @Autowired
    private KuaiShouCreativeMapper kuaiShouCreativeMapper;
    @Autowired
    private KuaiShouVideoGetMapper kuaiShouVideoGetMapper;

    @Override
    public void replaceBatch(List<KuaiShouCreative> creatives) {
        kuaiShouCreativeMapper.replaceBatch(creatives);
    }

    @Override
    public String getCodeByCreativeId(Long creativeId) {
        QueryWrapper<KuaiShouCreative> creativeQueryWrapper = new QueryWrapper<>();
        creativeQueryWrapper.eq("creative_id", creativeId);
        creativeQueryWrapper.orderByDesc("create_time");
        creativeQueryWrapper.last("limit 1");
        KuaiShouCreative kuaiShouCreative = kuaiShouCreativeMapper.selectOne(creativeQueryWrapper);
        if (!Check.isNull(kuaiShouCreative)) {
            String photoId = kuaiShouCreative.getPhotoId();
            if (!Check.isNull(photoId)) {
                QueryWrapper<KuaiShouVideoGet> videoGetQueryWrapper = new QueryWrapper<>();
                videoGetQueryWrapper.eq("photo_id", photoId);
                videoGetQueryWrapper.orderByDesc("create_time");
                videoGetQueryWrapper.last("limit 1");
                KuaiShouVideoGet kuaiShouVideoGet = kuaiShouVideoGetMapper.selectOne(videoGetQueryWrapper);
                if (!Check.isNull(kuaiShouVideoGet)) {
                    return kuaiShouVideoGet.getSignature();
                }
            }
        }
        return null;
    }

    /**
     * 创意条数
     *
     * @param accountId
     * @param unitId
     * @return
     */
    @Override
    public Integer checkCreativeCount(Long accountId, Long unitId) {
        return kuaiShouCreativeMapper.checkCreativeCount(accountId, unitId);
    }
}
