package com.fs.ad.controller;

import com.fs.baidu.api.BaiduApis;
import com.fs.baidu.domain.BdAccount;
import com.fs.baidu.domain.BdApi;
import com.fs.baidu.enums.BdTimeUnit;
import com.fs.baidu.mapper.BdApiMapper;
import com.fs.baidu.param.QueryReportDataParam;
import com.fs.baidu.service.IBdAccountService;
import com.fs.bdAdv.param.FsAdvSemStatisticsByDayParam;
import com.fs.bdAdv.service.IFsAdvSemService;
import com.fs.bdAdv.vo.FsAdvSemStatisticsByDayVo;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.qw.param.ConversionStatisticsParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/baiduStatistics")
public class StatisticsController extends BaseController {

    private final IFsAdvSemService fsAdvSemService;
    private final IBdAccountService bdAccountService;
    private final BaiduApis baiduApis;
    private final BdApiMapper bdApiMapper;

    @GetMapping("/conversionStatistics")
    public R conversionStatistics(ConversionStatisticsParam param){
        return bdAccountService.conversionStatistics(param);
    }


    @GetMapping("/selectFsAdvSemStatisticsByDayVo")
    public TableDataInfo selectFsAdvSemStatisticsByDayVo(FsAdvSemStatisticsByDayParam param){
        startPage();
        return getDataTable(fsAdvSemService.selectFsAdvSemStatisticsByDayVo(param));
    }

    /**
     * 导出
     * @param param
     * @return
     */
    @GetMapping("/fsAdvSemStatisticsExport")
    public AjaxResult fsAdvSemStatisticsExport(FsAdvSemStatisticsByDayParam param){
        List<FsAdvSemStatisticsByDayVo> list = fsAdvSemService.selectFsAdvSemStatisticsByDayVo(param);
        ExcelUtil<FsAdvSemStatisticsByDayVo> util = new ExcelUtil<FsAdvSemStatisticsByDayVo>(FsAdvSemStatisticsByDayVo.class);
        return util.exportExcel(list, "企微主体数据");
    }



    @GetMapping("/getReportData")
    public R getReportData(Long id, Integer reportType, LocalDate startDate, LocalDate endDate, Integer page, Integer rowCount){
        QueryReportDataParam param = new QueryReportDataParam();
        param.setReportType(reportType);
        param.setStartDate(startDate);
        param.setEndDate(endDate);
        param.setPage(page);
        param.setRowCount(rowCount);
        param.setTimeUnit(BdTimeUnit.SUMMARY);
        BdAccount account = bdAccountService.getById(id);
        BdApi byId = bdApiMapper.selectById(account.getApiId());
        return baiduApis.getReportData(byId.getAccessToken(), account.getAccountName(), param);
//        return R.ok();
    }

    @GetMapping("/bdDataStatic")
    public R bdDataStatic(){
        return baiduApis.bdDataStatic();
    }


}
