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.Advertiser;
import com.fs.newAdv.service.IAdvertiserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 广告商管理控制器
 *
 * @author zhangqin
 * @date 2025-11-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/adv/advertiser")
public class AdvertiserController {

    @Autowired
    private IAdvertiserService advertiserService;

    /**
     * 分页查询广告商列表
     */
    @GetMapping("/page")
    public Result<IPage<Advertiser>> page(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String advertiserName,
            @RequestParam(required = false) Integer custom,
            @RequestParam(required = false) Integer enabled) {

        Page<Advertiser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Advertiser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(advertiserName), Advertiser::getAdvertiserName, advertiserName);
        wrapper.eq(custom != null, Advertiser::getCustom, custom);
        wrapper.eq(enabled != null, Advertiser::getEnabled, enabled);
        wrapper.orderByDesc(Advertiser::getCreateTime);
        IPage<Advertiser> result = advertiserService.page(page, wrapper);
        return Result.success(result);
    }

    /**
     * 创建广告商
     */
    @PostMapping
    public Result<Void> create(@RequestBody @Validated Advertiser advertiser) {
        boolean success = advertiserService.save(advertiser);
        return success ? Result.success() : Result.error("创建失败");
    }

    /**
     * 启用停用
     */
    @PostMapping("/enable/{id}")
    public Result<Void> enable(@PathVariable Long id) {
        Advertiser byId = advertiserService.getById(id);
        byId.setEnabled(byId.getEnabled() == 1 ? 0 : 1);
        advertiserService.updateById(byId);
        return Result.success();
    }


}

