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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.statistics.api.dto.StatisticsAdPushCountDto;
import com.sptg.statistics.api.entity.StatisticsAdPushCount;
import com.sptg.statistics.biz.mapper.StatisticsAdPushCountMapper;
import com.sptg.statistics.biz.service.StatisticsAdPushCountServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * title: StatisticsAdPushServiceImpl
 * package: com.sptg.statistics.biz.security.impl
 * description: 统计发布广告数量-服务层实现类
 *
 *                
 *                
 */
@Service
public class StatisticsAdPushCountServiceImpl extends ServiceImpl<StatisticsAdPushCountMapper, StatisticsAdPushCount> implements StatisticsAdPushCountServiceInterface {

    @Override
    public StatisticsAdPushCount getStatisticsAdPushCountByTaskId(StatisticsAdPushCountDto statisticsAdPushCountDto){
        EntityWrapper<StatisticsAdPushCount> wrapper=new EntityWrapper<StatisticsAdPushCount>();
        //发布任务id
        Long adPushTaskId = statisticsAdPushCountDto.getAdPushTaskId();
        if(adPushTaskId==null||adPushTaskId<=0){
           return null;
        }
        wrapper.eq("l_ad_push_task_id",adPushTaskId);
        //发布推送状态
        Integer pushState = statisticsAdPushCountDto.getPushState();
        if(pushState!=null){
            wrapper.eq("i_push_state",pushState);
        }
        return (StatisticsAdPushCount) this.selectOne(wrapper);
    }

    @Override
    public long getAdPushCount(StatisticsAdPushCountDto statisticsAdPushCountDto){
        Map<String,Object> resultMap=null;
        EntityWrapper<StatisticsAdPushCount> wrapper=new EntityWrapper<StatisticsAdPushCount>();
        wrapper.setSqlSelect("COUNT(l_id)");

        //发布任务id
        Long adPushTaskId = statisticsAdPushCountDto.getAdPushTaskId();
        if(adPushTaskId!=null&&adPushTaskId>0){
            wrapper.eq("l_ad_push_task_id",adPushTaskId);
        }
        //发布推送状态
        Integer pushState = statisticsAdPushCountDto.getPushState();
        if(pushState!=null){
            wrapper.eq("i_push_state",pushState);
        }
        //机构位置
        if(StringUtils.isNotBlank(statisticsAdPushCountDto.getOrgPosition())) {
            wrapper.like("s_org_position", statisticsAdPushCountDto.getOrgPosition(), SqlLike.RIGHT);
        }
        long count=(long)this.selectObj(wrapper);
        return count;
    }

    @Override
    public boolean updateAdPushState(StatisticsAdPushCountDto StatisticsAdPushCountDto){
        Long adPushTaskId = StatisticsAdPushCountDto.getAdPushTaskId();
        Integer pushState = StatisticsAdPushCountDto.getPushState();
        if(adPushTaskId==null||adPushTaskId<=0||pushState==null){
            return false;
        }
        int count=this.baseMapper.updatePushStateByTaskId(adPushTaskId,pushState);
        boolean result=count>0?true:false;
        return result;
    }

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