package com.fs.app.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.ad.domain.AdHtmlTemplate;
import com.fs.ad.domain.AdSite;
import com.fs.ad.mapper.AdHtmlTemplateMapper;
import com.fs.ad.service.IAdAccountService;
import com.fs.ad.service.IAdHtmlClickLogService;
import com.fs.ad.service.IAdSiteService;
import com.fs.baidu.api.BaiduApis;
import com.fs.baidu.service.IBdAccountService;
import com.fs.baidu.vo.ad.AdBaiduClickCallbackVo;
import com.fs.baidu.vo.AdClickCallbackVo;
import com.fs.baidu.vo.ad.AdIqiyiClickCallbackVo;
import com.fs.baidu.vo.ad.AdYouKuClickCallbackVo;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.qw.domain.QwWorkLink;
import com.fs.qw.service.IQwWorkLinkService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 知识库Controller
 *
 * @author fs
 * @date 2024-04-21
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/baidu")
public class AdCallbackController extends BaseController {

    private final AdHtmlTemplateMapper adHtmlTemplateMapper;
    private final IAdHtmlClickLogService adHtmlClickLogService;
    private final BaiduApis baiduApis;
    private final IBdAccountService bdAccountService;
    private final IAdSiteService adSiteService;
    private final IAdAccountService adAccountService;
    private final IQwWorkLinkService qwWorkLinkService;

    //百度-页面点击接口
//    @GetMapping("/callback")
//    public R callback(String url, String bdVid, String t, Long id) {
//        if(id == null || StringUtils.isEmpty(bdVid)) {
//            return R.ok();
//        }
//        adHtmlClickLogService.addLog(url, id, bdVid, t);
//        return R.ok();
//    }
    // 百度点击监听返回接口
//    @GetMapping("/clickCallback")
//    public R clickCallback(AdClickCallbackVo vo){
//        log.info("百度监听地址返回数据：{}", JSON.toJSONString(vo));
//        adHtmlClickLogService.setLogBaiDu(vo, "67", 0);
//        return R.ok();
//    }
    @GetMapping("/getTemplateByNo")
    public R getTemplateByNo(String no){
        AdHtmlTemplate htmlUrl = adHtmlTemplateMapper.selectOne(new QueryWrapper<AdHtmlTemplate>().eq("no", no));
        if(htmlUrl == null){
            return R.error("错误编号");
        }
        return R.ok().put("data",  htmlUrl);
    }
    @GetMapping("/getTemplateById")
    public R getTemplateById(Long id){
        AdSite site = adSiteService.getById(id);
        if(site.getWorkId() != null){
            QwWorkLink byId = qwWorkLinkService.getById(site.getWorkId());
            if(byId != null){
                site.setWorkUrl(byId.getUrl());
            }
        }
        AdHtmlTemplate htmlUrl = adHtmlTemplateMapper.selectById(site.getTemplateId());
        if(htmlUrl == null){
            return R.error("错误编号");
        }
        return R.ok().put("site", site).put("data",  htmlUrl);
    }
//    @GetMapping("/syncPlan")
//    public R syncPlan(){
//        baiduApis.listAccount(1L);
//        return R.ok();
//    }


    @GetMapping("/baiduClickCallbackApi")
    public R baiduClickCallbackApi(AdBaiduClickCallbackVo vo){
        log.info("百度点击监听地址返回数据：{}", JSON.toJSONString(vo));
        adHtmlClickLogService.setLogBaiDuApi(vo);
        return R.ok();
    }
    @GetMapping("/baiduClickCallback")
    public R baiduClickCallback(AdBaiduClickCallbackVo vo){
        log.info("百度监听地址返回数据：{}", JSON.toJSONString(vo));
        adHtmlClickLogService.setLogBaiDu(vo);
        return R.ok();
    }
    @GetMapping("/youkuClickCallback")
    public R youkuClickCallback(AdYouKuClickCallbackVo vo){
        log.info("优酷监听地址返回数据：{}", JSON.toJSONString(vo));
        adHtmlClickLogService.setLogYouKu(vo);
        return R.ok();
    }
    @GetMapping("/iqiyiClickCallback")
    public R iqiyiClickCallback(AdIqiyiClickCallbackVo vo){
        log.info("爱奇艺监听地址返回数据：{}", JSON.toJSONString(vo));
        adHtmlClickLogService.setLogIqiyi(vo);
        return R.ok();
    }


}
