package com.sptg.statistics.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.permissions.api.entity.Area;
import com.sptg.permissions.api.feigns.AreaFeign;
import com.sptg.statistics.api.dto.StatisticsAdClickMobileDto;
import com.sptg.statistics.api.dto.StatisticsAdPlayFreeTimeDto;
import com.sptg.statistics.api.dto.StatisticsAdWatchDto;
import com.sptg.statistics.api.entity.StatisticsAdClickMobile;
import com.sptg.statistics.api.entity.StatisticsAdPlayFreeTime;
import com.sptg.statistics.api.entity.StatisticsAdWatch;
import com.sptg.statistics.api.feigns.StatisticsAdWatchFeign;
import com.sptg.statistics.biz.service.StatisticsAdClickMobileServiceInterface;
import com.sptg.statistics.biz.service.StatisticsAdPlayFreeTimeServiceInterface;
import com.sptg.statistics.biz.service.StatisticsAdWatchServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * title: StatisticsAdWatchController
 * package: com.sptg.statistics.biz.controller
 * description:统计广告观看--控制器
 *
 *                
 *                 
 */
@RestController
@RequiredArgsConstructor
public class StatisticsAdWatchController implements StatisticsAdWatchFeign {

    private final StatisticsAdWatchServiceInterface adWatchServiceInterface;
    private final StatisticsAdPlayFreeTimeServiceInterface adPlayFreeTimeServiceInterface;
    private final StatisticsAdClickMobileServiceInterface adClickMobileServiceInterface;
    private final AreaFeign areaFeign;

