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.statistics.api.dto.StatisticsAdPlayFreeTimeDto;
import com.sptg.statistics.api.entity.StatisticsAdPlayFreeTime;
import com.sptg.statistics.biz.mapper.StatisticsAdPlayFreeTimeMapper;
import com.sptg.statistics.biz.service.StatisticsAdPlayFreeTimeServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * title: StatisticsAdWatchFreeTimeServiceImpl
 * package: com.sptg.statistics.biz.security.impl
 * description: 闲时广告播放-服务层
 *
 *                
 *               
 */
@Service
public class StatisticsAdPlayFreeTimeServiceImpl extends ServiceImpl<StatisticsAdPlayFreeTimeMapper, StatisticsAdPlayFreeTime> implements StatisticsAdPlayFreeTimeServiceInterface {

    @Override
    public  Page<StatisticsAdPlayFreeTime> showAdRankList(PageBean<StatisticsAdPlayFreeTime, StatisticsAdPlayFreeTimeDto> pageBean){
        StatisticsAdPlayFreeTimeDto dto = pageBean.getDto();
        //条件
        EntityWrapper<StatisticsAdPlayFreeTime> wrapper=new EntityWrapper<>();
        //SELECT 查询字段
        wrapper.setSqlSelect("SUM(1) AS 'playCount',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("playCount");
        wrapper.orderDesc(orderByList);
        return this.selectPage(pageBean, wrapper);
    }

    @Override
    public long getAdPlayCount(StatisticsAdPlayFreeTimeDto dto){
        EntityWrapper<StatisticsAdPlayFreeTime> wrapper=new EntityWrapper<>();
        wrapper.setSqlSelect("COUNT(l_id)");
        //时间范围
        //开始时间
        Date startDate = dto.getStartDate();
        if(startDate!=null){
            wrapper.ge("t_watch_date",startDate);
        }
        //结束时间
        Date endDate = dto.getEndDate();
        if(endDate!=null){
            wrapper.lt("t_watch_date",endDate);
        }
        //按区域查询
        //省
        String province=dto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_region_province",province);
        }
        //市
        String city=dto.getRegionCity();
        if(StringUtils.isNotBlank(city)){
            wrapper.eq("s_region_city",city);
        }
        //区
        String area=dto.getRegionArea();
        if(StringUtils.isNotBlank(area)){
            wrapper.eq("s_region_area",area);
        }
        //查询某月数据
        if(dto.getSelectMonth()!=null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y%m')",sdf.format(dto.getSelectMonth()));
        }
        //查询某周数据
        if(dto.getYearWeek()!=null){
            wrapper.eq("YEARWEEK(date_format(t_watch_date,'%Y-%m-%d'),1)",dto.getYearWeek());
        }
        //按设备序列号
        String serialNumber=dto.getSerialNumber();
        if(StringUtils.isNotBlank(serialNumber)){
            wrapper.eq("s_device_serial_number",serialNumber);
        }
        //机构位置
        String orgPosition=dto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        long count= (long) this.selectObj(wrapper);
        return count;
    }
}
