package com.sptg.ad.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.ad.api.entity.AdPushContent;
import com.sptg.ad.biz.mapper.AdPushContentMapper;
import com.sptg.ad.biz.service.AdPushContentServiceInterface;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * title: AdContentImgServiceImpl
 * package: com.sptg.ad.biz.security.impl
 * description: 广告发布任务内容-接口实现类
 *
 *               
 *                 
 */
@Service
public class AdPushContentServiceImpl extends ServiceImpl<AdPushContentMapper, AdPushContent> implements AdPushContentServiceInterface {


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> addAdPushContent(AdPushContent adPushContent) {
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(adPushContent));
    }

    @Override
    public List<AdPushContent> getAdPushContents(Long taskId) {
        Wrapper<AdPushContent> wrapper = new EntityWrapper<>();
        //根据任务id查询
        wrapper.eq("l_task_id",taskId);
        return this.selectList(wrapper);

    }

    @Override
    public List<Long> getTaskIds(AdPushContent adPushContent){
        Wrapper<AdPushContent> wrapper = new EntityWrapper<>();
        //根据广告标题查询
        if(StringUtils.isNotBlank(adPushContent.getAdTitle())){
            wrapper.like("s_ad_title",adPushContent.getAdTitle());
        }
        //根据广告编号查询
        if(StringUtils.isNotBlank(adPushContent.getAdCode())){
            wrapper.like("s_ad_code",adPushContent.getAdCode());
        }
        //根据广告类型查询
        if(adPushContent.getAdTypeId()!=null){
            wrapper.eq("l_ad_type_id",adPushContent.getAdTypeId());
        }
        wrapper.like("s_org_position",adPushContent.getOrgPosition());
        List<AdPushContent> list = this.baseMapper.getTaskIds(wrapper);
        if(list!=null && list.size()>0){
            List<Long> taskIds = list.stream().map(AdPushContent::getTaskId).collect(Collectors.toList());
            return taskIds;
        }else{
            return null;
        }
    }

    @Override
    public void updateOrgPosition(String oldPosition, String newPosition) {
        this.baseMapper.updateOrgPosition(oldPosition,newPosition);
    }
}
