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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.statistics.api.dto.StatisticsAdWatchDto;
import com.sptg.statistics.api.entity.StatisticsAdPlayFreeTime;
import com.sptg.statistics.api.entity.StatisticsAdWatch;
import com.sptg.statistics.api.entity.StatisticsCount;
import com.sptg.statistics.biz.mapper.StatisticsAdWatchMapper;
import com.sptg.statistics.biz.service.StatisticsAdPlayFreeTimeServiceInterface;
import com.sptg.statistics.biz.service.StatisticsAdWatchServiceInterface;
import com.sptg.statistics.biz.service.StatisticsCountServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * title: StatisticsAdWatchServiceImpl
 * package: com.sptg.statistics.biz.security.impl
 * description: 统计广告观看-服务层
 *
 *                
 *                 
 */
@Service
@RequiredArgsConstructor
public class StatisticsAdWatchServiceImpl extends ServiceImpl<StatisticsAdWatchMapper, StatisticsAdWatch> implements StatisticsAdWatchServiceInterface {

    private final StatisticsCountServiceInterface statisticsCountServiceInterface;
    private final StatisticsAdPlayFreeTimeServiceInterface adPlayFreeTimeServiceInterface;

    @Override
    public Page<StatisticsAdWatch> showStatisticsAdWatchList(StatisticsAdWatchDto statisticsAdWatchDto) {
        Page<StatisticsAdWatch> page=statisticsAdWatchDto.getPage();
        //条件
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<>();
        //广告编号
        String adCode=statisticsAdWatchDto.getAdCode();
        //广告名称
        String adName=statisticsAdWatchDto.getAdName();
        //广告类别
        Long adTypeId=statisticsAdWatchDto.getAdTypeId();
        //省
        String regionProvince=statisticsAdWatchDto.getRegionProvince();
        //市
        String regionCity=statisticsAdWatchDto.getRegionCity();
        //区
        String regionArea=statisticsAdWatchDto.getRegionArea();
        //开始时间
        Date startCreateDate=statisticsAdWatchDto.getStartDate();
        //结束时间
        Date endCreateDate=statisticsAdWatchDto.getEndDate();
        if(StringUtils.isNotBlank(adCode)){
            wrapper.like("s_ad_code",adCode);
        }
        if(StringUtils.isNotBlank(adName)){
            wrapper.like("s_ad_name",adName);
        }
        if(adTypeId!=null&&adTypeId>0){
            wrapper.eq("l_ad_type_id",adTypeId);
        }
        if(startCreateDate!=null){
            wrapper.ge("t_watch_date",startCreateDate);
        }
        if(endCreateDate!=null){
            wrapper.le("t_watch_date",endCreateDate);
        }
        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);
        }
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //排序
        List<String> descWhere=new ArrayList<String>();
        descWhere.add("t_watch_date");
        wrapper.orderDesc(descWhere);
        return this.selectPage(page,wrapper);
    }

    @Override
    public List<StatisticsAdWatch> getAdWatchList(StatisticsAdWatchDto statisticsAdWatchDto) {
        List<StatisticsAdWatch> resultList=null;
        //条件
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<>();
        if(statisticsAdWatchDto!=null){
                //广告编号
                String adCode=statisticsAdWatchDto.getAdCode();
                //广告名称
                String adName=statisticsAdWatchDto.getAdName();
                //广告类别
                Long adTypeId=statisticsAdWatchDto.getAdTypeId();
                //省
                String regionProvince=statisticsAdWatchDto.getRegionProvince();
                //市
                String regionCity=statisticsAdWatchDto.getRegionCity();
                //区
                String regionArea=statisticsAdWatchDto.getRegionArea();
                //开始时间
                Date startCreateDate=statisticsAdWatchDto.getStartDate();
                //结束时间
                Date endCreateDate=statisticsAdWatchDto.getEndDate();
                if(StringUtils.isNotBlank(adCode)){
                    wrapper.like("s_ad_code",adCode);
                }
                if(StringUtils.isNotBlank(adName)){
                    wrapper.like("s_ad_name",adName);
                }
                if(adTypeId!=null){
                    wrapper.eq("l_ad_type_id",adTypeId);
                }
                if(startCreateDate!=null){
                    wrapper.ge("t_watch_date",startCreateDate);
                }
                if(endCreateDate!=null){
                    wrapper.le("t_watch_date",endCreateDate);
                }
                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);
                }
                //机构位置
                String orgPosition=statisticsAdWatchDto.getOrgPosition();
                //根据机构
                if(StringUtils.isNotBlank(orgPosition)) {
                    wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
                }
        }
        //排序
        wrapper.orderBy("t_watch_date",false);
        resultList= this.selectList(wrapper);
        return resultList;
    }

    @Override
    public  Page<StatisticsAdWatch> showStatisticsAdWatchRankList(StatisticsAdWatchDto statisticsAdWatchDto){
        Page<StatisticsAdWatch> page=statisticsAdWatchDto.getPage();
        //条件
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        //SELECT 查询字段
        wrapper.setSqlSelect("SUM(1) AS 'watchCount',s_ad_code AS adCode,s_ad_name AS adName,l_ad_type_id AS adTypeId,s_ad_type_name AS adTypeName");
        //广告类别
        Long adTypeId=statisticsAdWatchDto.getAdTypeId();
        //省
        String regionProvince=statisticsAdWatchDto.getRegionProvince();
        //市
        String regionCity=statisticsAdWatchDto.getRegionCity();
        //区
        String regionArea=statisticsAdWatchDto.getRegionArea();
        //机构位置
        String orgPosition=statisticsAdWatchDto.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("watchCount");
        wrapper.orderDesc(orderByList);
        return this.selectPage(page, wrapper);
    }

    @Override
    public  List<StatisticsAdWatch> getAdWatchRankList(StatisticsAdWatchDto statisticsAdWatchDto){
        //条件
        Wrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        if(statisticsAdWatchDto!=null){
                //SELECT 查询字段
                wrapper.setSqlSelect("SUM(1) AS 'watchCount',s_ad_code AS adCode,s_ad_name AS adName,l_ad_type_id AS adTypeId");
                //广告类别
                Long adTypeId=statisticsAdWatchDto.getAdTypeId();
                //省
                String regionProvince=statisticsAdWatchDto.getRegionProvince();
                //市
                String regionCity=statisticsAdWatchDto.getRegionCity();
                //区
                String regionArea=statisticsAdWatchDto.getRegionArea();
                //机构位置
                String orgPosition=statisticsAdWatchDto.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("watchCount");
        wrapper.orderDesc(orderByList);
        List<StatisticsAdWatch> adWatchList = this.selectList(wrapper);
        return  adWatchList;
    }

    @Override
    public Map<String,Object> statisticsAdWatchCount(StatisticsAdWatchDto statisticsAdWatchDto){
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        wrapper.setSqlSelect("SUM(1) AS adWatchTotalCount,COUNT(DISTINCT l_user_id)AS adWatchTotalUserCount," +
                "SUM(CASE WHEN i_is_admin!=2 or i_is_admin is null THEN 1 ELSE 0 END) AS adWatchTotalVisitorCount");
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        return this.selectMap(wrapper);
    }

    @Override
    public long statisticsAdWatchVisitorCount(StatisticsAdWatchDto statisticsAdWatchDto){
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        wrapper.setSqlSelect("COUNT(DISTINCT l_user_id)AS adWatchVisitorCount");
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //访客
        wrapper.eq("i_user_type", OperationConstant.THREE);
        long count= (long) this.selectObj(wrapper);
        return count;
    }

    @Override
    public List<Map<String,Object>> statisticsAdWatchForChart(StatisticsAdWatchDto statisticsAdWatchDto){
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        String sqlSelect="SUM(1) AS adWatchCount,COUNT(CASE WHEN i_is_admin=2 THEN 1 ELSE NULL END) AS adWatchUserCount";
        int type=statisticsAdWatchDto.getDateType();
        //按某月的天数统计
        if(type==1){
            sqlSelect+=",DATE_FORMAT(t_watch_date,'%d') AS day";
        }else{
            sqlSelect+=",DATE_FORMAT(t_watch_date,'%m') AS month ";
        }
        wrapper.setSqlSelect(sqlSelect);
        //按区域查询
        //省
        String province=statisticsAdWatchDto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_region_province",province);
        }
        //市
        String city=statisticsAdWatchDto.getRegionCity();
        if(StringUtils.isNotBlank(city)){
            wrapper.eq("s_region_city",city);
        }
        //区
        String area=statisticsAdWatchDto.getRegionArea();
        if(StringUtils.isNotBlank(area)){
            wrapper.eq("s_region_area",area);
        }
        //按广告分类
        Long adTypeId=statisticsAdWatchDto.getAdTypeId();
        if(adTypeId!=null){
            wrapper.eq("l_ad_type_id",adTypeId);
        }
        //按时间查
        //时间为空，就不统计
        Date date = statisticsAdWatchDto.getWatchDate();
        if(date==null) {
            return null;
        }
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        if(type==1){
            //按月查询
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y%c')", year + ""+month);
            //按天分组
            wrapper.groupBy("day");
        }else{
            //按年查询
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y')", String.valueOf(year));
            //按月分组
            wrapper.groupBy("month");
        }
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        return this.selectMaps(wrapper);
    }

    @Override
    public List<Map<String,Object>> statisticsAdWatchCountChartByDateType(StatisticsAdWatchDto statisticsAdWatchDto){
        //按时间查
        //时间为空，就不统计
        Date date = statisticsAdWatchDto.getWatchDate();
        if(date==null) {
            return null;
        }
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        //年
        int year=calendar.get(Calendar.YEAR);
        //月
        int month=calendar.get(Calendar.MONTH)+1;

        List<Map<String,Object>> statisticsAdWatchMap=null;
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        String sqlSelect="SUM(1) AS adWatchCount";
        int type=statisticsAdWatchDto.getDateType();
        //按某月的日期统计
        if(type==OperationConstant.ZERO){
            sqlSelect+=",DATE_FORMAT(t_watch_date,'%c') AS month";
            //按年查询
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y')", String.valueOf(year));
            //按月分组
            wrapper.groupBy("month");
        }else if(type==OperationConstant.ONE){
            //每日统计
            sqlSelect+=",DATE_FORMAT(t_watch_date,'%e') AS day ";
            //按月查询
            wrapper.eq("DATE_FORMAT(t_watch_date,'%c')", String.valueOf(month));
            //按天分组
            wrapper.groupBy("day");
        }
        wrapper.setSqlSelect(sqlSelect);
        //按区域查询
        //省
        String province=statisticsAdWatchDto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_region_province",province);
        }
        //市
        String city=statisticsAdWatchDto.getRegionCity();
        if(StringUtils.isNotBlank(city)){
            wrapper.eq("s_region_city",city);
        }
        //区
        String area=statisticsAdWatchDto.getRegionArea();
        if(StringUtils.isNotBlank(area)){
            wrapper.eq("s_region_area",area);
        }
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        statisticsAdWatchMap=this.selectMaps(wrapper);
        return statisticsAdWatchMap;
    }

    @Override
    public List<Map<String,Object>> statisticsAdWatchRankForArea(StatisticsAdWatchDto statisticsAdWatchDto){
        List<Map<String,Object>> statisticsAdWatchMap;
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        String sqlSelect="COUNT(1) AS adWatchCount";
        int areaType=statisticsAdWatchDto.getAreaType();
        //按市查询
        if(areaType==1){
            sqlSelect+=",s_region_city AS regionCity";
            //按市份分组
            wrapper.groupBy("s_region_city");
            wrapper.isNotNull("s_region_city");
        }else{
            //按省查询
            sqlSelect+=",s_region_province AS regionProvince";
            //按省份分组
            wrapper.groupBy("s_region_province");
            wrapper.isNotNull("s_region_province");

        }
        wrapper.setSqlSelect(sqlSelect);
        //省份
        String province=statisticsAdWatchDto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_region_province",province);
        }
        //排序
        wrapper.orderBy("adWatchCount",false);
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //分页查询
        Page page=statisticsAdWatchDto.getPage();
        if(page!=null){
            Page<Map<String, Object>> mapPage = this.selectMapsPage(page, wrapper);
            statisticsAdWatchMap=mapPage.getRecords();
        }else{
            statisticsAdWatchMap=this.selectMaps(wrapper);
        }
        return statisticsAdWatchMap;
    }

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

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void addAdWatch(StatisticsAdWatch statisticsAdWatch){
        //添加观看统计记录
        //任务类型 { 1：人脸广告，2：闲时广告 }
        Integer taskType=statisticsAdWatch.getTaskType();
        boolean  result;
        if(null!=taskType&&taskType==OperationConstant.TWO){
            StatisticsAdPlayFreeTime statisticsAdPlayFreeTime=new StatisticsAdPlayFreeTime();
            BeanUtils.copyProperties(statisticsAdWatch,statisticsAdPlayFreeTime);
            result=adPlayFreeTimeServiceInterface.insert(statisticsAdPlayFreeTime);
        }else{
            result=this.insert(statisticsAdWatch);
        }
        if(result){
            StatisticsCount statisticsCount=new StatisticsCount();
            if(null!=taskType&&taskType==OperationConstant.TWO) {
                //添加播放次数
                statisticsCount.setAdPlayFreeTimeCount(OperationConstant.ONE);
            }else{
                //添加观看次数
                Integer isAdmin = statisticsAdWatch.getIsAdmin();
                statisticsCount.setAdWatchCount(OperationConstant.ONE);
                if(isAdmin!=null&&isAdmin==OperationConstant.TWO){
                    //统计住户观看次数
                    statisticsCount.setAdWatchUserCount(OperationConstant.ONE);
                }else if(isAdmin==null||isAdmin==OperationConstant.ONE||isAdmin==OperationConstant.THREE){
                    //统计陌生人观看次数(服务人员+访客+路人)
                    statisticsCount.setAdWatchVisitorCount(OperationConstant.ONE);
                }
            }
            statisticsCount.setRootOrgId(statisticsAdWatch.getRootOrgId());
            statisticsCount.setOrgPosition(statisticsAdWatch.getOrgPosition());
            statisticsCountServiceInterface.addCount(statisticsCount);
        }
    }

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

    @Override
    public List<Map<String,Object>> statisticsAdWatchRankForAdType(StatisticsAdWatchDto statisticsAdWatchDto){
        List<Map<String,Object>> statisticsAdWatchMap=null;
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        String province=statisticsAdWatchDto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("adw.s_region_province",province);
        }
        //排序
        wrapper.orderBy("adWatchCount",false);
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("adw.s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //按广告分类分组
        wrapper.groupBy("adw.l_ad_type_id");
        //分页查询
        Page page=statisticsAdWatchDto.getPage();
        statisticsAdWatchMap=this.baseMapper.getAdWatchRankForAdTypeList(page, wrapper);
        return statisticsAdWatchMap;
    }

    @Override
    public List<Map<String,Object>> statisticsAdWatchPersonForDay(StatisticsAdWatchDto statisticsAdWatchDto){
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<StatisticsAdWatch>();
        String sqlSelect="COUNT(DISTINCT CONCAT(i_is_admin,i_user_type,l_user_id))AS adWatchCount,DATE_FORMAT(t_watch_date,'%d') AS day";
        wrapper.setSqlSelect(sqlSelect);
        //按区域查询
        //省
        String province=statisticsAdWatchDto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_region_province",province);
        }
        //市
        String city=statisticsAdWatchDto.getRegionCity();
        if(StringUtils.isNotBlank(city)){
            wrapper.eq("s_region_city",city);
        }
        //区
        String area=statisticsAdWatchDto.getRegionArea();
        if(StringUtils.isNotBlank(area)){
            wrapper.eq("s_region_area",area);
        }
        //按广告分类
        Long adTypeId=statisticsAdWatchDto.getAdTypeId();
        if(adTypeId!=null){
            wrapper.eq("l_ad_type_id",adTypeId);
        }
        //按时间查
        //时间为空，就不统计
        Date date = statisticsAdWatchDto.getWatchDate();
        if(date==null) {
            return null;
        }
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH)+1;
        //按月查询
        wrapper.eq("DATE_FORMAT(t_watch_date,'%Y%c')", year + ""+month);
        //按天分组
        wrapper.groupBy("day");
        //机构位置
        String orgPosition=statisticsAdWatchDto.getOrgPosition();
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //住户跟服务人员
        wrapper.lt("i_is_admin",OperationConstant.THREE);
        return this.selectMaps(wrapper);
    }

    @Override
    public List<Map<String, Object>> getAdWatchCountList(StatisticsAdWatchDto statisticsAdWatchDto) {
        EntityWrapper<StatisticsAdWatch> wrapper=new EntityWrapper<>();
        String sqlSelect = null;
        //按日查询
        if(OperationConstant.ZERO == statisticsAdWatchDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_watch_date,'%H') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y-%m-%d')",statisticsAdWatchDto.getSelectCon());
        }
        //本周
        if(OperationConstant.ONE == statisticsAdWatchDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_watch_date,'%w') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.where("YEARWEEK(date_format(t_watch_date,'%Y-%m-%d'),1) = YEARWEEK(now(),1)");
        }
        //按月查询
        if(OperationConstant.TWO == statisticsAdWatchDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_watch_date,'%d') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y-%m')",statisticsAdWatchDto.getSelectCon());
        }
        //按年查询
        if(OperationConstant.THREE == statisticsAdWatchDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_watch_date,'%m') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.eq("DATE_FORMAT(t_watch_date,'%Y')",statisticsAdWatchDto.getSelectCon());
        }
        //权限
        wrapper.like("s_org_position", statisticsAdWatchDto.getOrgPosition(),SqlLike.RIGHT);
        //分组
        wrapper.groupBy("indexDate");
        return this.selectMaps(wrapper);
    }

}
