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.AdvProjectEntity;
import com.fs.newAdv.service.IAdvProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangqin
 * @since 2025-11-27
 */
@RestController
@RequestMapping("/adv/project")
public class AdvProjectController {

    @Autowired
    private IAdvProjectService advProjectService;

    /**
     * 分页查询渠道
     */
    @GetMapping("/page")
    public Result<IPage<AdvProjectEntity>> page(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String projectName) {
        Page<AdvProjectEntity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AdvProjectEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(projectName), AdvProjectEntity::getProjectName, projectName);
        wrapper.orderByDesc(AdvProjectEntity::getCreateTime);
        IPage<AdvProjectEntity> result = advProjectService.page(page, wrapper);
        return Result.success(result);
    }

    /**
     * 创建渠道/修改渠道
     */
    @PostMapping("/add")
    public Result<Void> create(@RequestBody @Validated AdvProjectEntity advertiser) {
        boolean success = false;
        if (advertiser.getId() != null) {
            success = advProjectService.updateById(advertiser);
        } else {
            success = advProjectService.save(advertiser);
        }
        return success ? Result.success() : Result.error("创建失败");
    }
}
