package com.fs.app.controller;

import com.fs.app.facade.CallbackProcessingFacadeService;
import com.fs.app.facade.IConversionService;
import com.fs.common.result.Result;
import com.fs.newAdv.dto.req.QwExternalIdBindTrackReq;
import com.fs.newAdv.enums.SystemEventTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 广告商监测链接
 *
 * @author zhangqin
 */
@Slf4j
@RestController
@RequestMapping("/track")
public class TrackingController {

    @Autowired
    private CallbackProcessingFacadeService facadeService;


    @Autowired
    private IConversionService conversionService;
    /**
     * 监测链接端口
     *
     * @param allParams 所有URL参数
     * @param response  HTTP响应
     */
    @GetMapping("/click/{advertiserCode}")
    public void track(
            @RequestParam Map<String, String> allParams,
            @PathVariable("advertiserCode") Long advertiserCode,
            HttpServletResponse response) {
        log.info("接收监测请求 | params={}", allParams);
        try {
            // 2. 保存点击追踪记录
            facadeService.saveClickTrace(advertiserCode, allParams);
            // 3. 返回 200 OK
            response.setStatus(HttpServletResponse.SC_OK);
        } catch (Exception e) {
            log.error("监测请求处理失败 | params={}", allParams, e);
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }

    /**
     * 绑定企微用户与线索
     */
    @PostMapping("/bind/qwTrack")
    public Result<String> qwExternalIdBindTrack(@RequestBody QwExternalIdBindTrackReq req) {
        facadeService.qwExternalIdBindTrack(req);
        return Result.success();
    }

    /**
     * 绑定企微用户与线索
     */
    @PostMapping("/bind/qwTrack/test/{id1}/{id2}")
    public Result<String> qwExternalIdBindTrack1(@PathVariable String id1, @PathVariable String id2) {
        boolean success = conversionService.reportTrackingToAdvertiser(SystemEventTypeEnum.getByCode(id1), id2);
        return Result.success();
    }

}
