package com.fs.company.controller;

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.statis.StatisticsRedisConstant;
import com.fs.statis.dto.*;
import io.jsonwebtoken.lang.Assert;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.fs.statis.StatisticsRedisConstant.*;

/**
 * 首页-统计
 */
@RestController
@RequestMapping("/index/statistics")
public class IndexStatisticsController {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TokenService tokenService;
    /**
     * 分析概览
     */
    @PostMapping("/analysisPreview")
    public R analysisPreview(@RequestBody AnalysisPreviewQueryDTO param){
        AnalysisPreviewDTO analysisPreviewDTO = null;
        Integer type = param.getType();
        Integer userType = param.getUserType();

        if(type == null) {
            type = 0;
        }

        if(userType == null) {
            userType = 0;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        analysisPreviewDTO = redisCache.getCacheObject(String.format("%s:%d:%d:%d",DATA_OVERVIEW_DEALER_ANALYSISPREVIEW,type,userType,param.getCompanyId()));

        return R.ok().put("data",analysisPreviewDTO);
    }


    /**
     * 消费余额
     */
    @GetMapping("/rechargeComsumption")
    public R rechargeComsumption(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();

        ConsumptionBalanceDataDTO consumptionBalanceDataDTO = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_BALANCE,companyId));
        return R.ok().put("data", consumptionBalanceDataDTO);
    }

    /**
     * 获取统计流量
     * @return
     */
    @GetMapping("/trafficLog")
    public R getTrafficLog(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        TrafficLogDTO trafficLogDTO = redisCache.getCacheObject(String.format("%s:%d",DATA_OVERVIEW_TRAFFIC_LOG,companyId));
        return R.ok().put("data",trafficLogDTO);
    }

    /**
     * 观看趋势
     */
    @PostMapping("/watchEndPlayTrend")
    public R watchEndPlayTrend(@RequestBody AnalysisPreviewQueryDTO param){
        Integer type = param.getType();
        Integer userType = param.getUserType();

        if(type == null) {
            type = 0;
        }
        if(userType == null){
            userType = 0;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);

        String key = String.format("%s:%d:%d:%d", DATA_OVERVIEW_DEALER_CHARTS, type,userType,param.getCompanyId());
        List<DeaMemberTopTenDTO> deaMemberTopTenDTOS = redisCache.getCacheObject(key);
        return R.ok().put("data", deaMemberTopTenDTOS);
    }

    /**
     * 经销商会员观看
     */
    @PostMapping("/deaMemberTopTen")
    public R deaMemberTopTen(@RequestBody AnalysisPreviewQueryDTO param){
        Integer type = param.getType();
        Integer statisticalType = param.getStatisticalType();
        Integer userType = param.getUserType();

        if(type == null) {
            type = 0;
        }
        if(userType == null){
            userType = 0;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);

        List<DeaMemberTopTenDTO> deaMemberTopTenDTOS = redisCache.getCacheObject(String.format("%s:%d:%d:%d:%d", CHARTS_MEMBER_TOP_TEN_WATCH, type, statisticalType,userType,param.getCompanyId()));
        if(deaMemberTopTenDTOS == null){
            deaMemberTopTenDTOS = new ArrayList<>();
        }
        return R.ok().put("data", deaMemberTopTenDTOS);
    }

    /**
     * 奖励金额top10
     */
    @PostMapping("/rewardMoneyTopTen")
    public R rewardMoneyTopTen(@RequestBody AnalysisPreviewQueryDTO param){
        Integer type = param.getType();
        Integer dataType = param.getDataType();
        Integer userType = param.getUserType();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);

        List<RewardMoneyTopTenDTO> rewardMoneyTopTenDTOS = redisCache.getCacheObject( String.format("%s:%d:%d:%d:%d", CHARTS_REWARD_MONEY_TOP_TEN, type,dataType,userType,param.getCompanyId()));
        return R.ok().put("data", rewardMoneyTopTenDTOS);
    }

    /**
     * 答题红包金额趋势图
     */
    @PostMapping("/rewardMoneyTrend")
    public R rewardMoneyTrend(@RequestBody AnalysisPreviewQueryDTO param){
        Integer type = param.getType();
        Integer userType = param.getUserType();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);

        List<RewardMoneyTrendDTO> rewardMoneyTrendDTOS = redisCache.getCacheObject( String.format("%s:%d:%d:%d", CHARTS_REWARD_MONEY_TREND, type,userType,param.getCompanyId()));
        return R.ok().put("data", rewardMoneyTrendDTOS);
    }

    /**
     * 课程观看top10
     */
    @PostMapping("/watchCourseTopTen")
    public R watchCourseTopTen(@RequestBody AnalysisPreviewQueryDTO param){
        Integer type = param.getType();
        String sort = param.getSort();
        Integer statisticalType = param.getStatisticalType();
        Integer userType = param.getUserType();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        param.setCompanyId(companyId);

        List<CourseStatsDTO> courseStatsDTOS = redisCache.getCacheObject(String.format("%s:%d:%d:%d:%s:%d", CHARTS_WATCH_TOP_TEN, type,statisticalType,userType,sort,param.getCompanyId()));
        return R.ok().put("data", courseStatsDTOS);
    }

    /**
     * 数据概览
     */
    @GetMapping("/dealerAggregated")
    public R dealerAggregated(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();

        DealerAggregatedDTO dealerAggregatedDTO = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AGGREGATED,companyId));

        return R.ok().put("data",dealerAggregatedDTO);
    }

    /**
     * 短信余额
     */
    @GetMapping("/smsBalance")
    public R smsBalance(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();

        Long smsBalance = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_SMS_BALANCE,companyId));

        return R.ok().put("data", smsBalance);
    }


    /**
     * 授权信息
     */
    @GetMapping("/authorizationInfo")
    public R authorizationInfo(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();

        AuthorizationInfoDTO authorizationInfoDTO = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.DATA_OVERVIEW_DEALER_AUTHORIZATION_INFO,companyId));

        return R.ok().put("data", authorizationInfoDTO);
    }


    /**
     * 当月订单数统计
     * @return
     */
    @GetMapping("/thisMonthOrderCount")
    public R thisMonthOrderCount(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();

        R result = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.THIS_MONTH_ORDER_COUNT,companyId));
        return result;
    }

    /**
     * 当月收益统计
     * @return
     */
    @GetMapping("/thisMonthRecvCount")
    public R thisMonthRecvCount(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();

        R result = redisCache.getCacheObject(String.format("%s:%d",StatisticsRedisConstant.THIS_MONTH_RECV_COUNT,companyId));
        return result;
    }
}
