package com.fs.company.controller.newAdv;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.result.Result;
import com.fs.newAdv.domain.ConversionLog;
import com.fs.newAdv.service.IConversionLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/adv/conversion-log")
@Validated
public class ConversionLogController {

    @Autowired
    private IConversionLogService conversionLogService;

    @GetMapping("/page")
    public Result<IPage<ConversionLog>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String conversionType,
            @RequestParam(required = false) String advertiserId,
            @RequestParam(required = false) String callbackStatus,
            @RequestParam(required = false) String callbackAccountId,
            @RequestParam(required = false) String traceId) {
        Page<ConversionLog> pageParam = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ConversionLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(conversionType), ConversionLog::getSysConversionEvent, conversionType);
        queryWrapper.eq(ObjectUtil.isNotEmpty(advertiserId), ConversionLog::getAdvertiserId, advertiserId);
        queryWrapper.eq(ObjectUtil.isNotEmpty(callbackStatus), ConversionLog::getCallbackStatus, callbackStatus);
        queryWrapper.eq(ObjectUtil.isNotEmpty(traceId), ConversionLog::getTraceId, traceId);
        queryWrapper.eq(ObjectUtil.isNotEmpty(callbackAccountId), ConversionLog::getCallbackAccountId, callbackAccountId);
        queryWrapper.orderByDesc(ConversionLog::getCreateTime);

        IPage<ConversionLog> result = conversionLogService.page(pageParam, queryWrapper);
        return Result.success(result);
    }
}
