package com.fs.ad.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.ad.domain.BdReportData;
import com.fs.ad.mapper.BdReportDataMapper;
import com.fs.ad.service.IBdReportDataService;
import com.fs.baidu.api.BaiduApis;
import com.fs.baidu.domain.BdAccount;
import com.fs.baidu.enums.BdTimeUnit;
import com.fs.baidu.param.QueryReportDataParam;
import com.fs.baidu.service.IBdAccountService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 百度定向统计Service业务层处理
 * 
 * @author fs
 * @date 2025-03-13
 */
@Service
@AllArgsConstructor
public class BdReportDataServiceImpl extends ServiceImpl<BdReportDataMapper, BdReportData> implements IBdReportDataService {

    private final BaiduApis baiduApis;
    private final IBdAccountService bdAccountService;

    /**
     * 查询百度定向统计
     * 
     * @param id 百度定向统计主键
     * @return 百度定向统计
     */
    @Override
    public BdReportData selectBdReportDataById(Long id)
    {
        return baseMapper.selectBdReportDataById(id);
    }

    /**
     * 查询百度定向统计列表
     * 
     * @param bdReportData 百度定向统计
     * @return 百度定向统计
     */
    @Override
    public List<BdReportData> selectBdReportDataList(BdReportData bdReportData)
    {
        return baseMapper.selectBdReportDataList(bdReportData);
    }

    /**
     * 新增百度定向统计
     * 
     * @param bdReportData 百度定向统计
     * @return 结果
     */
    @Override
    public int insertBdReportData(BdReportData bdReportData)
    {
        return baseMapper.insertBdReportData(bdReportData);
    }

    /**
     * 修改百度定向统计
     * 
     * @param bdReportData 百度定向统计
     * @return 结果
     */
    @Override
    public int updateBdReportData(BdReportData bdReportData)
    {
        return baseMapper.updateBdReportData(bdReportData);
    }

    /**
     * 批量删除百度定向统计
     * 
     * @param ids 需要删除的百度定向统计主键
     * @return 结果
     */
    @Override
    public int deleteBdReportDataByIds(Long[] ids)
    {
        return baseMapper.deleteBdReportDataByIds(ids);
    }

    /**
     * 删除百度定向统计信息
     * 
     * @param id 百度定向统计主键
     * @return 结果
     */
    @Override
    public int deleteBdReportDataById(Long id)
    {
        return baseMapper.deleteBdReportDataById(id);
    }

    @Override
    public void syncReportData(LocalDate time) {
        List<Integer> typeList = Arrays.asList(2324048, 2503330, 2510393, 2532512);
        List<BdAccount> list = bdAccountService.list();
        typeList.forEach(type->{
            QueryReportDataParam param = new QueryReportDataParam();
            param.setReportType(type);
            param.setStartDate(time);
            param.setEndDate(time);
            param.setPage(0);
            param.setRowCount(200);
            param.setStartRow(0);
            param.setTimeUnit(BdTimeUnit.DAY);
            list.forEach(account -> {
                baiduApis.getReportDataAll(account, param);
            });
        });
    }

    public void syncCreativeStatistics(LocalDate time) {
        List<BdAccount> list = bdAccountService.list();
        QueryReportDataParam param = new QueryReportDataParam();
        param.setReportType(2094816);
        param.setStartDate(time);
        param.setEndDate(time);
        param.setPage(0);
        param.setRowCount(200);
        param.setStartRow(0);
        param.setTimeUnit(BdTimeUnit.DAY);
        list.forEach(account -> baiduApis.getCreativeStatisticsAll(account, param));
    }
}
