package com.sptg.gateway.controller.statistics;

import com.sptg.ad.api.dto.AdAdsenseDto;
import com.sptg.ad.api.dto.AdContentDto;
import com.sptg.ad.api.entity.AdAdsense;
import com.sptg.ad.api.entity.AdContent;
import com.sptg.ad.api.feigns.AdAdsenseFeign;
import com.sptg.ad.api.feigns.AdContentFeign;
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.permissions.api.entity.Account;
import com.sptg.statistics.api.constant.StatisticsSecurityToken;
import com.sptg.statistics.api.dto.StatisticsAdPushDto;
import com.sptg.statistics.api.entity.StatisticsCount;
import com.sptg.statistics.api.feigns.StatisticsAdPushFeign;
import com.sptg.statistics.api.feigns.StatisticsCountFeign;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * title: StatisticsAdPushController
 * package: com.sptg.gateway.controller.statistics
 * description: 统计广告发布-网关控制器
 *
 *                
 *                 
 */

@RestController
@RequestMapping(value = "/statistics/statisticsAdPush")
@AllArgsConstructor
public class StatisticsAdPushController {
    private final StatisticsAdPushFeign statisticsAdPushFeign;
    private final StatisticsCountFeign statisticsCountFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;

    private final AdContentFeign adContentFeign;
    private final AdAdsenseFeign adAdsenseFeign;
    /**
     * 功能描述:人脸广告发布统计
     * @return ModelAndView
     *                
     *                     
     */
    @RequestMapping("/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_PUSH_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-push");
        //1人脸广告
        modelAndView.addObject("pushTaskType",OperationConstant.ONE);
        return modelAndView;
    }

    /**
     * 功能描述:统计 广告曝光量 累计发布广告条数 当月广告曝光量 当月已发布广告条数
     * @return ResultBean<Map<String,Object>>
     *                
     *                       
     */
    @RequestMapping(value="/statisticsAdPushTotal")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_PUSH_MANAGE,format = DataFormatEnum.JSON)
    public  ResultBean<Map<String,Object>> statisticsAdPushTotal(Integer taskType){
        Map<String,Object> resultMap=new HashMap<String,Object>(OperationConstant.FOUR);
        StatisticsAdPushDto  statisticsAdPushDto=new StatisticsAdPushDto();
        //获取机构位置
        Account account= this.securityServiceInterface.getOnLineUser();
        statisticsAdPushDto.setOrgPosition(account.getOrgPosition());
        statisticsAdPushDto.setTaskType(taskType==null?OperationConstant.ONE:taskType);
        //查询统计数量
        ResultBean<StatisticsCount> statisticsCountResultBean = statisticsCountFeign.selectCountOne(account.getOrgPosition());
        StatisticsCount statisticsCount=statisticsCountResultBean.getData();
        if(statisticsCount!=null){
            long adExposureCount;
            long adPushCount;
            //闲时广告发布类型
            if(taskType==OperationConstant.TWO){
                adExposureCount = statisticsCount.getAdExposureFreeTimeCount();
                adPushCount = statisticsCount.getAdPushFreeTimeCount();
            }else{
                //人脸广告发布类型
                adExposureCount = statisticsCount.getAdExposureCount();
                adPushCount = statisticsCount.getAdPushCount();
            }
            resultMap.put("adExposureCount",adExposureCount);
            resultMap.put("adPushCount",adPushCount);
        }
        //查询当月广告数量跟广告曝光量
        ResultBean<Map<String,Object>> result=statisticsAdPushFeign.statisticsCurrentMonthAdPushTotal(statisticsAdPushDto);
        Map<String,Object> currentMonthAdPushMap=result.getData();
        if(currentMonthAdPushMap!=null){
            resultMap.putAll(currentMonthAdPushMap);
        }
        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:统计 线下广告数量 线上广告数量 视频广告数量 图片广告数量 广告位数量
     *
     * @return ResultBean<Map < String, Object>>
     */
    @RequestMapping(value = "/statisticsAdPushTotalNew")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_PUSH_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Map<String, Object>> statisticsAdPushTotalNew() {
        Map<String, Object> resultMap = new HashMap<String, Object>(OperationConstant.FIVE);
        StatisticsAdPushDto statisticsAdPushDto = new StatisticsAdPushDto();
        Account account = this.securityServiceInterface.getOnLineUser();
        statisticsAdPushDto.setOrgPosition(account.getOrgPosition());
        AdContentDto adContentDto = new AdContentDto();
        adContentDto.setOrgPosition(account.getOrgPosition());
        adContentDto.setOnlineOrOffline(0);
        ResultBean<AdContent> list = adContentFeign.getList(adContentDto);
        if(list!=null){
            List<AdContent> adContentList = list.getList();
            if(adContentList != null){
                resultMap.put("offlineAdCount", adContentList.size());
            }else{
                resultMap.put("offlineAdCount", 0);
            }
        }
        adContentDto.setOnlineOrOffline(1);
        ResultBean<AdContent> list1 = adContentFeign.getList(adContentDto);
        if(list1!=null){
            List<AdContent> adContentList = list1.getList();
            if(adContentList != null) {
                resultMap.put("onlineAdCount", adContentList.size());
            }else {
                resultMap.put("onlineAdCount", 0);
            }
        }
        adContentDto.setOnlineOrOffline(null);
        adContentDto.setMaterialType("视频");
        ResultBean<AdContent> list2 = adContentFeign.getList(adContentDto);
        if(list2!=null){
            List<AdContent> adContentList = list2.getList();
            if(adContentList != null) {
                resultMap.put("videoAdCount", adContentList.size());
            }else{
                resultMap.put("videoAdCount",0);
            }
        }
        adContentDto.setMaterialType("图片");
        ResultBean<AdContent> list3 = adContentFeign.getList(adContentDto);
        if(list3!=null){
            List<AdContent> adContentList = list3.getList();
            if(adContentList != null) {
                resultMap.put("pictureAdCount", adContentList.size());
            }else{
                resultMap.put("pictureAdCount",0);
            }
        }
        ResultBean<Integer> allAdsenseCount = adAdsenseFeign.getAllAdsenseCount();
        if(null != allAdsenseCount){
            resultMap.put("adsenseCount", allAdsenseCount.getData());
        }
        return new ResultBean<>(ResultEnum.SUCCESS, resultMap);
    }
/**
     * 功能描述:广告发布统计图数据
     *
     * @param statisticsAdPushDto
     * @return ResultBean<List < Map < String, Object>>>
     */

    @RequestMapping(value="/statisticsAdPushAndExposure")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_PUSH_MANAGE,format = DataFormatEnum.JSON)
    public  ResultBean<List<Map<String,Object>>> statisticsAdPushAndExposure(StatisticsAdPushDto  statisticsAdPushDto){
        Account account= this.securityServiceInterface.getOnLineUser();
        statisticsAdPushDto.setOrgPosition(account.getOrgPosition());
        //任务广告类型
        Integer taskType=statisticsAdPushDto.getTaskType();
        if(null==taskType){
            //默认值
            statisticsAdPushDto.setTaskType(OperationConstant.ONE);
        }
        ResultBean<List<Map<String,Object>>> resultListBean=statisticsAdPushFeign.statisticsAdPushAndExposure(statisticsAdPushDto);
        return  resultListBean;
    }

    /**
     * 功能描述:线上广告按年月统计
     *
     * @param statisticsAdPushDto
     * @return ResultBean<List < Map < String, Object>>>
     */
    @RequestMapping(value = "/statisticsAdOnlineByYearMonth")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_PUSH_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> statisticsAdOnlineByYearMonth(StatisticsAdPushDto statisticsAdPushDto) {
        Account account = this.securityServiceInterface.getOnLineUser();
        statisticsAdPushDto.setOrgPosition(account.getOrgPosition());
        Integer taskType = statisticsAdPushDto.getTaskType();
        if (null == taskType) {
            statisticsAdPushDto.setTaskType(OperationConstant.ONE);
        }
        ResultBean<List<Map<String, Object>>> resultListBean = statisticsAdPushFeign.statisticsAdPushAndExposure(statisticsAdPushDto);
        return resultListBean;
    }
/**
     * 功能描述:闲时广告发布统计
     *
     * @return ModelAndView
     */

    @RequestMapping("/freeTime/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_STATISTICS_AD_PUSH_FREE_TIME_MANAGE)
    public ModelAndView freeTimeIndex(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-ad-push-online-offline");
        //2闲时广告
        modelAndView.addObject("pushTaskType",OperationConstant.TWO);
        return modelAndView;
    }
}
