package com.sptg.gateway.controller.statistics;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.entity.AdType;
import com.sptg.ad.api.feigns.AdTypeFeign;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.statistics.api.constant.StatisticsSecurityToken;
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.entity.StatisticsCount;
import com.sptg.statistics.api.feigns.StatisticsAdWatchFeign;
import com.sptg.statistics.api.feigns.StatisticsCountFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: StatisticsAdWatchController
 * package: com.sptg.gateway.controller.statistics
 * description: 统计广告观看-网关控制器
 *
 *                
 *                
 */

@RestController
@RequestMapping(value = "/statistics/statisticsAdWatch")
@RequiredArgsConstructor
@Slf4j
public class StatisticsAdWatchController {
    private final StatisticsAdWatchFeign statisticsAdWatchFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final UserFeign userFeign;
    private final StatisticsCountFeign statisticsCountFeign;
    private final AdTypeFeign adTypeFeign;

    /**
     * 功能描述:广告观看统计页面
     * @return ModelAndView
     *                
     *                       
     */
    @RequestMapping("/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-watch");
        return modelAndView;
    }

    /**
     * 功能描述:广告观看详细页面
     * @return ModelAndView
     *                
     *                       
     */
    @RequestMapping("/detail/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_MANAGE)
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-watch-detail");
        return modelAndView;
    }

     /**
      * 功能描述:统计广告列表
      * @param page
      * @param dto
      * @return ResultBean<Page<StatisticsAdWatch>>
      *                
      *                       
      */
    @RequestMapping(value="/showStatisticsAdWatchList")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<StatisticsAdWatch>> showStatisticsAdWatchList(Page<StatisticsAdWatch> page,  StatisticsAdWatchDto dto){
        dto.setPage(page);
        Account account=this.securityServiceInterface.getOnLineUser();
        //机构位置
        String orgPosition=account.getOrgPosition();
        dto.setOrgPosition(orgPosition);
        ResultBean<Page<StatisticsAdWatch>> resultBean=statisticsAdWatchFeign.showStatisticsAdWatchList(dto);
        Page page2=resultBean.getData();
        List<StatisticsAdWatch> statisticsAdWatchList=page2.getRecords();
        statisticsAdWatchList.forEach((StatisticsAdWatch adWatch)->{
            //获取用户名称并赋值
            Serializable userId=adWatch.getUserId();
            if(userId!=null){
                ResultBean<String> userNameResultBean=userFeign.getUserNameById(userId);
                String userName=userNameResultBean.getData();
                adWatch.setUserName(userName);
            }
            //获取广告分类名称并赋值
            Long adTypeId=adWatch.getAdTypeId();
            if(adTypeId!=null){
                AdType adType=adTypeFeign.getAdTypeById(adTypeId);
                String name=adType!=null?adType.getName():"";
                if(StringUtils.isNotBlank(name)){
                    adWatch.setAdTypeName(name);
                }
            }
        });
        return  resultBean;
    }

    /**
     * 功能描述:广告观看统计排名页面
     * @return ModelAndView
     *                
     *                       
     */
    @RequestMapping("/rank/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_RANK_MANAGE)
    public ModelAndView rank(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-watch-rank");
        return modelAndView;
    }

    /**
     * 功能描述:展示统计广告观看排名
     * @param page
     * @param statisticsAdWatchDto
     * @return ResultBean<Page<StatisticsAdWatch>>
     *                
     *                       
     */
    @RequestMapping(value="/showStatisticsAdWatchRankList")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_RANK_MANAGE)
    public ResultBean<Page<StatisticsAdWatch>> showStatisticsAdWatchRankList(Page<StatisticsAdWatch> page,  StatisticsAdWatchDto statisticsAdWatchDto){
        statisticsAdWatchDto.setPage(page);
        Account account=this.securityServiceInterface.getOnLineUser();
        //机构位置
        String orgPosition=account.getOrgPosition();
        statisticsAdWatchDto.setOrgPosition(orgPosition);
        ResultBean<Page<StatisticsAdWatch>> resultBean=statisticsAdWatchFeign.showStatisticsAdWatchRankList(statisticsAdWatchDto);
        Page page2=resultBean.getData();
        List<StatisticsAdWatch> statisticsAdWatchList=page2.getRecords();
        statisticsAdWatchList.forEach(adWatch->{
            //获取广告分类名称并赋值
            Long adTypeId=adWatch.getAdTypeId();
            if(adTypeId!=null){
                AdType adType=adTypeFeign.getAdTypeById(adTypeId);
                String name=adType!=null?adType.getName():"";
                if(!StringUtils.isBlank(name)){
                    adWatch.setAdTypeName(name);
                }
            }
        });
        return  resultBean;
    }

    /**
     * 功能描述: 统计 总观看次数（adWatchTotalCount） 总住户观看次数（adWatchTotalUserCount） 总访客观看次数（adWatchTotalVisitorCount）
     * @param
     * @return ResultBean<Map<String,Object>>
     *                
     *                       
     */
    @RequestMapping(value="/statisticsAdWatchTotal")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_MANAGE)
    public ResultBean<Map<String,Object>> statisticsAdWatchTotal(){
        Map<String,Object> resultMap=new HashMap<String,Object>(OperationConstant.THREE);
        StatisticsAdWatchDto statisticsAdWatchDto=new StatisticsAdWatchDto();
        Account account=this.securityServiceInterface.getOnLineUser();
        //机构位置
        String orgPosition=account.getOrgPosition();
        statisticsAdWatchDto.setOrgPosition(orgPosition);
        //统计数量
        ResultBean<StatisticsCount> statisticsCountResultBean = statisticsCountFeign.selectCountOne(orgPosition);
        StatisticsCount statisticsCount=statisticsCountResultBean.getData();
        if(statisticsCount!=null){
            //总观看次数
            resultMap.put("adWatchTotalCount",statisticsCount.getAdWatchCount());
            //访客观看次数
            resultMap.put("adWatchTotalVisitorCount",statisticsCount.getAdWatchVisitorCount());
            //住户观看次数
            resultMap.put("adWatchTotalUserCount",statisticsCount.getAdWatchUserCount());
        }
        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:统计 某年12个月(或某月约30天)观看次数 住户观看人数 住户观看次数 (统计图)
     * @param statisticsAdWatchDto
     * @return ResultBean<List<Map<String,Object>>>
     *                
     *                       
     */
    @RequestMapping(value="/statisticsAdWatchForChart")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_MANAGE)
    public ResultBean<List<Map<String,Object>>> statisticsAdWatchForChart(StatisticsAdWatchDto statisticsAdWatchDto){
        Account account=this.securityServiceInterface.getOnLineUser();
        //机构位置
        String orgPosition=account.getOrgPosition();
        statisticsAdWatchDto.setOrgPosition(orgPosition);
        return  statisticsAdWatchFeign.statisticsAdWatchForChart(statisticsAdWatchDto);
    }
    
    /**
     * 功能描述:广告观看导出
     * @param response
     *             
     *                      
     */
    @RequestMapping(value = "/exportAdWatch")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_EXPORT)
    public void exportAdWatch(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/5");
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 功能描述:广告观看排行导出
     * @param response
     *             
     *                      
     */
    @RequestMapping(value = "/exportAdWatchRank")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_WATCH_RANK_EXPORT)
    public void exportAdWatchRank(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/6");
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 功能描述:闲时广告播放统计排名页面
     * @return ModelAndView
     *                
     *                     
     */
    @RequestMapping("/playFreeTimeRank/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_STATISTICS_AD_PLAY_FREE_TIME_RANK_MANAG)
    public ModelAndView playFreeTimeRank(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-play-free-time-rank");
        return modelAndView;
    }

    /**
     * 功能描述:展示闲时广告播放排名
     * @param page 分页条件
     * @param dto 查询条件
     * @return ResultBean<Page<StatisticsAdPlayFreeTime>>
     *                
     *                     
     */
    @RequestMapping(value="/showAdPlayFreeTimeRankRankList")
    @Security(token = StatisticsSecurityToken.STATISTICS_STATISTICS_AD_PLAY_FREE_TIME_RANK_MANAG)
    public ResultBean<Page<StatisticsAdPlayFreeTime>> showAdPlayFreeTimeRankRankList(PageBean<StatisticsAdPlayFreeTime,StatisticsAdPlayFreeTimeDto> page, StatisticsAdPlayFreeTimeDto dto){
        Account account=this.securityServiceInterface.getOnLineUser();
        //机构位置
        String orgPosition=account.getOrgPosition();
        dto.setOrgPosition(orgPosition);
        page.setDto(dto);
        ResultBean<Page<StatisticsAdPlayFreeTime>> resultBean=statisticsAdWatchFeign.showAdPlayFreeTimeRankList(page);
        Page page2=resultBean.getData();
        List<StatisticsAdPlayFreeTime> list=page2.getRecords();
        list.forEach(adWatch->{
            //获取广告分类名称并赋值
            Long adTypeId=adWatch.getAdTypeId();
            if(adTypeId!=null){
                AdType adType=adTypeFeign.getAdTypeById(adTypeId);
                String name=adType!=null?adType.getName():"";
                if(!StringUtils.isBlank(name)){
                    adWatch.setAdTypeName(name);
                }
            }
        });
        return  resultBean;
    }

    /**
     * 功能描述:移动端广告点击统计排名页面
     * @return ModelAndView
     *                
     *                     
     */
    @RequestMapping("/clickMobileRank/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_STATISTICS_AD_CLICK_MOBILE_RANK_MANAGE)
    public ModelAndView clickMobileRank(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-click-mobile-rank");
        return modelAndView;
    }

    /**
     * 功能描述:展示移动端广告点击排名
     * @param page 分页条件
     * @param dto 查询条件
     * @return ResultBean<Page<StatisticsAdClickMobile>>
     *                
     *                     
     */
    @RequestMapping(value="/showStatisticsAdClickMobileRankList")
    @Security(token = StatisticsSecurityToken.STATISTICS_STATISTICS_AD_CLICK_MOBILE_RANK_MANAGE)
    public ResultBean<Page<StatisticsAdClickMobile>> showStatisticsAdClickMobileRankList(PageBean<StatisticsAdClickMobile,StatisticsAdClickMobileDto> page, StatisticsAdClickMobileDto dto){
        Account account=this.securityServiceInterface.getOnLineUser();
        //机构位置
        String orgPosition=account.getOrgPosition();
        dto.setOrgPosition(orgPosition);
        page.setDto(dto);
        ResultBean<Page<StatisticsAdClickMobile>> resultBean=statisticsAdWatchFeign.showAdClickMobileRankList(page);
        Page page2=resultBean.getData();
        List<StatisticsAdClickMobile> list=page2.getRecords();
        list.forEach(adWatch->{
            //获取广告分类名称并赋值
            Long adTypeId=adWatch.getAdTypeId();
            if(adTypeId!=null){
                AdType adType=adTypeFeign.getAdTypeById(adTypeId);
                String name=adType!=null?adType.getName():"";
                if(!StringUtils.isBlank(name)){
                    adWatch.setAdTypeName(name);
                }
            }
        });
        return  resultBean;
    }
}