    @PostMapping("/statistics/statisticsAdWatch/showStatisticsAdWatchList")
    @Override
    public ResultBean<Page<StatisticsAdWatch>> showStatisticsAdWatchList(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        Page<StatisticsAdWatch> page= adWatchServiceInterface.showStatisticsAdWatchList(statisticsAdWatchDto);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @PostMapping("/statistics/statisticsAdWatch/addAdWatch")
    @Override
    public ResultBean<Boolean> addAdWatch(@RequestBody StatisticsAdWatch statisticsAdWatch) {
        adWatchServiceInterface.addAdWatch(statisticsAdWatch);
        return new ResultBean<Boolean>(ResultEnum.SUCCESS,true);
    }

    @PostMapping("/statistics/statisticsAdWatch/showStatisticsAdWatchRankList")
    @Override
    public ResultBean<Page<StatisticsAdWatch>> showStatisticsAdWatchRankList(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        Page<StatisticsAdWatch> page= adWatchServiceInterface.showStatisticsAdWatchRankList( statisticsAdWatchDto);
        return  new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchCount")
    @Override
    public ResultBean<Map<String,Object>> statisticsAdWatchCount(@RequestBody  StatisticsAdWatchDto statisticsAdWatchDto) {
        Map<String,Object> map= adWatchServiceInterface.statisticsAdWatchCount(statisticsAdWatchDto);
        return  new ResultBean<>(ResultEnum.SUCCESS,map);
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchVisitorCount")
    @Override
    public ResultBean<Long> statisticsAdWatchVisitorCount(@RequestBody  StatisticsAdWatchDto statisticsAdWatchDto) {
        long count= adWatchServiceInterface.statisticsAdWatchVisitorCount(statisticsAdWatchDto);
        return  new ResultBean<>(ResultEnum.SUCCESS,count);
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchForChart")
    @Override
    public ResultBean<List<Map<String,Object>>> statisticsAdWatchForChart(@RequestBody  StatisticsAdWatchDto statisticsAdWatchDto) {
        List<Map<String,Object>> list= adWatchServiceInterface.statisticsAdWatchForChart(statisticsAdWatchDto);
        return  new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,list);
    }

    @PostMapping("/statistics/statisticsAdWatch/getAdWatchList")
    @Override
    public List<StatisticsAdWatch> getAdWatchList(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<StatisticsAdWatch> resultList= adWatchServiceInterface.getAdWatchList( statisticsAdWatchDto);
        return  resultList;
    }

    @PostMapping("/statistics/statisticsAdWatch/getAdWatchRankList")
    @Override
    public List<StatisticsAdWatch> getAdWatchRankList(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<StatisticsAdWatch> resultList= adWatchServiceInterface.getAdWatchRankList( statisticsAdWatchDto);
        return  resultList;
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchRankForArea")
    @Override
    public  List<Map<String, Object>> statisticsAdWatchRankForArea(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<Map<String, Object>> maps = adWatchServiceInterface.statisticsAdWatchRankForArea(statisticsAdWatchDto);
        //转换省市编号换成省市中文
        if(maps!=null){
            maps.forEach(map->{
                String code="";
                //市转换
                if(statisticsAdWatchDto.getAreaType()==1){
                    code=(String)map.get("regionCity");
                }else{ //省转换
                    code=(String)map.get("regionProvince");
                }
                if(StringUtils.isNotBlank(code)){
                    Area area = areaFeign.selectByCode(code);
                    if(null!=area){
                        //缩写的
                        String areaName=area.getShortName();
                        map.put("area",areaName);
                        map.put("adWatchCount",map.get("adWatchCount"));
                    }
                }
            });
        }
        return  maps;
    }

    @PostMapping("/statistics/statisticsAdWatch/getAdWatchCount")
    @Override
    public Long getAdWatchCount(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        return  adWatchServiceInterface.getAdWatchCount( statisticsAdWatchDto);
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchCountChartByDateType")
    @Override
    public  List<Map<String, Object>> statisticsAdWatchCountChartByDateType(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<Map<String, Object>> maps = adWatchServiceInterface.statisticsAdWatchCountChartByDateType(statisticsAdWatchDto);
        return  maps;
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchRankForAdType")
    @Override
    public  List<Map<String, Object>> statisticsAdWatchRankForAdType(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<Map<String, Object>> maps = adWatchServiceInterface.statisticsAdWatchRankForAdType(statisticsAdWatchDto);
        return  maps;
    }

    @PostMapping("/statistics/statisticsAdWatch/statisticsAdWatchPersonForDay")
    @Override
    public  List<Map<String, Object>> statisticsAdWatchPersonForDay(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<Map<String, Object>> maps = adWatchServiceInterface.statisticsAdWatchPersonForDay(statisticsAdWatchDto);
        return  maps;
    }

    @PostMapping("/statistics/statisticsAdWatch/getAdWatchCountList")
    @Override
    public ResultBean<List<Map<String, Object>>> getAdWatchCountList(@RequestBody StatisticsAdWatchDto statisticsAdWatchDto) {
        List<Map<String,Object>> list= this.adWatchServiceInterface.getAdWatchCountList(statisticsAdWatchDto);
        return  new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,list);
    }

    @PostMapping("/statistics/statisticsAdPlayFreeTime/showRankList")
    @Override
    public ResultBean<Page<StatisticsAdPlayFreeTime>> showAdPlayFreeTimeRankList(@RequestBody PageBean<StatisticsAdPlayFreeTime, StatisticsAdPlayFreeTimeDto> pageBean) {
        Page<StatisticsAdPlayFreeTime> page=adPlayFreeTimeServiceInterface.showAdRankList(pageBean);
        return  new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @PostMapping("/statistics/statisticsAdClickMobile/showRankList")
    @Override
    public ResultBean<Page<StatisticsAdClickMobile>> showAdClickMobileRankList(@RequestBody PageBean<StatisticsAdClickMobile, StatisticsAdClickMobileDto> pageBean) {
        Page<StatisticsAdClickMobile> page=adClickMobileServiceInterface.showAdRankList(pageBean);
        return  new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @PostMapping("/statistics/statisticsAdClickMobile/addAdClickMobile")
    @Override
    public ResultBean<Boolean> addAdClickMobile(StatisticsAdClickMobile statisticsAdClickMobile) {
        adClickMobileServiceInterface.addAdClickMobile(statisticsAdClickMobile);
        return  new ResultBean<>(ResultEnum.SUCCESS,true);
    }

    @PostMapping("/statistics/statisticsAdPlayFreeTime/getAdPlayCount")
    @Override
    public Long getAdPlayCount(@RequestBody StatisticsAdPlayFreeTimeDto dto) {
        return  adPlayFreeTimeServiceInterface.getAdPlayCount( dto);
    }
}
