package com.yunzhi.marketing.xlk.controller;

import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.center.taUser.entity.TaUser;
import com.yunzhi.marketing.center.taUser.service.ITaUserService;
import com.yunzhi.marketing.xlk.dto.ChannelCustomerDTO;
import com.yunzhi.marketing.xlk.dto.PcStatisticsDTO;
import com.yunzhi.marketing.xlk.service.IPcStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/api")
@Api(value = "xlk-pc端首页统计接口", tags = "xlk-pc端首页统计接口")
public class PcIndexStatisticsController extends BaseController {

    @Autowired
    private IPcStatisticsService pcStatisticsService;
    @Autowired
    public ITaUserService iTaUserService;

    /**
     * 首页六个用户数统计
     * @return
     */
    @ApiOperation(value = "admin-首页六个用户数统计", notes = "admin-首页六个用户数统计")
    @GetMapping(value="/admin/statistics/user")
    public ResponseBean getUserStatistics(@RequestHeader("authorization") String token, HttpServletRequest request){
        Integer userId = getUserId(request);
        TaUser taUser = iTaUserService.getById(userId);
        return pcStatisticsService.getUserStatistics(taUser);
    }

    /**
     * 首页用户来源
     * @return
     */
    @ApiOperation(value = "admin-首页用户来源统计", notes = "admin-首页用户来源统计")
    @GetMapping(value="/admin/statistics/userOrigin")
    public ResponseBean getUserOriginStatistics(@RequestHeader("authorization") String token, HttpServletRequest request){
        Integer userId = getUserId(request);
        TaUser taUser = iTaUserService.getById(userId);
        return pcStatisticsService.getUserOriginStatistics(taUser);
    }

    /**
     * 首页性别比例
     * @return
     */
    @ApiOperation(value = "admin-首页性别比例统计", notes = "admin-首页性别比例统计")
    @GetMapping(value="/admin/statistics/userGender")
    public ResponseBean getuserGenderStatistics(@RequestHeader("authorization") String token, HttpServletRequest request){
        Integer userId = getUserId(request);
        TaUser taUser = iTaUserService.getById(userId);
        return pcStatisticsService.getuserGenderStatistics(taUser);
    }

    /**
     * 首页手机授权转换率
     * @return
     */
    @ApiOperation(value = "admin-首页手机授权转换率统计", notes = "admin-首页手机授权转换率统计")
    @GetMapping(value="/admin/statistics/userAuthor")
    public ResponseBean getUserAuthorStatistics(@RequestHeader("authorization") String token, HttpServletRequest request){
        Integer userId = getUserId(request);
        TaUser taUser = iTaUserService.getById(userId);
        return pcStatisticsService.getUserAuthorStatistics(taUser);
    }

    /**
     * 项目排行统计图
     * @return
     */
    @ApiOperation(value = "admin-项目排行统计图", notes = "admin-项目排行统计图")
    @GetMapping(value = "/admin/statistics/barList")
    public ResponseBean selectBuildingBar(@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                          @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  LocalDateTime endDate,
                                          @RequestParam(value = "cityId", required = false) String cityId,
                                          HttpServletRequest request) {
        Integer userId = getUserId(request);
        TaUser taUser = iTaUserService.getById(userId);
        PcStatisticsDTO pcStatisticsDTO = new PcStatisticsDTO();
        pcStatisticsDTO.setOrgId(taUser.getOrgId());
        pcStatisticsDTO.setStartTime(startDate);
        pcStatisticsDTO.setEndTime(endDate);
        pcStatisticsDTO.setCityId(cityId);
        return pcStatisticsService.getStatsBarList(taUser, pcStatisticsDTO);
    }

    /**
     * 活动统计
     * @return
     */
    @ApiOperation(value = "admin-活动统计", notes = "admin-活动统计")
    @GetMapping(value = "/admin/statistics/activityList")
    public ResponseBean selectActivityList(@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                          @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  LocalDateTime endDate,
                                          @RequestParam(value = "buildingId", required = false) String buildingId,
                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                                          HttpServletRequest request) {
        Integer userId = getUserId(request);
        TaUser taUser = iTaUserService.getById(userId);
        PcStatisticsDTO pcStatisticsDTO = new PcStatisticsDTO();
        pcStatisticsDTO.setOrgId(taUser.getOrgId());
        pcStatisticsDTO.setStartTime(startDate);
        pcStatisticsDTO.setEndTime(endDate);
        pcStatisticsDTO.setBuildingId(buildingId);
        pcStatisticsDTO.setPageNum(pageNum);
        pcStatisticsDTO.setPageSize(pageSize);
        return pcStatisticsService.selectActivityList(taUser, pcStatisticsDTO);
    }
}
