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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.statistics.api.dto.StatisticsAdClickMobileDto;
import com.sptg.statistics.api.entity.StatisticsAdClickMobile;
import com.sptg.statistics.api.entity.StatisticsCount;
import com.sptg.statistics.biz.mapper.StatisticsAdClickMobileMapper;
import com.sptg.statistics.biz.service.StatisticsAdClickMobileServiceInterface;
import com.sptg.statistics.biz.service.StatisticsCountServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * title: StatisticsAdWatchFreeTimeServiceImpl
 * package: com.sptg.statistics.biz.security.impl
 * description: 移动端广告点击统计-服务层
 *
 *                
 *               
 */
@Service
@RequiredArgsConstructor
public class StatisticsAdClickMobileServiceImpl extends ServiceImpl<StatisticsAdClickMobileMapper, StatisticsAdClickMobile> implements StatisticsAdClickMobileServiceInterface {

    private final StatisticsCountServiceInterface statisticsCountServiceInterface;

    @Override
    public  Page<StatisticsAdClickMobile> showAdRankList(PageBean<StatisticsAdClickMobile, StatisticsAdClickMobileDto> pageBean){
        StatisticsAdClickMobileDto dto = pageBean.getDto();
        //条件
        EntityWrapper<StatisticsAdClickMobile> wrapper=new EntityWrapper<>();
        //SELECT 查询字段
        wrapper.setSqlSelect("SUM(1) AS 'clickCount',s_ad_code AS adCode,s_ad_name AS adName,l_ad_type_id AS adTypeId,s_ad_type_name AS adTypeName");
        //广告类别
        Long adTypeId=dto.getAdTypeId();
        //省
        String regionProvince=dto.getRegionProvince();
        //市
        String regionCity=dto.getRegionCity();
        //区
        String regionArea=dto.getRegionArea();
        //机构位置
        String orgPosition=dto.getOrgPosition();

        if(adTypeId!=null&&adTypeId>0){
            wrapper.eq("l_ad_type_id",adTypeId);
        }
        if(StringUtils.isNotBlank(regionProvince)){
            wrapper.eq("s_region_province",regionProvince);
        }
        if(StringUtils.isNotBlank(regionCity)){
            wrapper.eq("s_region_city",regionCity);
        }
        if(StringUtils.isNotBlank(regionArea)){
            wrapper.eq("s_region_area",regionArea);
        }
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        wrapper.groupBy("l_ad_id");
        List orderByList=new ArrayList();
        orderByList.add("clickCount");
        wrapper.orderDesc(orderByList);
        return this.selectPage(pageBean, wrapper);
    }

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void addAdClickMobile(StatisticsAdClickMobile adClickMobile) {
        boolean result=this.insert(adClickMobile);
        if(result){
            StatisticsCount statisticsCount=new StatisticsCount();
             //添移动端广告点击次数
            statisticsCount.setAdClickMobileCount(OperationConstant.ONE);
            statisticsCount.setRootOrgId(adClickMobile.getRootOrgId());
            statisticsCount.setOrgPosition(adClickMobile.getOrgPosition());
            statisticsCountServiceInterface.addCount(statisticsCount);
        }
    }
}
