package com.fs.company.controller.newAdv;

import cn.hutool.core.util.StrUtil;
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.TrackingLink;
import com.fs.newAdv.mapper.TrackingLinkMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 监测链接管理控制器
 * 提供查询接口
 *
 * @author zhangqin
 * @date 2025-11-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/adv/tracking-link")
public class TrackingLinkController {

    @Autowired
    private TrackingLinkMapper trackingLinkMapper;

    /**
     * 分页查询监测链接列表
     */
    @GetMapping("/page")
    public Result<IPage<TrackingLink>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String trackingName,
            @RequestParam(required = false) Long advertiserId,
            @RequestParam(required = false) String promotionType) {

        Page<TrackingLink> page = new Page<>(current, size);

        LambdaQueryWrapper<TrackingLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(trackingName), TrackingLink::getTrackingName, trackingName);
        wrapper.eq(advertiserId != null, TrackingLink::getAdvertiserId, advertiserId);
        wrapper.eq(StrUtil.isNotBlank(promotionType), TrackingLink::getPromotionType, promotionType);
        wrapper.orderByDesc(TrackingLink::getCreateTime);

        IPage<TrackingLink> result = trackingLinkMapper.selectPage(page, wrapper);
        return Result.success(result);
    }

    /**
     * 查询所有监测链接列表（不分页）
     */
    @GetMapping("/list")
    public Result<List<TrackingLink>> list(
            @RequestParam(required = false) String trackingName,
            @RequestParam(required = false) Long advertiserId) {

        LambdaQueryWrapper<TrackingLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(trackingName), TrackingLink::getTrackingName, trackingName);
        wrapper.eq(advertiserId != null, TrackingLink::getAdvertiserId, advertiserId);
        wrapper.orderByDesc(TrackingLink::getCreateTime);

        List<TrackingLink> list = trackingLinkMapper.selectList(wrapper);
        return Result.success(list);
    }

    /**
     * 根据ID查询监测链接详情
     */
    @GetMapping("/{id}")
    public Result<TrackingLink> getById(@PathVariable @NotNull(message = "链接ID不能为空") Long id) {
        TrackingLink trackingLink = trackingLinkMapper.selectById(id);
        return Result.success(trackingLink);
    }

    /**
     * 根据广告商ID查询监测链接列表
     */
    @GetMapping("/advertiser/{advertiserId}")
    public Result<List<TrackingLink>> getByAdvertiserId(
            @PathVariable @NotNull(message = "广告商ID不能为空") Long advertiserId) {

        LambdaQueryWrapper<TrackingLink> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TrackingLink::getAdvertiserId, advertiserId);
        wrapper.orderByDesc(TrackingLink::getCreateTime);

        List<TrackingLink> list = trackingLinkMapper.selectList(wrapper);
        return Result.success(list);
    }
}

